⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsocket.c

📁 很牛的GUI源码wxWidgets-2.8.0.zip 可在多种平台下运行.
💻 C
📖 第 1 页 / 共 3 页
字号:
  GSocket *connection = NULL ;  assert(socket != NULL);  /* Reenable CONNECTION events */  socket->m_detected &= ~GSOCK_CONNECTION_FLAG;  /* If the socket has already been created, we exit immediately */  if (socket->m_endpoint == kOTInvalidEndpointRef || !socket->m_server)  {    socket->m_error = GSOCK_INVSOCK;    return NULL;  }  /* Create a GSocket object for the new connection */  connection = GSocket_new();  if (!connection)  {    socket->m_error = GSOCK_MEMERR;    return NULL;  }  /* Wait for a connection (with timeout) */  if (_GSocket_Input_Timeout(socket) == GSOCK_TIMEDOUT)  {    GSocket_destroy(connection);    /* socket->m_error set by _GSocket_Input_Timeout */    return NULL;  }// TODO#if 0  connection->m_endpoint = accept(socket->m_endpoint, &from, (WX_SOCKLEN_T *) &fromlen);#endif  if (connection->m_endpoint == kOTInvalidEndpointRef )  {    if (errno == EWOULDBLOCK)      socket->m_error = GSOCK_WOULDBLOCK;    else      socket->m_error = GSOCK_IOERR;    GSocket_destroy(connection);    return NULL;  }  /* Initialize all fields */  connection->m_server   = FALSE;  connection->m_stream   = TRUE;  connection->m_oriented = TRUE;  /* Setup the peer address field */  connection->m_peer = GAddress_new();  if (!connection->m_peer)  {    GSocket_destroy(connection);    socket->m_error = GSOCK_MEMERR;    return NULL;  } // TODO #if 0  err = _GAddress_translate_from(connection->m_peer, &from, fromlen);  if (err != GSOCK_NOERROR)  {    GAddress_destroy(connection->m_peer);    GSocket_destroy(connection);    socket->m_error = err;    return NULL;  }  ioctl(connection->m_endpoint, FIONBIO, &arg);#endif  _GSocket_Enable_Events(connection);  return connection;}/* 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(GSocket *sck){  assert(sck != NULL);  if (sck->m_endpoint != kOTInvalidEndpointRef )  {    sck->m_error = GSOCK_INVSOCK;    return GSOCK_INVSOCK;  }  if (!sck->m_local)  {    sck->m_error = GSOCK_INVADDR;    return GSOCK_INVADDR;  }  /* Initialize all fields */  sck->m_stream   = FALSE;  sck->m_server   = FALSE;  sck->m_oriented = FALSE;  /* Create the socket */// TODO#if 0  sck->m_endpoint = socket(sck->m_local->m_realfamily, SOCK_DGRAM, 0);  socket_set_ref( sck->m_endpoint , (unsigned long) &gMacNetEvents ,  (unsigned long) sck ) ;#endif  if (sck->m_endpoint == kOTInvalidEndpointRef )  {    sck->m_error = GSOCK_IOERR;    return GSOCK_IOERR;  }// TODO#if 0  ioctl(sck->m_endpoint, FIONBIO, &arg);#endif  _GSocket_Enable_Events(sck);  /* Bind to the local address,   * and retrieve the actual address bound.   */// TODO#if 0  if ((bind(sck->m_endpoint, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||      (getsockname(sck->m_endpoint,                   sck->m_local->m_addr,                   (WX_SOCKLEN_T *) &sck->m_local->m_len) != 0))  {    close(sck->m_endpoint);    sck->m_endpoint    = -1;    sck->m_error = GSOCK_IOERR;    return GSOCK_IOERR;  }#endif  return GSOCK_NOERROR;}/* 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(GSocket *sck, GSocketStream stream){  InetAddress addr ;  TEndpointInfo	info;   OSStatus		err = kOTNoError;  TCall peer ;  assert(sck != NULL);  /* Enable CONNECTION events (needed for nonblocking connections) */  sck->m_detected &= ~GSOCK_CONNECTION_FLAG;  if (sck->m_endpoint != kOTInvalidEndpointRef )  {    sck->m_error = GSOCK_INVSOCK;    return GSOCK_INVSOCK;  }  if (!sck->m_peer)  {    sck->m_error = GSOCK_INVADDR;    return GSOCK_INVADDR;  }  /* Streamed or dgram socket? */  sck->m_stream   = (stream == GSOCK_STREAMED);  sck->m_oriented = TRUE;  sck->m_server   = FALSE;  /* Create the socket */#if TARGET_CARBON  sck->m_endpoint =  	OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ;#else  sck->m_endpoint =  	OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ;#endif  if ( sck->m_endpoint == kOTInvalidEndpointRef || err != kOTNoError )  {		sck->m_endpoint = kOTInvalidEndpointRef ;    	sck->m_error = GSOCK_IOERR;    	return GSOCK_IOERR;  }  err = OTBind( sck->m_endpoint , nil , nil ) ;  if ( err != kOTNoError )  {    	return GSOCK_IOERR;  }  SetDefaultEndpointModes( sck->m_endpoint , sck ) ;// TODO#if 0  ioctl(sck->m_endpoint, FIONBIO, &arg);#endif  _GSocket_Enable_Events(sck);  _GAddress_translate_to( sck->m_peer , &addr ) ;  memset( &peer , 0 , sizeof( TCall ) ) ;  peer.addr.len = sizeof( InetAddress ) ;  peer.addr.buf = (unsigned char*) &addr ;  err = OTConnect( sck->m_endpoint , &peer , nil ) ;  if ( err != noErr )  {    /* 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 == kOTNoDataErr ) && (!sck->m_non_blocking))    {      if (_GSocket_Output_Timeout(sck) == GSOCK_TIMEDOUT)      {      	OTSndOrderlyDisconnect( sck->m_endpoint ) ;        sck->m_endpoint = kOTInvalidEndpointRef ;        /* sck->m_error is set in _GSocket_Output_Timeout */        return GSOCK_TIMEDOUT;      }      else      {/*        int error;        WX_SOCKLEN_T len = sizeof(error);        getsockopt(sck->m_endpoint, SOL_SOCKET, SO_ERROR, (void*) &error, &len);        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 == kOTNoDataErr) && (sck->m_non_blocking))    {      sck->m_error = GSOCK_WOULDBLOCK;      return GSOCK_WOULDBLOCK;    }    /* If connect failed with an error other than EINPROGRESS,     * then the call to GSocket_Connect has failed.     */    OTSndOrderlyDisconnect( sck->m_endpoint ) ;    sck->m_endpoint = kOTInvalidEndpointRef ;    sck->m_error = GSOCK_IOERR;    return GSOCK_IOERR;  }//  OTInetEventHandler(sck, T_CONNECT , kOTNoError , NULL ) ;  return GSOCK_NOERROR;}/* Generic IO *//* Like recv(), send(), ... */int GSocket_Read(GSocket *socket, char *buffer, int size){  int ret = 0 ;  assert(socket != NULL);  /* Reenable INPUT events */  socket->m_detected &= ~GSOCK_INPUT_FLAG;  if (socket->m_endpoint == kOTInvalidEndpointRef || socket->m_server)  {    socket->m_error = GSOCK_INVSOCK;    return -1;  }  /* If the socket is blocking, wait for data (with a timeout) */  if (_GSocket_Input_Timeout(socket) == GSOCK_TIMEDOUT)    return -1;  /* Read the data */  if (socket->m_stream)    ret = _GSocket_Recv_Stream(socket, buffer, size);  else    ret = _GSocket_Recv_Dgram(socket, buffer, size);  if (ret == -1)  {    if (errno == EWOULDBLOCK)      socket->m_error = GSOCK_WOULDBLOCK;    else      socket->m_error = GSOCK_IOERR;  }  return ret;}int GSocket_Write(GSocket *socket, const char *buffer, int size){  int ret;  assert(socket != NULL);  if (socket->m_endpoint == kOTInvalidEndpointRef || socket->m_server)  {    socket->m_error = GSOCK_INVSOCK;    return -1;  }  /* If the socket is blocking, wait for writability (with a timeout) */  if (_GSocket_Output_Timeout(socket) == GSOCK_TIMEDOUT)    return -1;  /* Write the data */  if (socket->m_stream)    ret = _GSocket_Send_Stream(socket, buffer, size);  else    ret = _GSocket_Send_Dgram(socket, buffer, size);  if (ret == -1)  {    if (errno == EWOULDBLOCK)      socket->m_error = GSOCK_WOULDBLOCK;    else      socket->m_error = GSOCK_IOERR;    /* 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.     */    socket->m_detected &= ~GSOCK_OUTPUT_FLAG;    return -1;  }  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 *  mode (blocking | nonblocking) of the socket. */GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags){  assert(socket != NULL);  wxMacProcessNotifierEvents() ;  /*  state = OTGetEndpointState(socket->m_endpoint);  if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( socket->m_detected & GSOCK_INPUT_FLAG ) )  {  	size_t sz = 0 ;  	OTCountDataBytes( socket->m_endpoint , &sz ) ;  	if ( state == T_INCON || sz > 0 )  	{        socket->m_detected |= GSOCK_INPUT_FLAG ;		(socket->m_cbacks[GSOCK_INPUT])(socket, GSOCK_INPUT, socket->m_data[GSOCK_INPUT]); 	}  }  if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( socket->m_detected & GSOCK_OUTPUT_FLAG ) )  {  	if ( state == T_DATAXFER || state == T_INREL )  	{        socket->m_detected |=GSOCK_OUTPUT_FLAG ;		(socket->m_cbacks[GSOCK_OUTPUT])(socket, GSOCK_OUTPUT, socket->m_data[GSOCK_OUTPUT]);  	}  }  */  return ( flags & socket->m_detected ) ;}/* Flags *//* GSocket_SetNonBlocking: *  Sets the socket to non-blocking mode. All IO calls will return *  immediately. */void GSocket_SetNonBlocking(GSocket *socket, int non_block){  assert(socket != NULL);  socket->m_non_blocking = non_block;}/* GSocket_SetTimeout: *  Sets the timeout for blocking calls. Time is expressed in *  milliseconds. */void GSocket_SetTimeout(GSocket *socket, unsigned long millisec){  assert(socket != NULL);//  this is usually set too high and we have not yet been able to detect a closed//  stream, thus we leave the 10 sec timeout//  socket->m_timeout = millisec;}/* GSocket_GetError: *  Returns the last error which occurred for this socket. Note that successful *  operations do not clear this back to GSOCK_NOERROR, so use it only *  after an error. */GSocketError GSocket_GetError(GSocket *socket){  assert(socket != NULL);  return socket->m_error;}/* Callbacks *//* GSOCK_INPUT: *   There is data to be read in the input buffer. If, after a read *   operation, there is still data available, the callback function will *   be called again. * GSOCK_OUTPUT: *   The socket is available for writing. That is, the next write call *   won't block. This event is generated only once, when the connection is *   first established, and then only if a call failed with GSOCK_WOULDBLOCK, *   when the output buffer empties again. This means that the app should *   assume that it can write since the first OUTPUT event, and no more *   OUTPUT events will be generated unless an error occurs. * GSOCK_CONNECTION: *   Connection successfully established, for client sockets, or incoming *   client connection, for server sockets. Wait for this event (also watch *   out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. * GSOCK_LOST: *   The connection is lost (or a connection request failed); this could *   be due to a failure, or due to the peer closing it gracefully. *//* GSocket_SetCallback: *  Enables the callbacks specified by 'flags'. Note that 'flags' *  may be a combination of flags OR'ed toghether, so the same *  callback function can be made to accept different events. *  The callback function must have the following prototype: * *  void function(GSocket *socket, GSocketEvent event, char *cdata) */void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags,                         GSocketCallback callback, char *cdata){  int count;  assert(socket != NULL);  for (count = 0; count < GSOCK_MAX_EVENT; count++)  {    if ((flags & (1 << count)) != 0)    {      socket->m_cbacks[count] = callback;      socket->m_data[count] = cdata;    }  }}/* GSocket_UnsetCallback: *  Disables all callbacks specified by 'flags', which may be a *  combination of flags OR'ed toghether. */void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags){  int count;  assert(socket != NULL);  for (count = 0; count < GSOCK_MAX_EVENT; count++)  {    if ((flags & (1 << count)) != 0)    {      socket->m_cbacks[count] = NULL;      socket->m_data[count] = NULL;    }  }}#define CALL_CALLBACK(socket, event) {                                  \  _GSocket_Disable(socket, event);                                      \  if (socket->m_cbacks[event])                                          \    socket->m_cbacks[event](socket, event, socket->m_data[event]);      \}int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size){	OTFlags flags ;	OTResult res ;	OTByteCount sz = 0 ;  	OTCountDataBytes( socket->m_endpoint , &sz ) ;  	if ( size > (int)sz )  	  size = sz ;	res = OTRcv( socket->m_endpoint , buffer , size , &flags ) ;	if ( res < 0 )	{		return -1 ;	}		// we simulate another read event if there are still bytes	if ( socket->m_takesEvents )	{  		OTByteCount sz = 0 ;  		OTCountDataBytes( socket->m_endpoint , &sz ) ;  		if ( sz > 0 )  		{        	socket->m_detected |= GSOCK_INPUT_FLAG ;			(socket->m_cbacks[GSOCK_INPUT])(socket, GSOCK_INPUT, socket->m_data[GSOCK_INPUT]); 		} 	} 	return res ;}int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size){// TODO

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -