- /* 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;
-}
-
-/* GSocket_SetBroadcast:
-* Simply sets the m_broadcast flag on the socket. GSocket_SetServer will
-* make the appropriate setsockopt() call.
-* Implemented as a GSocket function because clients (ie, wxSocketServer)
-* don't have access to the GSocket struct information.
-* Returns true if the flag was set correctly, false if an error occurred
-* (ie, if the parameter was NULL)
-*/
-bool GSocket::SetBroadcast()
-{
- /* socket must not be in use/already bound */
- if (m_fd == INVALID_SOCKET) {
- m_broadcast = true;
- return true;
- }
- return false;
-}
-
-bool GSocket::DontDoBind()
-{
- /* socket must not be in use/already bound */
- if (m_fd == INVALID_SOCKET) {
- m_dobind = false;
- 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 ret, err;
- u_long arg = 1;
-
- /* Enable CONNECTION events (needed for nonblocking connections) */
- m_detected &= ~GSOCK_CONNECTION_FLAG;
-
- 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;
- }
-
- ioctlsocket(m_fd, FIONBIO, (u_long FAR *) &arg);
- GSocketManager::Get()->Enable_Events(this);
-
- // If the reuse flag is set, use the applicable socket reuse flag
- if (m_reusable)
- {
- setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&arg, sizeof(arg));
- }
-
- if (m_initialRecvBufferSize >= 0)
- setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, (const char*)&m_initialRecvBufferSize, sizeof(m_initialRecvBufferSize));
- if (m_initialSendBufferSize >= 0)
- setsockopt(m_fd, SOL_SOCKET, SO_SNDBUF, (const char*)&m_initialSendBufferSize, sizeof(m_initialSendBufferSize));
-
- // If a local address has been set, then we need to bind to it before calling connect
- if (m_local && m_local->m_addr)
- {
- bind(m_fd, m_local->m_addr, m_local->m_len);
- }
-
- /* Connect it to the peer address, with a timeout (see below) */
- ret = connect(m_fd, m_peer->m_addr, m_peer->m_len);
-
- if (ret == SOCKET_ERROR)
- {
- err = WSAGetLastError();
-
- /* If connect failed with EWOULDBLOCK 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 == WSAEWOULDBLOCK) && (!m_non_blocking))