diff --git a/README.md b/README.md index 1b557ec..e34a4e5 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,17 @@ 1) Install libwebsockets. +``` + sudo dnf install g++ openssl-devel cmake cd sdk git clone https://libwebsockets.org/repo/libwebsockets cd libwebsockets mkdir build cd build - sudo dnf install openssl-devel cmake cmake -DCMAKE_INSTALL_PREFIX:PATH=/home/kevin/sdk/libwebsockets/build/out .. +``` +2) Environment setup: + + export LD_LIBRARY_PATH=~/sdk/libwebsockets/build/out/lib + \ No newline at end of file diff --git a/cwSocket.cpp b/cwSocket.cpp index b9c7c34..847995f 100644 --- a/cwSocket.cpp +++ b/cwSocket.cpp @@ -205,6 +205,9 @@ namespace cw rc = cwLogError(rc,"There are no available slots to 'accept' a new socket connection."); goto errLabel; } + + printf("Socket: userId:%i connected.", s->userId); + // initialize the socket record cs = p->sockA + sockIdx; @@ -226,6 +229,10 @@ namespace cw if((rc = addrToString( (const struct sockaddr_in*)&remoteAddr, cs->ntopBuf, sizeof(cs->ntopBuf) )) != kOkRC ) goto errLabel; + if( s->cbFunc != nullptr ) + s->cbFunc( s->cbArg, kConnectCbId, s->userId, s->connId, nullptr, 0, (const struct sockaddr_in*)&remoteAddr ); + + errLabel: if( rc != kOkRC ) close(fd); @@ -415,8 +422,17 @@ namespace cw // ... and connect this socket to the remote address/port 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); return rc; @@ -1094,7 +1110,7 @@ namespace cw 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; char addr[ INET_ADDRSTRLEN+1 ]; diff --git a/cwSocket.h b/cwSocket.h index da3739c..2bbe92d 100644 --- a/cwSocket.h +++ b/cwSocket.h @@ -20,7 +20,7 @@ namespace cw // 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'. - 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 {