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

📄 tcpw32.c

📁 extremeDB s sample code,useful for you
💻 C
📖 第 1 页 / 共 2 页
字号:
 * * IN nw_channel_h   chan       - pointer to a channel descriptor * IN const char connect_string - interconnect dependent connection-string * IN timer_unit timeout        - connection timeout * * Return: *  * Returns MSO_S_OK if successful or error code (see above). *  *************************************************************************/MCO_RET nw_connect(                   nw_channel_h chan,                      const char * connect_string,                   timer_unit timeout                  ){  const char *cc = connect_string;  char name[128];//  SOCKADDR_IN   csin;  uint4         mode = 1;  int           len;  conn_msg_t    msg;  MCO_RET       rc;  struct hostent *he;    chan->status = 0;/* Translate interconnect-dependent connect-string * Format: "xxx.xxx.xxx.xxx:nnnn" or "inet-name:port" * * Find ':' in connect string */    init_channel( chan );    for (;;)    {      if ( *cc == '\0' ) return (MCO_RET)(MCO_E_NW_INVADDR);      if ( *cc == ':' ) break;      cc++;    }    cc++;    len = cc-connect_string;    if ((int)(cc-connect_string) >= (int)sizeof(name) ) return (MCO_RET)(MCO_E_NW_NOMEM);    strncpy(name, connect_string, (int)(cc-connect_string-1));    name[(int)(cc-connect_string-1)] = 0;/* * Translate IP address */    if ( (he = gethostbyname(name)) == NULL ) return (MCO_RET)(MCO_E_NW_INVADDR);    chan->sin.sin_addr.S_un.S_addr = *(PULONG)(he->h_addr_list[0]);    chan->sin.sin_family = AF_INET;     chan->sin.sin_port = htons((uint2)atoi(cc));    if( (rc=nw_create_socket(chan)) != MCO_S_OK) {      return rc;    }        chan->event = CreateEvent( NULL, 0, 0, NULL);    chan->event1 = CreateEvent( NULL, 0, 0, NULL);    if ( (connect((SOCKET)chan->socket, (LPSOCKADDR)&chan->sin, sizeof(chan->sin))) < 0 ) {ErrRet: nw_close(chan);        return (MCO_RET)(MCO_E_NW_CONNECT);    }    chan->status = NWST_INITIALIZED | NWST_CONNECTED;    if(        nw_recv(              chan,              (PCHAR)&msg,              sizeof(conn_msg_t),              (PSHORT)&len,              timeout)        !=MCO_S_OK      )    {        goto ErrRet;    }    chan->index = msg.index;    return MCO_S_OK;}/* * Not implemented. The stubs for the compliance with other platforms *//*************************************************************************** * * Accept the cancel point connection * * IN  nw_channel_h chan      - pointer to the listener channel. * IN OUT nw_channel_h  ioch  - pointer to the IO channel waiting for connection. * IN  unsigned long timeout) - wait-for-connect timeout. *  * Description: * Waits for the connection of a remote host to the IO channel. * * Returns MSO_S_OK if successful or error code (see above). */MCO_RET nw_accept_cancel_point(                   nw_channel_h chan,                   char * port,                   timer_unit timeout                   ){    return MCO_S_OK;}/*************************************************************************** * * Connect to cancel point * * IN nw_channel_h   chan       - pointer to a channel descriptor * IN const char connect_string - interconnect dependent connection-string * IN  unsigned long timeout); *  * Description: * Connects IO channel to the remote host by it's name. * * Returns MSO_S_OK if successful or error code (see above). */MCO_RET nw_connect_cancel_point( nw_channel_h chan,                                 const char * connect_string,                                   timer_unit timeout                               ){    return MCO_S_OK;}/************************************************************************* *  * Description: * * Low level data transfer. * Sends a block of data to the communication channel * * Parameters: * * IN nw_channel_h  chan    - pointer to a channel descriptor. * IN char*  buffer         - buffer to send. * IN int2 buflen           - buffer length,is cut to 32767 bytes. * IN timer_unit timeout    - send timeout in milliseconds. It is media dependent *                            & application dependent. Application MUST set this  *                            parameter considering the media rate & it's own needs. *  * Return: * * Returns MSO_S_OK if successful or error code (see above). * *************************************************************************/MCO_RET nw_send (                  nw_channel_h chan,                     char* buffer,                  int2 buflen,                     timer_unit timeout                ){  int             rcs;  WSAOVERLAPPED   ovl;  WSABUF          wsabuf;  uint4           flags;  timer_unit      time  = timeout;  timer_unit      t1, t2;  uint4           st;    if(timeout != MCO_TM_INFINITE) {      t1 = t2 = mco_system_get_current_time();    }    if( (chan->status&(NWST_INITIALIZED|NWST_CONNECTED)) !=  (NWST_INITIALIZED|NWST_CONNECTED) ) {      return (MCO_RET)(MCO_E_NW_CONNECT);    }    while( buflen )    {      if(timeout != MCO_TM_INFINITE) {        if( ((t2=mco_system_get_current_time())-t1) > time) {          return (MCO_RET)(MCO_E_NW_TIMEOUT);        }        time -= t2 - t1;        t2 = t1;      }      wsabuf.buf = (PCHAR)buffer;      wsabuf.len = (uint4)buflen;      ovl.hEvent = chan->event;      if(          WSASend( chan->socket,  &wsabuf, 1,                (PULONG)&rcs, 0, &ovl, NULL        ) <0 )      {         int err;         if((err=WSAGetLastError())!=WSA_IO_PENDING) {            return (MCO_RET)(MCO_E_NW_SENDERR);         }         if((st = WSAWaitForMultipleEvents(2,(const WSAEVENT FAR *)&chan->event,0,time,0))           != WSA_WAIT_EVENT_0 ) {           if(st == WSA_WAIT_FAILED) {            return (MCO_RET)(MCO_E_NW_FATAL);           }           if(st == WSA_WAIT_EVENT_0+1) {            return (MCO_RET)(MCO_E_NW_CANCEL);           }            return (MCO_RET)(MCO_E_NW_TIMEOUT);         }         WSAGetOverlappedResult( chan->socket, &ovl,                               (PULONG)&rcs, 0, (PDWORD)&flags );      }      if( !rcs )  { // graceful closure       return (MCO_RET)(MCO_E_NW_SENDERR);      }      buflen = (short)(buflen - (int2) rcs);      NumBytes += rcs;      buffer += rcs;    }    return MCO_S_OK;}/************************************************************************ * *  Description: * * Low level data transfer. * Receives block of data from the communication channel * *  Parameters: * * IN  nw_channel_h chan  - pointer to a channel descriptor. * OUT char* buffer       - buffer to receive. * IN  int2 buflen        - buffer length limit,  is cut to 32767 bytes. * OUT int2* recvlen,     - actual received length, is cut to 32767 bytes. * IN  timer_unit timeout - receive timeout in milliseconds. It is media dependent *                            & application dependet. Application MUST set this parameter *                            considering the media rate & it's own needs *  Return: * * Returns MSO_S_OK if successful or error code (see above). * ************************************************************************/MCO_RET nw_recv (                  nw_channel_h chan,                   char* buffer,                    int2 buflen,                 int2* recvlen,                   timer_unit timeout                ){  int             rcs;  int2            length = buflen;  WSAOVERLAPPED   ovl;  WSABUF          wsabuf;  uint4           flags;  timer_unit      time  = timeout;  timer_unit      t1, t2;  uint4           st;  if(timeout != MCO_TM_INFINITE)    t1 = t2 = mco_system_get_current_time();  if( (chan->status&(NWST_INITIALIZED|NWST_CONNECTED)) !=  (NWST_INITIALIZED|NWST_CONNECTED) )  {    return (MCO_RET)(MCO_E_NW_CONNECT);  }  while (length) {    if(timeout != MCO_TM_INFINITE) {      if( (((t2=mco_system_get_current_time())-t1)) > time) {        return (MCO_RET)(MCO_E_NW_TIMEOUT);      }      time -= t2 - t1;      t2 = t1;    }    wsabuf.buf = (PCHAR)buffer;    wsabuf.len = (uint4)length;    ovl.hEvent = chan->event;    flags      = 0;    if (WSARecv ( chan->socket, &wsabuf, 1, (PULONG)&rcs,                  (PDWORD)&flags, &ovl, NULL ) < 0 ) {      int err;      if((err = WSAGetLastError())!=WSA_IO_PENDING) {        return (MCO_RET)(MCO_E_NW_RECVERR);      }      if((st =WSAWaitForMultipleEvents(2,(const WSAEVENT FAR *)&chan->event,0,time,0))       != WSA_WAIT_EVENT_0 ) {         if(st == WSA_WAIT_EVENT_0+1) {#ifdef  NW_DEBUG_OUTPUT           Printf("Replica was cancelled\n");#endif           return (MCO_RET)(MCO_E_NW_CANCEL);         }         return (MCO_RET)(MCO_E_NW_TIMEOUT);      }      WSAGetOverlappedResult( chan->socket, &ovl,                             (PULONG)&rcs, 0, (PDWORD)&flags );    }    if( !rcs )  { // graceful closure      return (MCO_RET)(MCO_E_NW_RECVERR);    }    length = (short)(length - (short)rcs);    NumBytes += rcs;    buffer += rcs;  }  *recvlen = (int2)buflen;  return MCO_S_OK;}/************************************************************************ * * Cancels the communication channel * *  Parameters: * * IN nw_channel_h ch      - pointer to a channel descriptor. * ************************************************************************/void  nw_cancel(nw_channel_h ch){  ch->status |= NWST_CANCEL;  SetEvent(ch->event1 );}#endif// CFG_TCP_SOCKET_CHANNEL#endif /* _WIN32 */

⌨️ 快捷键说明

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