/////////////////////////////////////////////////////////////////////// /// Function: Create /// /// Author: $author$ /// Date: 1/31/2012 /////////////////////////////////////////////////////////////////////// virtual XosError Create() { XosError error = XOS_ERROR_FAILED; if (INVALID_SOCKET != (m_attached = socket (m_family, m_type, m_protocol))) { #if defined(WINDOWS) // Windows int optval = 1; int err; if ((err = setsockopt (m_attached, IPPROTO_TCP, TCP_NODELAY, (char*)&optval, sizeof(optval)))) { XOS_DBE(("() failed on setsockopt\n")); closesocket(m_attached); m_attached = INVALID_SOCKET; return error; } #else // defined(WINDOWS) // Unix #endif // defined(WINDOWS) return XOS_ERROR_NONE; } XOS_DBE(("() failed error = %d\n", GET_SOCKET_ERROR(0))); return error; }
/////////////////////////////////////////////////////////////////////// // Function: Bind // // Author: $author$ // Date: 4/6/2009 /////////////////////////////////////////////////////////////////////// virtual EvError Bind (USHORT port, const char* host=0) { EvError error = EV_ERROR_FAILED; SOCKADDR_IN sockAddrIn; int err; if (host) return EV_ERROR_NOT_IMPLEMENTED; if (INVALID_SOCKET == m_attached) return EV_ERROR_NOT_ATTACHED; if (INVALID_SOCKET_PORT == port) port = m_port; sockAddrIn.sin_family = m_family; sockAddrIn.sin_port = htons(port); sockAddrIn.sin_addr.s_addr = htonl(m_addr); if ((err = bind(m_attached, (SOCKADDR*)(&sockAddrIn), sizeof(sockAddrIn)))) DBE("() failed to bind socket error = %d\n", GET_SOCKET_ERROR(err)); else error = EV_ERROR_NONE; return error; }
/////////////////////////////////////////////////////////////////////// // Function: Read // // Author: $author$ // Date: 4/16/2009 /////////////////////////////////////////////////////////////////////// virtual LONG Read (void* buffer, ULONG size) { char* chars = (char*)(buffer); LONG recved; if (0 > (recved = recv(m_attached, chars, size, 0))) { DBE("() failed error = %d\n", GET_SOCKET_ERROR(recved)); return -EV_ERROR_READ; } return recved; }
/////////////////////////////////////////////////////////////////////// // Function: Write // // Author: $author$ // Date: 4/16/2009 /////////////////////////////////////////////////////////////////////// virtual LONG Write (const void* buffer, LONG length) { const char* chars = (const char*)(buffer); LONG sent; if (0 > (sent = send(m_attached, chars, length, 0))) { DBE("() failed error = %d\n", GET_SOCKET_ERROR(sent)); return -EV_ERROR_WRITE; } return sent; }
/////////////////////////////////////////////////////////////////////// /// Function: Close /// /// Author: $author$ /// Date: 1/31/2012 /////////////////////////////////////////////////////////////////////// virtual XosError Close() { XosError error = XOS_ERROR_NONE; int err; if (INVALID_SOCKET != m_attached) if ((err = closesocket(m_attached))) { XOS_DBE(("() failed to close socket error = %d\n", GET_SOCKET_ERROR(err))); error = XOS_ERROR_FAILED; } m_attached = INVALID_SOCKET; return error; }
/////////////////////////////////////////////////////////////////////// /// Function: Send /// /// Author: $author$ /// Date: 1/31/2012 /////////////////////////////////////////////////////////////////////// virtual ssize_t Send (const void* buffer, int length, int flags=0) { ssize_t count = -XOS_ERROR_FAILED; const char* chars = (const char*)(buffer); int sent; if (INVALID_SOCKET != m_attached) if (0 > (sent = send(m_attached, chars, length, 0))) XOS_DBE(("() failed error = %d\n", GET_SOCKET_ERROR(sent))); else count = sent; return count; }
int ei_connect_t(int fd, void *sinp, int sin_siz, unsigned ms) { int res; int error; int s_res; struct timeval tv; fd_set writefds; fd_set exceptfds; if (ms == 0) { res = connect(fd, sinp, sin_siz); return (res < 0) ? -1 : res; } else { SET_NONBLOCKING(fd); res = connect(fd, sinp, sin_siz); error = GET_SOCKET_ERROR(); SET_BLOCKING(fd); if (!MEANS_SOCKET_ERROR(res)) { return (res < 0) ? -1 : res; } else { if (error != ERROR_WOULDBLOCK && error != ERROR_INPROGRESS) { return -1; } else { tv.tv_sec = (long) (ms/1000U); ms %= 1000U; tv.tv_usec = (long) (ms * 1000U); FD_ZERO(&writefds); FD_SET(fd,&writefds); FD_ZERO(&exceptfds); FD_SET(fd,&exceptfds); s_res = select(fd + 1, NULL, &writefds, &exceptfds, &tv); switch (s_res) { case 0: return -2; case 1: if (FD_ISSET(fd, &exceptfds)) { return -1; } else { return 0; /* Connect completed */ } default: return -1; } } } } }
/////////////////////////////////////////////////////////////////////// /// Function: SetOpt /// /// Author: $author$ /// Date: 1/31/2012 /////////////////////////////////////////////////////////////////////// virtual XosError SetOpt (SOCKET_OPT_LEVEL level, SOCKET_OPT_NAME name, const SOCKET_OPT_VALUE* value, SOCKET_OPT_LENGTH length) { XosError error = XOS_ERROR_FAILED; int err; if (INVALID_SOCKET != m_attached) if ((err = setsockopt (m_attached, level, name, (const char*)value, length))) XOS_DBE(("() failed to setsockopt error = %d\n", GET_SOCKET_ERROR(err))); else error = XOS_ERROR_NONE; return error; }
/////////////////////////////////////////////////////////////////////// // Function: Listen // // Author: $author$ // Date: 4/6/2009 /////////////////////////////////////////////////////////////////////// virtual EvError Listen(int backlog=UNDEFINED) { EvError error = EV_ERROR_FAILED; int err; if (INVALID_SOCKET == m_attached) return EV_ERROR_NOT_ATTACHED; if (UNDEFINED == backlog) backlog = m_backlog; if ((err = listen(m_attached, backlog))) DBE("() failed to listen error = %d\n", GET_SOCKET_ERROR(err)); else error = EV_ERROR_NONE; return error; }
/////////////////////////////////////////////////////////////////////// // Function: ShutDown // // Author: $author$ // Date: 5/12/2009 /////////////////////////////////////////////////////////////////////// virtual EvError ShutDown (int how=SD_BOTH, bool onlyIfOpen=false) { EvError error = EV_ERROR_FAILED; int err; if (INVALID_SOCKET == m_attached) if (onlyIfOpen) return EV_ERROR_NOT_ATTACHED; else return EV_ERROR_NONE; if ((err = shutdown(m_attached, how))) { DBE("() failed to shutdown socket error = %d\n", GET_SOCKET_ERROR(err)); return error; } else error = EV_ERROR_NONE; return error; }
/////////////////////////////////////////////////////////////////////// // Function: GetOpt // // Author: $author$ // Date: 5/14/2009 /////////////////////////////////////////////////////////////////////// virtual EvError GetOpt (SOCKET_OPT_LEVEL level, SOCKET_OPT_NAME name, SOCKET_OPT_VALUE* value, SOCKET_OPT_LENGTH& length) const { EvError error = EV_ERROR_FAILED; int err; if (INVALID_SOCKET == m_attached) return EV_ERROR_NOT_ATTACHED; if ((err = getsockopt (m_attached, level, name, (char*)value, &length))) DBE("() failed to getsockopt error = %d\n", GET_SOCKET_ERROR(err)); else error = EV_ERROR_NONE; return error; }
/////////////////////////////////////////////////////////////////////// /// Function: Connect /// /// Author: $author$ /// Date: 1/31/2012 /////////////////////////////////////////////////////////////////////// virtual XosError Connect (const SOCKADDR_IN& sockAddrIn) { XosError error = XOS_ERROR_FAILED; int err; if (INVALID_SOCKET != m_attached) if ((err = connect(m_attached, (SOCKADDR*)(&sockAddrIn), sizeof(sockAddrIn)))) { XOS_DBE(("() failed to connect error = %d\n", GET_SOCKET_ERROR(err))); return error; } else { m_family = sockAddrIn.sin_family; m_addr = sockAddrIn.sin_addr.s_addr; m_port = sockAddrIn.sin_port; error = XOS_ERROR_NONE; } return error; }
/////////////////////////////////////////////////////////////////////// // Function: Close // // Author: $author$ // Date: 4/6/2009 /////////////////////////////////////////////////////////////////////// virtual EvError Close (bool onlyIfOpen=false) { EvError error = EV_ERROR_FAILED; int err; if (INVALID_SOCKET == m_attached) if (onlyIfOpen) return EV_ERROR_NOT_ATTACHED; else return EV_ERROR_NONE; if ((err = closesocket(m_attached))) { DBE("() failed to close socket error = %d\n", GET_SOCKET_ERROR(err)); return error; } else error = EV_ERROR_NONE; m_attached = INVALID_SOCKET; return error; }
/////////////////////////////////////////////////////////////////////// // Function: Create // // Author: $author$ // Date: 4/6/2009 /////////////////////////////////////////////////////////////////////// virtual EvError Create() { EvError error = EV_ERROR_FAILED; if (INVALID_SOCKET != m_attached) return EV_ERROR_ALREADY_ATTACHED; #if defined(WIN32) // Windows if ((error = EvWsa::Startup())) { DBE("() failed on wsa startup\n"); return error; } #endif // defined(WIN32) if (INVALID_SOCKET != (m_attached = socket (m_family, m_type, m_protocol))) { #if defined(WIN32) // Windows int optval = 1; int err; if ((err = setsockopt (m_attached, IPPROTO_TCP, TCP_NODELAY, (char*)&optval, sizeof(optval)))) { DBE("() failed on setsockopt\n"); closesocket(m_attached); m_attached = INVALID_SOCKET; return error; } #endif // defined(WIN32) return EV_ERROR_NONE; } DBE("() failed error = %d\n", GET_SOCKET_ERROR(0)); return error; }
/////////////////////////////////////////////////////////////////////// // Function: Accept // // Author: $author$ // Date: 4/6/2009 /////////////////////////////////////////////////////////////////////// virtual EvError Accept #if defined(WIN32) // Windows (CEvSocket& connection, bool updateAcceptContext=false) #else // defined(WIN32) // Unix (CEvSocket& connection) #endif // defined(WIN32) { EvError error = EV_ERROR_FAILED; SOCKADDR_IN sockAddrIn; socklen_t sockAddrInLen = sizeof(sockAddrIn); if (INVALID_SOCKET == m_attached) return EV_ERROR_NOT_ATTACHED; if (INVALID_SOCKET != connection.m_attached) return EV_ERROR_ALREADY_ATTACHED; if (INVALID_SOCKET != (connection.m_attached = accept (m_attached, (SOCKADDR*)(&sockAddrIn), &sockAddrInLen))) error = EV_ERROR_NONE; else { DBE("() failed to accept error = %d\n", GET_SOCKET_ERROR(-1)); return error; } #if defined(WIN32) // Windows if (updateAcceptContext) if ((error = connection. UpdateAcceptContext(m_attached))) { DBE("() failed to update accept context\n"); connection.Close(); } #endif // defined(WIN32) return error; }
/////////////////////////////////////////////////////////////////////// // Function: Connect // // Author: $author$ // Date: 4/6/2009 /////////////////////////////////////////////////////////////////////// virtual EvError Connect (USHORT port, const char* host=0) { EvError error = EV_ERROR_FAILED; SOCKADDR_IN sockAddrIn; HOSTENT *hostEnt; UINT32 addr; int err; if (INVALID_SOCKET == m_attached) return EV_ERROR_NOT_ATTACHED; if (INVALID_SOCKET_PORT == (SOCKET_PORT)(port)) port = m_port; if (!(hostEnt = gethostbyname(host))) { DBE("() Failed to gethostbyname \"%s\"\n", host); return error; } addr = ntohl(*(UINT32*)(hostEnt->h_addr)); sockAddrIn.sin_addr.s_addr = htonl(addr); sockAddrIn.sin_family = m_family; sockAddrIn.sin_port = htons(port); if ((err = connect(m_attached, (SOCKADDR*)(&sockAddrIn), sizeof(sockAddrIn)))) { DBE("() failed to connect error = %d\n", GET_SOCKET_ERROR(err)); return error; } m_addr = addr; m_port = port; error = EV_ERROR_NONE; return error; }