cwSocket.h/cpp : added callback on connection.

This commit is contained in:
kpl 2020-03-18 15:53:11 -04:00
parent 47bcd57d79
commit fe142ad7a8
3 changed files with 27 additions and 5 deletions

View File

@ -28,11 +28,17 @@
1) Install libwebsockets. 1) Install libwebsockets.
```
sudo dnf install g++ openssl-devel cmake
cd sdk cd sdk
git clone https://libwebsockets.org/repo/libwebsockets git clone https://libwebsockets.org/repo/libwebsockets
cd libwebsockets cd libwebsockets
mkdir build mkdir build
cd build cd build
sudo dnf install openssl-devel cmake
cmake -DCMAKE_INSTALL_PREFIX:PATH=/home/kevin/sdk/libwebsockets/build/out .. cmake -DCMAKE_INSTALL_PREFIX:PATH=/home/kevin/sdk/libwebsockets/build/out ..
```
2) Environment setup:
export LD_LIBRARY_PATH=~/sdk/libwebsockets/build/out/lib

View File

@ -206,6 +206,9 @@ namespace cw
goto errLabel; goto errLabel;
} }
printf("Socket: userId:%i connected.", s->userId);
// initialize the socket record // initialize the socket record
cs = p->sockA + sockIdx; cs = p->sockA + sockIdx;
@ -226,6 +229,10 @@ 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 )
s->cbFunc( s->cbArg, kConnectCbId, s->userId, s->connId, nullptr, 0, (const struct sockaddr_in*)&remoteAddr );
errLabel: errLabel:
if( rc != kOkRC ) if( rc != kOkRC )
close(fd); close(fd);
@ -415,7 +422,16 @@ namespace cw
// ... and connect this socket to the remote address/port // ... and connect this socket to the remote address/port
if( connect(s->sockH, (struct sockaddr*)&addr, sizeof(addr)) == cwSOCKET_SYS_ERR ) if( connect(s->sockH, (struct sockaddr*)&addr, sizeof(addr)) == cwSOCKET_SYS_ERR )
return cwLogSysError(kOpFailRC, errno, "Socket connect to %s:%i failed.", cwStringNullGuard(remoteAddr), remotePort ); {
if( cwIsNotFlag(s->createFlags, kBlockingFl) && errno == EINPROGRESS )
{
// if the socket is non-blocking the connection will complete asynchronously
}
else
{
return cwLogSysError(kOpFailRC, errno, "Socket connect to %s:%i failed.", cwStringNullGuard(remoteAddr), remotePort );
}
}
s->flags = cwSetFlag(s->flags,kIsConnectedFl); s->flags = cwSetFlag(s->flags,kIsConnectedFl);
@ -1094,7 +1110,7 @@ namespace cw
return true; return true;
} }
void _socketTestCbFunc( void* cbArg, sock::cbId_t cbId, unsigned userId, unsigned connId, const void* byteA, unsigned byteN, struct sockaddr_in* srcAddr ) void _socketTestCbFunc( void* cbArg, sock::cbId_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

@ -20,7 +20,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, struct sockaddr_in* srcAddr ); typedef void (*callbackFunc_t)( void* cbArg, cbId_t cbId, unsigned userId, unsigned connId, const void* byteA, unsigned byteN, const struct sockaddr_in* srcAddr );
enum enum
{ {