📄 inettransport.c
字号:
rc = OBX_RC_ERR_SOCKET_CREATE; } } else { OBXDBGERR(("[ERROR] iobxInetTransportListen() server socket already listening.\n")); rc = OBX_RC_ERR_SOCKET_ALREADY_LISTENING; } return rc;}/*** Accept an inbound connection from a peer transport. This call should block until** a connection has been established.** When a connection has been accepted, the passed 'connectionid' is set. This will be** provided by the caller on all subsuquent calls made against the active connection.*/ObxRc iobxInetTransportAccept( void **connectionid ) { int addrlen = sizeof( struct sockaddr_in ); ObxRc rc = OBX_RC_OK; ObxInetConnectionBlock *conblk = NULL; OBXDBGFLOW(("iobxInetTransportAccept() entry, connid=0x%08x\n", *connectionid)); if ( obxServer.active ) { if ( *connectionid ) { conblk = *connectionid; if ( conblk->connected ) { conblk->connected = FALSE; sclose( conblk->fd ); } OBXDBGINFO(("iobxInetTransportAccept() calling accept().\n")); if ( (conblk->fd = accept( obxServer.fd, (struct sockaddr *)&conblk->peer, &addrlen )) >= 0 ) { OBXDBGINFO(("iobxInetTransportAccept() accept() returns.\n")); conblk->connected = TRUE; } else { OBXDBGERR(("[ERROR] iobxInetTransportAccept() socket accept fails.\n")); OBXDBGSOCKERR(); rc = OBX_RC_ERR_SOCKET_ACCEPT; } } else { OBXDBGERR(("[ERROR] iobxInetTransportAccept() bad plist on call.\n")); rc = OBX_RC_ERR_BADPLIST; /* No connection block */ } } else { OBXDBGERR(("[ERROR] iobxInetTransportAccept() socket not listening.\n")); rc = OBX_RC_ERR_SOCKET_NOT_LISTENING; /* listen() not called */ } return rc;}/* ************************************** *//* When transport is acting as client *//* ************************************** *//*** Initiate a connection to a remote peer transport.** When a connection has been created, the passed 'connectionid' is set. This will be** provided by the caller on all subsuquent calls made against the active connection.*/ObxRc iobxInetTransportConnect( void **connectionid ) { ObxRc rc = OBX_RC_OK; ObxInetConnectionBlock *conblk = NULL; OBXDBGFLOW(("iobxInetTransportConnect() entry, connid=0x%08x\n", *connectionid)); if ( *connectionid ) { conblk = *connectionid; if ( conblk->connected ) { sclose( conblk->fd ); } conblk->fd = socket( AF_INET, SOCK_STREAM, 0 ); if ( conblk->fd >= 0 ) { memset( &conblk->peer, 0, sizeof(struct sockaddr_in) ); conblk->peer.sin_family = AF_INET; conblk->peer.sin_port = htons( (unsigned short)((obxMeta.port <= 0) ? OBEX_PORT : obxMeta.port) ); if ( (rc = iobxGetPeerAddr( obxMeta.host, &conblk->peer )) == OBX_RC_OK ) { OBXDBGINFO(("iobxInetTransportConnect() attempting connect() host=%s\tport=%d.\n", obxMeta.host, obxMeta.port)); if ( connect( conblk->fd, (struct sockaddr *)&conblk->peer, sizeof(struct sockaddr_in)) >= 0 ) { OBXDBGINFO(("iobxInetTransportConnect() connect() succeeds.\n")); conblk->connected = TRUE; } else { OBXDBGERR(("[ERROR] iobxInetTransportConnect() socket connect fails.\n")); OBXDBGSOCKERR(); rc = OBX_RC_ERR_SOCKET_CONNECT; } } else { OBXDBGERR(("[ERROR] iobxInetTransportConnect() unexpected rc from iobxGetPeerAddr().\n")); } } else { OBXDBGERR(("[ERROR] iobxInetTransportConnect() socket create fails.\n")); rc = OBX_RC_ERR_SOCKET_CREATE; } } else { OBXDBGERR(("[ERROR] iobxInetTransportConnect() bad plist on call.\n")); rc = OBX_RC_ERR_BADPLIST; /* no con block */ } return rc;}/* ************************************** *//* Functions used on connected transports *//* ************************************** *//*** Send 'length' bytes of data from 'buf', set the actual number** written in 'wrote'.** Note that the inbound 'connectionid' was created by either a connect() or accept() call.*/ObxRc iobxInetTransportSend( void **connectionid, const void *buf, int length, int *wrote, short allowShort ) { ObxRc rc = OBX_RC_OK; ObxInetConnectionBlock *conblk = NULL; int remainingSend = length; void *cursor; int sent; OBXDBGFLOW(("iobxInetTransportSend() entry, connid=0x%08x\tlength=%d\tallowShort=%d\n", *connectionid, length, allowShort)); OBXDBGMEM(("iobxInetTransportSend()", buf, length)); *wrote = 0; if ( *connectionid ) { conblk = *connectionid; cursor = (void *)buf; do { sent = swrite( conblk->fd, cursor, remainingSend ); if ( sent != SOCKET_ERROR ) { OBXDBGINFO(("iobxInetTransportSend() socket send, %d bytes sent.\n", sent)); remainingSend -= sent; (unsigned char *)cursor += sent; /* Treat as bytes */ *wrote += sent; } else { OBXDBGERR(("[ERROR] iobxInetTransportSend() socket send resulted in SOCKET_ERROR.\n")); OBXDBGSOCKERR(); rc = OBX_RC_ERR_SOCKET; } } while ( allowShort == FALSE && remainingSend > 0 && rc == OBX_RC_OK ); } else { OBXDBGERR(("[ERROR] iobxInetTransportSend() bad plist on call.\n")); rc = OBX_RC_ERR_BADPLIST; /* No con block */ } return rc;}/*** Receive 'length' bytes of data and place into 'buf', set the actual** number of bytes read in 'actual'.** Note that the inbound 'connectionid' was created by either a connect() or accept() call.*/ObxRc iobxInetTransportRecv( void **connectionid, void *buf, int length, int *actual, short allowShort ) { ObxRc rc = OBX_RC_OK; ObxInetConnectionBlock *conblk = NULL; int remainingRead = length; void *cursor; int iread; OBXDBGFLOW(("iobxInetTransportRecv() entry, connid=0x%08x\tlength=%d\n", *connectionid, length)); *actual = 0; if ( *connectionid ) { conblk = *connectionid; cursor = buf; do { iread = sread( conblk->fd, cursor, remainingRead ); switch ( iread ) { case SOCKET_ERROR: OBXDBGERR(("[ERROR] iobxInetTransportRecv() socket send resulted in SOCKET_ERROR.\n")); OBXDBGSOCKERR(); rc = OBX_RC_ERR_SOCKET; break; case 0: OBXDBGINFO(("iobxInetTransportRecv() socket read results in EOF.\n")); rc = OBX_RC_ERR_SOCKET_EOF; break; default: OBXDBGINFO(("iobxInetTransportRecv() socket read, %d bytes read.\n", iread)); OBXDBGMEM(("iobxInetTransportRecv()", cursor, iread)); remainingRead -= iread; (unsigned char *)cursor += iread; /* Treat as bytes */ *actual += iread; break; } } while ( allowShort == FALSE && remainingRead > 0 && rc == OBX_RC_OK ); } else { OBXDBGERR(("[ERROR] iobxInetTransportRecv() bad plist on call.\n")); rc = OBX_RC_ERR_BADPLIST; /* No con block */ } return rc;}/*** Clean up all internals, subsuquent use of this 'connectionid' should result in an error.** Note that the inbound 'connectionid' was created by an open() call.** Passing in a null connectionid is ignored.*/ObxRc iobxInetTransportClose( void **connectionid ) { ObxInetConnectionBlock *conblk = NULL; OBXDBGFLOW(("iobxInetTransportClose() entry, connid=0x%08x\n", *connectionid)); if ( connectionid ) { conblk = *connectionid; if ( conblk ) { if ( conblk->connected ) { sclose( conblk->fd ); } free( conblk ); *connectionid = NULL; } } return OBX_RC_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -