cwSocket.h/cpp : Changed name of cbId_t to cbOpId_t

This commit is contained in:
kevin.larke 2020-04-01 10:46:31 -04:00
parent 14ee0765db
commit bc3c683e41
4 changed files with 26 additions and 19 deletions

2
cwIo.h
View File

@ -53,7 +53,7 @@ namespace cw
typedef struct socket_msg_str typedef struct socket_msg_str
{ {
sock::cbId_t cbId; sock::cbOpId_t cbId;
unsigned userId; unsigned userId;
unsigned connId; unsigned connId;
const void* byteA; const void* byteA;

View File

@ -138,7 +138,7 @@ namespace cw
s->createFlags = 0; s->createFlags = 0;
s->flags = 0; s->flags = 0;
s->pollfd->events = 0; s->pollfd->events = 0;
s->pollfd->fd = cwSOCKET_NULL_SOCK; s->pollfd->fd = cwSOCKET_NULL_SOCK; // poll() ignores records when fd < 0
s->remoteSockAddr.sin_family = AF_UNSPEC; s->remoteSockAddr.sin_family = AF_UNSPEC;
return rc; return rc;
@ -186,6 +186,12 @@ namespace cw
return kOkRC; return kOkRC;
} }
void _callback( sock_t* s, cbOpId_t opId, const struct sockaddr_in* srcAddr=nullptr, const void* buf=nullptr, unsigned bufByteN=0 )
{
if( s->cbFunc != nullptr )
s->cbFunc( s->cbArg, opId, s->userId, s->connId, buf, bufByteN, srcAddr );
}
rc_t _accept( mgr_t* p, sock_t* s, unsigned sockN ) rc_t _accept( mgr_t* p, sock_t* s, unsigned sockN )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
@ -215,8 +221,6 @@ namespace cw
goto errLabel; goto errLabel;
} }
printf("Socket: userId:%i connected.\n", s->userId);
// initialize the socket record // initialize the socket record
cs = p->sockA + sockIdx; cs = p->sockA + sockIdx;
@ -238,9 +242,7 @@ namespace cw
if((rc = addrToString( (const struct sockaddr_in*)&remoteAddr, cs->ntopBuf, sizeof(cs->ntopBuf) )) != kOkRC ) if((rc = addrToString( (const struct sockaddr_in*)&remoteAddr, cs->ntopBuf, sizeof(cs->ntopBuf) )) != kOkRC )
goto errLabel; goto errLabel;
if( s->cbFunc != nullptr ) _callback( cs, kConnectCbId, (const struct sockaddr_in*)&remoteAddr);
s->cbFunc( s->cbArg, kConnectCbId, s->userId, cs->connId, nullptr, 0, (const struct sockaddr_in*)&remoteAddr );
errLabel: errLabel:
if( rc != kOkRC ) if( rc != kOkRC )
@ -297,7 +299,7 @@ namespace cw
if( fromAddr == nullptr && s->remoteSockAddr.sin_family != AF_UNSPEC) if( fromAddr == nullptr && s->remoteSockAddr.sin_family != AF_UNSPEC)
fromAddr = &s->remoteSockAddr; fromAddr = &s->remoteSockAddr;
s->cbFunc( s->cbArg, kReceiveCbId, s->userId, s->connId, b, bytesReadN, fromAddr ); _callback( s, kReceiveCbId, fromAddr, b, bytesReadN );
} }
} }
@ -314,6 +316,8 @@ namespace cw
cwLogWarning("Socket Disconnected."); cwLogWarning("Socket Disconnected.");
s->flags = cwClrFlag(s->flags,kIsConnectedFl); s->flags = cwClrFlag(s->flags,kIsConnectedFl);
_callback( s, kDisconnectCbId );
} }
return cwLogSysError(kOpFailRC,errno,"recvfrom() failed."); return cwLogSysError(kOpFailRC,errno,"recvfrom() failed.");
@ -358,7 +362,9 @@ namespace cw
// block waiting for data on one of the ports // block waiting for data on one of the ports
if((sysRC = ::poll(pfd,pfdN,timeOutMs)) == 0) if((sysRC = ::poll(pfd,pfdN,timeOutMs)) == 0)
{
rc = kTimeOutRC; rc = kTimeOutRC;
}
else else
{ {
unsigned newSockN = 0; unsigned newSockN = 0;
@ -373,18 +379,19 @@ namespace cw
// if the socket was disconnected or is no longer valid // if the socket was disconnected or is no longer valid
if( cwIsFlag(p->sockA[i].pollfd->revents,POLLHUP) || cwIsFlag(p->sockA[i].pollfd->revents,POLLNVAL) ) if( cwIsFlag(p->sockA[i].pollfd->revents,POLLHUP) || cwIsFlag(p->sockA[i].pollfd->revents,POLLNVAL) )
{ {
printf("Socket userId:%i connId:%i disconnected.\n",p->sockA[i].userId,p->sockA[i].connId); _callback( p->sockA + i, kDisconnectCbId );
_closeSock(p,p->sockA+i); _closeSock(p,p->sockA+i);
continue; continue;
} }
// if an error occured on this socket
if( p->sockA[i].pollfd->revents & POLLERR ) if( p->sockA[i].pollfd->revents & POLLERR )
{ {
printf("ERROR on socket user id:%i conn id:%i\n",p->sockA[i].userId,p->sockA[i].connId); cwLogError(kOpFailRC,"ERROR on socket user id:%i conn id:%i\n",p->sockA[i].userId,p->sockA[i].connId);
// TODO: should the socket be closed? marked as disconnected?
} }
if( p->sockA[i].pollfd->revents & POLLIN ) if( p->sockA[i].pollfd->revents & POLLIN )
{ {
unsigned actualReadN = 0; unsigned actualReadN = 0;
@ -1149,7 +1156,7 @@ namespace cw
} }
// Callback thread used by socksrv::test() below // Callback thread used by socksrv::test() below
void _socketTestCbFunc( void* cbArg, sock::cbId_t cbId, unsigned userId, unsigned connId, const void* byteA, unsigned byteN, const struct sockaddr_in* srcAddr ) void _socketTestCbFunc( void* cbArg, sock::cbOpId_t cbId, unsigned userId, unsigned connId, const void* byteA, unsigned byteN, const struct sockaddr_in* srcAddr )
{ {
rc_t rc; rc_t rc;
char addr[ INET_ADDRSTRLEN+1 ]; char addr[ INET_ADDRSTRLEN+1 ];

View File

@ -11,7 +11,7 @@ namespace cw
// userId is the id assigned to the receiving socket // userId is the id assigned to the receiving socket
// connId is an automatically assigned id which represents the remote endpoint which is connected to 'userId'. // connId is an automatically assigned id which represents the remote endpoint which is connected to 'userId'.
typedef void (*callbackFunc_t)( void* cbArg, cbId_t cbId, unsigned userId, unsigned connId, const void* byteA, unsigned byteN, const struct sockaddr_in* srcAddr ); typedef void (*callbackFunc_t)( void* cbArg, cbOpId_t cbId, unsigned userId, unsigned connId, const void* byteA, unsigned byteN, const struct sockaddr_in* srcAddr );
rc_t createMgr( handle_t& hRef, unsigned recvBufByteN, unsigned maxSocketN ); rc_t createMgr( handle_t& hRef, unsigned recvBufByteN, unsigned maxSocketN );
rc_t destroyMgr( handle_t& hRef ); rc_t destroyMgr( handle_t& hRef );

View File

@ -9,10 +9,10 @@ namespace cw
typedef enum typedef enum
{ {
kReceiveCbId,
kConnectCbId, kConnectCbId,
kDisconnectCbId kReceiveCbId,
} cbId_t; kDisconnectCbId,
} cbOpId_t;