static inline int ight_socket_normalize_if_invalid_unix_(int filenum) { /* * This makes sense only in the Unix world in which all negative * integers different from -1 are non-canonical representations of * the invalid socket descriptor. */ if (!ight_socket_valid(filenum)) filenum = IGHT_SOCKET_INVALID; return (filenum); }
IghtConnectionState::IghtConnectionState(const char *family, const char *address, const char *port, long long filenum) { auto evbase = ight_get_global_event_base(); auto poller = ight_get_global_poller(); if (!ight_socket_valid(filenum)) filenum = IGHT_SOCKET_INVALID; /* Normalize */ /* * TODO: switch to RAII. */ // filedesc: if valid, it is set on success only if ((this->bev = bufferevent_socket_new(evbase, (evutil_socket_t) filenum, BEV_OPT_DEFER_CALLBACKS)) == NULL) throw std::bad_alloc(); // closing: nothing to be done if (!ight_socket_valid(filenum)) this->connecting = 1; // reading: nothing to be done if ((this->address = strdup(address)) == NULL) { bufferevent_free(this->bev); throw std::bad_alloc(); } if ((this->port = strdup(port)) == NULL) { bufferevent_free(this->bev); free(this->address); throw std::bad_alloc(); } if ((this->addrlist = new (std::nothrow) IghtStringVector(poller, 16)) == NULL) { bufferevent_free(this->bev); free(this->address); free(this->port); throw std::bad_alloc(); } if ((this->family = strdup(family)) == NULL) { bufferevent_free(this->bev); free(this->address); free(this->port); delete (this->addrlist); throw std::bad_alloc(); } if ((this->pflist = new (std::nothrow) IghtStringVector(poller, 16)) == NULL) { bufferevent_free(this->bev); free(this->address); free(this->port); delete (this->addrlist); free(this->family); throw std::bad_alloc(); } // must_resolve_ipv4: if connecting, set later by this->resolve() // must_resolve_ipv6: if connecting, set later by this->resolve() /* * The following makes this non copyable and non movable. */ bufferevent_setcb(this->bev, this->handle_read, this->handle_write, this->handle_event, this); if (!ight_socket_valid(filenum)) this->start_connect = IghtDelayedCall(0.0, std::bind( this->resolve, this)); else this->filedesc = filenum; /* Own the socket on success */ }