void TSocket::setSocketOptions(const Options& options) { // Connect timeout options_.connTimeout = options.connTimeout; // Send timeout if (options.sendTimeout >= 0) { setSendTimeout(options.sendTimeout); } // Recv timeout if (options.recvTimeout >= 0) { setRecvTimeout(options.recvTimeout); } // Send Buffer Size if (options.sendBufSize > 0) { setSendBufSize(options.sendBufSize); } // Recv Buffer Size if (options.recvBufSize > 0) { setRecvBufSize(options.recvBufSize); } // Linger setLinger(options.lingerOn, options.lingerVal); // No delay setNoDelay(options.noDelay); setReuseAddress(options.reuseAddr); }
static void connectHandler(int fd, void *data, int flags) { privateSocketStruct *pss= (privateSocketStruct *)data; FPRINTF((stderr, "connectHandler(%d, %p, %d)\n", fd, data, flags)); if (flags & AIO_X) /* -- exception */ { /* error during asynchronous connect() */ aioDisable(fd); pss->sockError= socketError(fd); pss->sockState= Unconnected; perror("connectHandler"); } else /* (flags & AIO_W) -- connect completed */ { /* connect() has completed */ int error= socketError(fd); if (error) { FPRINTF((stderr, "connectHandler: error %d (%s)\n", error, strerror(error))); pss->sockError= error; pss->sockState= Unconnected; } else { pss->sockState= Connected; setLinger(pss->s, 1); } } notify(pss, CONN_NOTIFY); }
// linger: Integer containing the linger period (ms) bool ZeroMQHandler::setSlotLinger(const Basic::Integer* const msg) { // Save the linger period for use in the initialization of the // socket bool ok = false; if (msg != nullptr) ok = setLinger(*msg); return ok; }
ZmqSocket::~ZmqSocket() { if (sock != 0) { setLinger(0); close(); } }
void sqSocketAbortConnection(SocketPtr s) { FPRINTF((stderr, "abortConnection(%d)\n", SOCKET(s))); if (!socketValid(s)) return; setLinger(SOCKET(s), 0); sqSocketCloseConnection(s); }
unsigned short ZmqSocket::create(ZmqContext &context, int type) { CHECK_RET(context.isInitialize()); CHECK_RET_CODE(sock == 0, ERR_CONNECTED); sock = zmq_socket(context, type); CHECK_API_RET(sock != 0); this->type = type; setLinger(0); return NO_ERR; }
/* TCP => open a connection. * UDP => set remote address. */ void sqSocketConnectToPort(SocketPtr s, sqInt addr, sqInt port) { struct sockaddr_in saddr; if (!socketValid(s)) return; FPRINTF((stderr, "connectTo(%d)\n", SOCKET(s))); memset(&saddr, 0, sizeof(saddr)); saddr.sin_family= AF_INET; saddr.sin_port= htons((short)port); saddr.sin_addr.s_addr= htonl(addr); if (TCPSocketType != s->socketType) { /* --- UDP/RAW --- */ if (SOCKET(s) >= 0) { memcpy((void *)&SOCKETPEER(s), (void *)&saddr, sizeof(SOCKETPEER(s))); SOCKETSTATE(s)= Connected; } } else { /* --- TCP --- */ int result; aioEnable(SOCKET(s), PSP(s), 0); result= connect(SOCKET(s), (struct sockaddr *)&saddr, sizeof(saddr)); FPRINTF((stderr, "connect() => %d\n", result)); if (result == 0) { /* connection completed synchronously */ SOCKETSTATE(s)= Connected; notify(PSP(s), CONN_NOTIFY); setLinger(SOCKET(s), 1); } else { if (errno == EINPROGRESS || errno == EWOULDBLOCK) { /* asynchronous connection in progress */ SOCKETSTATE(s)= WaitingForConnection; aioHandle(SOCKET(s), connectHandler, AIO_WX); /* W => connect() */ } else { /* connection error */ perror("sqConnectToPort"); SOCKETSTATE(s)= Unconnected; SOCKETERROR(s)= errno; notify(PSP(s), CONN_NOTIFY); } } } }
/* accept() can now be performed for the socket: call accept(), and replace the server socket with the new client socket leaving the client socket unhandled */ static void acceptHandler(int fd, void *data, int flags) { privateSocketStruct *pss= (privateSocketStruct *)data; FPRINTF((stderr, "acceptHandler(%d, %p ,%d)\n", fd, data, flags)); if (flags & AIO_X) /* -- exception */ { /* error during listen() */ aioDisable(fd); pss->sockError= socketError(fd); pss->sockState= Invalid; pss->s= -1; close(fd); fprintf(stderr, "acceptHandler: aborting server %d pss=%p\n", fd, pss); } else /* (flags & AIO_R) -- accept() is ready */ { int newSock= accept(fd, 0, 0); if (newSock < 0) { if (errno == ECONNABORTED) { /* let's just pretend this never happened */ aioHandle(fd, acceptHandler, AIO_RX); return; } /* something really went wrong */ pss->sockError= errno; pss->sockState= Invalid; perror("acceptHandler"); aioDisable(fd); close(fd); fprintf(stderr, "acceptHandler: aborting server %d pss=%p\n", fd, pss); } else /* newSock >= 0 -- connection accepted */ { pss->sockState= Connected; setLinger(newSock, 1); if (pss->multiListen) { pss->acceptedSock= newSock; } else /* traditional listen -- replace server with client in-place */ { aioDisable(fd); close(fd); pss->s= newSock; aioEnable(newSock, pss, 0); } } } notify(pss, CONN_NOTIFY); }
Persistor() : Module() , socket_(nullptr) , id_("Persistor") { socket_ = zmq_socket(ctx_, ZMQ_DEALER); assert(socket_); setLinger(socket_); setIdentity(socket_, id_); int rc = zmq_bind(socket_, "tcp://127.0.0.1:5563"); assert(rc); }
void TSocket::write(const uint8_t* buf, uint32_t len) { uint32_t sent = 0; while (sent < len) { uint32_t b = write_partial(buf + sent, len - sent); if (b == 0) { // This should only happen if the timeout set with SO_SNDTIMEO expired. // Raise an exception. // However, first set SO_LINGER to 0 seconds for this socket. We think // the remote endpoint is not responding, so when we call close() we just // want the server to drop any untransmitted data immediately, instead of // moving into the TCP_FIN_WAIT1 state and continuing to try and send it. setLinger(true, 0); throw TTransportException(TTransportException::TIMED_OUT, "send timeout expired " + getSocketInfo()); } sent += b; } }
/** @return false and set error code and description string on failure */ AREXPORT bool ArSocket::open(int port, Type type, const char *openOnIP) { int ret; char localhost[MAXGETHOSTSTRUCT]; myError = NoErr; myErrorStr.clear(); if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)) { //ret=WSAGetLastError(); myError = NetFail; myErrorStr="Failure to make TCP socket"; ArLog::logErrorFromOS(ArLog::Normal, "ArSocket::open: could not create tcp socket"); return(false); } else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)) { myError = NetFail; myErrorStr="Failure to make UDP socket"; ArLog::logErrorFromOS(ArLog::Normal, "ArSocket::open: could not create udp socket"); return(false); } setLinger(0); setReuseAddress(); /* MPL this is useless withw hat I Took out below if (gethostname(localhost, sizeof(localhost)) == 1) { myErrorStr="Failure to locate localhost"; ArLog::logErrorFromOS(ArLog::Normal, "ArSocket::open: gethostname failed"); return(false); } */ memset(&mySin, 0, sizeof(mySin)); /* MPL took this out since it was just overriding it with the INADDR_ANY anyways and it could cause slowdowns if a machine wasn't configured so lookups are quick if (!hostAddr(localhost, mySin.sin_addr)) return(false); */ setRawIPString(); if (openOnIP != NULL) { if (!hostAddr(openOnIP, mySin.sin_addr)) { myError = NameLookup; myErrorStr = "Name lookup failed"; ArLog::log(ArLog::Normal, "Couldn't find ip of %s to open on", openOnIP); ::shutdown(myFD, SD_BOTH); myFD = INVALID_SOCKET; return(false); } else { //printf("Opening on %s\n", openOnIP); } } else { mySin.sin_addr.s_addr=htonl(INADDR_ANY); } mySin.sin_family=AF_INET; mySin.sin_port=hostToNetOrder(port); myType=type; if ((ret=bind(myFD, (struct sockaddr *)&mySin, sizeof(mySin))) < 0) { myErrorStr="Failure to bind socket to port "; sprintf(localhost, "%d", port); myErrorStr+=localhost; myError = NetFail; ArLog::logErrorFromOS(ArLog::Normal, "ArSocket::open: could not bind"); ::shutdown(myFD, SD_BOTH); myFD = INVALID_SOCKET; return(false); } if ((type == TCP) && (listen(myFD, 5) < 0)) { myErrorStr="Failure to listen on socket"; myError = NetFail; ArLog::logErrorFromOS(ArLog::Normal, "ArSocket::open: could not listen"); ::shutdown(myFD, SD_BOTH); myFD = INVALID_SOCKET; return(false); } return(true); }
/** @return false and set error code and description string on failure */ bool ArSocket::open(int port, Type type, const char *openOnIP) { int ret; char localhost[maxHostNameLen()]; myError=NoErr; myErrorStr.clear(); if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0)) { myErrorStr="Failure to make TCP socket"; perror("socket"); return(false); } else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0)) { myErrorStr="Failure to make UDP socket"; perror("socket"); return(false); } ArUtil::setFileCloseOnExec(myFD); setLinger(0); setReuseAddress(); myType=type; /* MPL removed this since with what I Took out down below months ago if (gethostname(localhost, sizeof(localhost)) == 1) { myErrorStr="Failure to locate localhost"; perror("gethostname"); return(false); } */ bzero(&mySin, sizeof(mySin)); /* MPL took this out since it was just overriding it with the INADDR_ANY anyways and it could cause slowdowns if a machine wasn't configured so lookups are quick if (!hostAddr(localhost, mySin.sin_addr) && !hostAddr("localhost", mySin.sin_addr)) return(false); */ if (openOnIP != NULL) { if (!hostAddr(openOnIP, mySin.sin_addr)) { ArLog::log(ArLog::Normal, "Couldn't find ip of %s to open on", openOnIP); myError = NameLookup; myErrorStr = "Name lookup failed"; return(false); } else { //printf("Opening on %s\n", openOnIP); } } else { mySin.sin_addr.s_addr=htonl(INADDR_ANY); } setRawIPString(); mySin.sin_family=AF_INET; mySin.sin_port=hostToNetOrder(port); if ((ret=bind(myFD, (struct sockaddr *)&mySin, sizeof(mySin))) < 0) { myError = NetFail; myErrorStr="Failure to bind socket to port "; sprintf(localhost, "%d", port); myErrorStr+=localhost; perror("socket"); return(false); } if ((type == TCP) && (listen(myFD, 5) < 0)) { myError = NetFail; myErrorStr="Failure to listen on socket"; perror("listen"); return(false); } return(true); }