gsocket.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,932 行 · 第 1/4 页
CPP
1,932 行
assert(this);
/* must not be in use */
if (m_fd != INVALID_SOCKET)
{
m_error = GSOCK_INVSOCK;
return GSOCK_INVSOCK;
}
/* the local addr must have been set */
if (!m_local)
{
m_error = GSOCK_INVADDR;
return GSOCK_INVADDR;
}
/* Initialize all fields */
m_stream = true;
m_server = true;
/* Create the socket */
m_fd = socket(m_local->m_realfamily, SOCK_STREAM, 0);
if (m_fd == INVALID_SOCKET)
{
m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
/* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
#ifdef SO_NOSIGPIPE
setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(u_long));
#endif
ioctl(m_fd, FIONBIO, &arg);
gs_gui_functions->Enable_Events(this);
/* allow a socket to re-bind if the socket is in the TIME_WAIT
state after being previously closed.
*/
if (m_reusable)
setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(u_long));
/* Bind to the local address,
* retrieve the actual address bound,
* and listen up to 5 connections.
*/
if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) ||
(getsockname(m_fd,
m_local->m_addr,
(WX_SOCKLEN_T *) &m_local->m_len) != 0) ||
(listen(m_fd, 5) != 0))
{
Close();
m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
return GSOCK_NOERROR;
}
/* GSocket_WaitConnection:
* Waits for an incoming client connection. Returns a pointer to
* a GSocket object, or NULL if there was an error, in which case
* the last error field will be updated for the calling GSocket.
*
* Error codes (set in the calling GSocket)
* GSOCK_INVSOCK - the socket is not valid or not a server.
* GSOCK_TIMEDOUT - timeout, no incoming connections.
* GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
* GSOCK_MEMERR - couldn't allocate memory.
* GSOCK_IOERR - low-level error.
*/
GSocket *GSocket::WaitConnection()
{
struct sockaddr from;
WX_SOCKLEN_T fromlen = sizeof(from);
GSocket *connection;
GSocketError err;
int arg = 1;
assert(this);
/* If the socket has already been created, we exit immediately */
if (m_fd == INVALID_SOCKET || !m_server)
{
m_error = GSOCK_INVSOCK;
return NULL;
}
/* Create a GSocket object for the new connection */
connection = GSocket_new();
if (!connection)
{
m_error = GSOCK_MEMERR;
return NULL;
}
/* Wait for a connection (with timeout) */
if (Input_Timeout() == GSOCK_TIMEDOUT)
{
delete connection;
/* m_error set by _GSocket_Input_Timeout */
return NULL;
}
connection->m_fd = accept(m_fd, &from, (WX_SOCKLEN_T *) &fromlen);
/* Reenable CONNECTION events */
Enable(GSOCK_CONNECTION);
if (connection->m_fd == INVALID_SOCKET)
{
if (errno == EWOULDBLOCK)
m_error = GSOCK_WOULDBLOCK;
else
m_error = GSOCK_IOERR;
delete connection;
return NULL;
}
/* Initialize all fields */
connection->m_server = false;
connection->m_stream = true;
/* Setup the peer address field */
connection->m_peer = GAddress_new();
if (!connection->m_peer)
{
delete connection;
m_error = GSOCK_MEMERR;
return NULL;
}
err = _GAddress_translate_from(connection->m_peer, &from, fromlen);
if (err != GSOCK_NOERROR)
{
delete connection;
m_error = err;
return NULL;
}
#if defined(__EMX__) || defined(__VISAGECPP__)
ioctl(connection->m_fd, FIONBIO, (char*)&arg, sizeof(arg));
#else
ioctl(connection->m_fd, FIONBIO, &arg);
#endif
gs_gui_functions->Enable_Events(connection);
return connection;
}
bool GSocket::SetReusable()
{
/* socket must not be null, and must not be in use/already bound */
if (this && m_fd == INVALID_SOCKET) {
m_reusable = true;
return true;
}
return false;
}
/* Client specific parts */
/* GSocket_Connect:
* For stream (connection oriented) sockets, GSocket_Connect() tries
* to establish a client connection to a server using the peer address
* as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
* connection has been successfully established, or one of the error
* codes listed below. Note that for nonblocking sockets, a return
* value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
* request can be completed later; you should use GSocket_Select()
* to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
* corresponding asynchronous events.
*
* For datagram (non connection oriented) sockets, GSocket_Connect()
* just sets the peer address established with GSocket_SetPeer() as
* default destination.
*
* Error codes:
* GSOCK_INVSOCK - the socket is in use or not valid.
* GSOCK_INVADDR - the peer address has not been established.
* GSOCK_TIMEDOUT - timeout, the connection failed.
* GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
* GSOCK_MEMERR - couldn't allocate memory.
* GSOCK_IOERR - low-level error.
*/
GSocketError GSocket::Connect(GSocketStream stream)
{
int err, ret;
int arg = 1;
assert(this);
/* Enable CONNECTION events (needed for nonblocking connections) */
Enable(GSOCK_CONNECTION);
if (m_fd != INVALID_SOCKET)
{
m_error = GSOCK_INVSOCK;
return GSOCK_INVSOCK;
}
if (!m_peer)
{
m_error = GSOCK_INVADDR;
return GSOCK_INVADDR;
}
/* Streamed or dgram socket? */
m_stream = (stream == GSOCK_STREAMED);
m_server = false;
m_establishing = false;
/* Create the socket */
m_fd = socket(m_peer->m_realfamily,
m_stream? SOCK_STREAM : SOCK_DGRAM, 0);
if (m_fd == INVALID_SOCKET)
{
m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
/* FreeBSD variants can't use MSG_NOSIGNAL, and instead use a socket option */
#ifdef SO_NOSIGPIPE
setsockopt(m_fd, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&arg, sizeof(u_long));
#endif
#if defined(__EMX__) || defined(__VISAGECPP__)
ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg));
#else
ioctl(m_fd, FIONBIO, &arg);
#endif
/* Connect it to the peer address, with a timeout (see below) */
ret = connect(m_fd, m_peer->m_addr, m_peer->m_len);
/* We only call Enable_Events if we know we aren't shutting down the socket.
* NB: Enable_Events needs to be called whether the socket is blocking or
* non-blocking, it just shouldn't be called prior to knowing there is a
* connection _if_ blocking sockets are being used.
* If connect above returns 0, we are already connected and need to make the
* call to Enable_Events now.
*/
if (m_non_blocking || ret == 0)
{
gs_gui_functions->Enable_Events(this);
}
if (ret == -1)
{
err = errno;
/* If connect failed with EINPROGRESS and the GSocket object
* is in blocking mode, we select() for the specified timeout
* checking for writability to see if the connection request
* completes.
*/
if ((err == EINPROGRESS) && (!m_non_blocking))
{
if (Output_Timeout() == GSOCK_TIMEDOUT)
{
Close();
/* m_error is set in _GSocket_Output_Timeout */
return GSOCK_TIMEDOUT;
}
else
{
int error;
SOCKOPTLEN_T len = sizeof(error);
getsockopt(m_fd, SOL_SOCKET, SO_ERROR, (char*) &error, &len);
gs_gui_functions->Enable_Events(this);
if (!error)
return GSOCK_NOERROR;
}
}
/* If connect failed with EINPROGRESS and the GSocket object
* is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
* (and return GSOCK_WOULDBLOCK) but we don't close the socket;
* this way if the connection completes, a GSOCK_CONNECTION
* event will be generated, if enabled.
*/
if ((err == EINPROGRESS) && (m_non_blocking))
{
m_establishing = true;
m_error = GSOCK_WOULDBLOCK;
return GSOCK_WOULDBLOCK;
}
/* If connect failed with an error other than EINPROGRESS,
* then the call to GSocket_Connect has failed.
*/
Close();
m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
return GSOCK_NOERROR;
}
/* Datagram sockets */
/* GSocket_SetNonOriented:
* Sets up this socket as a non-connection oriented (datagram) socket.
* Before using this function, the local address must have been set
* with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
* on success, or one of the following otherwise.
*
* Error codes:
* GSOCK_INVSOCK - the socket is in use.
* GSOCK_INVADDR - the local address has not been set.
* GSOCK_IOERR - low-level error.
*/
GSocketError GSocket::SetNonOriented()
{
int arg = 1;
assert(this);
if (m_fd != INVALID_SOCKET)
{
m_error = GSOCK_INVSOCK;
return GSOCK_INVSOCK;
}
if (!m_local)
{
m_error = GSOCK_INVADDR;
return GSOCK_INVADDR;
}
/* Initialize all fields */
m_stream = false;
m_server = false;
/* Create the socket */
m_fd = socket(m_local->m_realfamily, SOCK_DGRAM, 0);
if (m_fd == INVALID_SOCKET)
{
m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
#if defined(__EMX__) || defined(__VISAGECPP__)
ioctl(m_fd, FIONBIO, (char*)&arg, sizeof(arg));
#else
ioctl(m_fd, FIONBIO, &arg);
#endif
gs_gui_functions->Enable_Events(this);
/* Bind to the local address,
* and retrieve the actual address bound.
*/
if ((bind(m_fd, m_local->m_addr, m_local->m_len) != 0) ||
(getsockname(m_fd,
m_local->m_addr,
(WX_SOCKLEN_T *) &m_local->m_len) != 0))
{
Close();
m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
return GSOCK_NOERROR;
}
/* Generic IO */
/* Like recv(), send(), ... */
int GSocket::Read(char *buffer, int size)
{
int ret;
assert(this);
if (m_fd == INVALID_SOCKET || m_server)
{
m_error = GSOCK_INVSOCK;
return -1;
}
/* Disable events during query of socket status */
Disable(GSOCK_INPUT);
/* If the socket is blocking, wait for data (with a timeout) */
if (Input_Timeout() == GSOCK_TIMEDOUT)
/* We no longer return here immediately, otherwise socket events would not be re-enabled! */
ret = -1;
else {
/* Read the data */
if (m_stream)
ret = Recv_Stream(buffer, size);
else
ret = Recv_Dgram(buffer, size);
}
/* If recv returned zero, then the connection is lost, and errno is not set.
* Otherwise, recv has returned an error (-1), in which case we have lost the
* socket only if errno does _not_ indicate that there may be more data to read.
*/
if (ret == 0)
m_error = GSOCK_IOERR;
else if (ret == -1) {
if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
m_error = GSOCK_WOULDBLOCK;
else
m_error = GSOCK_IOERR;
}
/* Enable events again now that we are done processing */
Enable(GSOCK_INPUT);
return ret;
}
int GSocket::Write(const char *buffer, int size)
{
int ret;
assert(this);
GSocket_Debug(( "GSocket_Write #1, size %d\n", size ));
if (m_fd == INVALID_SOCKET || m_server)
{
m_error = GSOCK_INVSOCK;
return -1;
}
GSocket_Debug(( "GSocket_Write #2, size %d\n", size ));
/* If the socket is blocking, wait for writability (with a timeout) */
if (Output_Timeout() == GSOCK_TIMEDOUT)
return -1;
GSocket_Debug(( "GSocket_Write #3, size %d\n", size ));
/* Write the data */
if (m_stream)
ret = Send_Stream(buffer, size);
else
ret = Send_Dgram(buffer, size);
GSocket_Debug(( "GSocket_Write #4, size %d\n", size ));
if (ret == -1)
{
if ((errno == EWOULDBLOCK) || (errno == EAGAIN))
{
m_error = GSOCK_WOULDBLOCK;
GSocket_Debug(( "GSocket_Write error WOULDBLOCK\n" ));
}
else
{
m_error = GSOCK_IOERR;
GSocket_Debug(( "GSocket_Write error IOERR\n" ));
}
/* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
* in MSW). Once the first OUTPUT event is received, users can assume
* that the socket is writable until a read operation fails. Only then
* will further OUTPUT events be posted.
*/
Enable(GSOCK_OUTPUT);
return -1;
}
GSocket_Debug(( "GSocket_Write #5, size %d ret %d\n", size, ret ));
return ret;
}
/* GSocket_Select:
* Polls the socket to determine its status. This function will
* check for the events specified in the 'flags' parameter, and
* it will return a mask indicating which operations can be
* performed. This function won't block, regardless of the
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?