From 49bec1142fca18bd1d9bf65810d19baf5a795664 Mon Sep 17 00:00:00 2001 From: kpl Date: Wed, 18 Mar 2020 14:08:21 -0400 Subject: [PATCH] cwSocket.h/cpp, main.cpp : Updates to socksrv::test(). --- cwSocket.cpp | 64 +++++++++++++++++++++++++++++++--------------------- cwSocket.h | 10 ++++---- main.cpp | 56 ++++++++++++++++++++++++++++++--------------- 3 files changed, 81 insertions(+), 49 deletions(-) diff --git a/cwSocket.cpp b/cwSocket.cpp index 35efcd7..b9c7c34 100644 --- a/cwSocket.cpp +++ b/cwSocket.cpp @@ -415,7 +415,7 @@ 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 failed." ); + return cwLogSysError(kOpFailRC, errno, "Socket connect to %s:%i failed.", cwStringNullGuard(remoteAddr), remotePort ); s->flags = cwSetFlag(s->flags,kIsConnectedFl); @@ -817,7 +817,7 @@ cw::rc_t cw::sock::send( handle_t h, unsigned userId, unsigned connId, const voi return rc; // If this a pre-connected socket ... - if( cwIsFlag(s->flags,kIsConnectedFl) ) + if( cwIsFlag(s->flags,kIsConnectedFl) ) { if( ::send( s->sockH, data, dataByteCnt, 0 ) == cwSOCKET_SYS_ERR ) rc = cwLogSysError(kOpFailRC,errno,"Send failed."); @@ -1094,14 +1094,14 @@ namespace cw return true; } - void _socketCbFunc( 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, struct sockaddr_in* srcAddr ) { rc_t rc; - char buf[ INET_ADDRSTRLEN+1 ]; + char addr[ INET_ADDRSTRLEN+1 ]; - if((rc = sock::addrToString( srcAddr, buf, INET_ADDRSTRLEN )) == kOkRC ) + if((rc = sock::addrToString( srcAddr, addr, INET_ADDRSTRLEN )) == kOkRC ) { - printf("user:%i %s %s\n", userId, (const char*)byteA, buf ); + printf("from user:%i conn:%i at %s : %s ", userId, connId, addr, (const char*)byteA ); } } @@ -1166,24 +1166,30 @@ cw::rc_t cw::socksrv::stop( handle_t h ) return thread::pause( p->thH ); } -cw::rc_t cw::socksrv::test( sock::portNumber_t localPort, const char* remoteAddrIp, sock::portNumber_t remotePort ) +cw::rc_t cw::socksrv::test( sock::portNumber_t localPort, const char* remoteAddrIp, sock::portNumber_t remotePort, unsigned flags ) { - handle_t h; - rc_t rc = kOkRC; - unsigned timeOutMs = 50; - unsigned recvBufByteN = 2048; - unsigned maxSocketN = 10; - unsigned userId = 10; - unsigned sockFlags = sock::kNonBlockingFl; - - const unsigned sbufN = 31; + handle_t h; + rc_t rc = kOkRC; + unsigned timeOutMs = 50; + unsigned recvBufByteN = 2048; + unsigned maxSocketN = 10; + unsigned userId = 10; + unsigned sockFlags = sock::kNonBlockingFl | flags; + bool serverFl = remoteAddrIp == nullptr; + const unsigned sbufN = 31; char sbuf[ sbufN+1 ]; - // create the socket server + if( serverFl ) + printf("Server listening on port: %i\n", localPort ); + else + printf("Client connecting to server %s:%i\n", remoteAddrIp,remotePort); + + + // create the socket manager if((rc = createMgrSrv(h, timeOutMs, recvBufByteN, maxSocketN )) != kOkRC ) return cwLogError(rc,"Socket server create failed."); - // start the socket server + // start the socket manager if((rc = start(h)) != kOkRC ) { cwLogError(rc,"Socker server start failed."); @@ -1191,13 +1197,13 @@ cw::rc_t cw::socksrv::test( sock::portNumber_t localPort, const char* remoteAdd } // create a socket - if((rc = create( mgrHandle(h), userId, localPort, sockFlags, timeOutMs, _socketCbFunc, nullptr, remoteAddrIp, remotePort)) != kOkRC ) + if((rc = create( mgrHandle(h), userId, localPort, sockFlags, timeOutMs, _socketTestCbFunc, nullptr, remoteAddrIp, remotePort)) != kOkRC ) { cwLogError(rc,"Socket server socket create failed."); goto errLabel; } - printf("quit\n"); + printf("'quit' to exit\n"); // readline loop while( true ) @@ -1229,11 +1235,17 @@ cw::rc_t cw::socksrv::test( sock::portNumber_t localPort, const char* remoteAdd return rcSelect(rc,rc1,rc2); } -cw::rc_t cw::socksrv::testServer( sock::portNumber_t localPort ) +cw::rc_t cw::socksrv::testMain( bool tcpFl, sock::portNumber_t localPort, const char* remoteAddrIp, sock::portNumber_t remotePort ) { - return test(localPort,nullptr,sock::kInvalidPortNumber); -} -cw::rc_t cw::socksrv::testClient( sock::portNumber_t localPort, const char* remoteAddrIp, sock::portNumber_t remotePort ) -{ - return test(localPort,remoteAddrIp,remotePort); + unsigned flags = 0; + + if( tcpFl ) + { + flags |= sock::kTcpFl | sock::kStreamFl | sock::kReuseAddrFl | sock::kReusePortFl; + + if( remoteAddrIp == nullptr ) + flags |= sock::kListenFl; + } + + return test(localPort,remoteAddrIp,remotePort,flags); } diff --git a/cwSocket.h b/cwSocket.h index 18b24af..da3739c 100644 --- a/cwSocket.h +++ b/cwSocket.h @@ -17,8 +17,9 @@ namespace cw } cbId_t; - // userId is the id assigned to a socket created with kStreamFl | kListenFl - // connId is an automatically id which is assigned to represent the remote endpoint which is connected to 'userId'. + + // 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 ); enum @@ -131,9 +132,8 @@ namespace cw rc_t start( handle_t h ); rc_t stop( handle_t h ); - rc_t test( sock::portNumber_t localPort, const char* remoteAddrIp, sock::portNumber_t remotePort ); - rc_t testServer( sock::portNumber_t localPort ); - rc_t testClient( sock::portNumber_t localPort, const char* remoteAddrIp, sock::portNumber_t remotePort ); + rc_t test( sock::portNumber_t localPort, const char* remoteAddrIp, sock::portNumber_t remotePort, unsigned flags=0 ); + rc_t testMain( bool tcpFl, sock::portNumber_t localPort, const char* remoteAddrIp=nullptr, sock::portNumber_t remotePort=sock::kInvalidPortNumber ); } } diff --git a/main.cpp b/main.cpp index 9c68517..d4491d8 100644 --- a/main.cpp +++ b/main.cpp @@ -204,31 +204,52 @@ void socketSrvTcpTest( cw::object_t* cfg, int argc, const char* argv[] ) } } -void sockServerTest( cw::object_t* cfg, int argc, const char* argv[] ) +void sockMgrTest( cw::object_t* cfg, int argc, const char* argv[] ) { - if( argc >= 2 ) + bool tcpFl = false; + unsigned short localPort = 0; + const char* remoteIp = nullptr; + unsigned short remotePort = 0; + + if( argc <3 ) { - unsigned short localPort = atoi(argv[1]); - - printf("local port:%i\n", localPort ); - - cw::socksrv::testServer( localPort ); + printf("Invalid argument count."); + printf("Usage: ./cw_rt 'udp | tcp' { }\n"); + goto errLabel; } - -} -void sockClientTest( cw::object_t* cfg, int argc, const char* argv[] ) -{ + if( argc >= 4 ) { - unsigned short localPort = atoi(argv[1]); - const char* remoteIp = argv[2]; - unsigned short remotePort = atoi(argv[3]); + remoteIp = argv[3]; - printf("local:%i to remote:%s %i\n", localPort, remoteIp, remotePort); + if( argc >= 5 ) + remotePort = atoi(argv[4]); - cw::socksrv::testClient(localPort, remoteIp, remotePort ); } + + if( strcmp(argv[1],"tcp")!=0 && strcmp(argv[1],"udp")!=0 ) + { + printf("The first argument must be 'udp' or 'tcp'\n"); + goto errLabel; + } + + tcpFl = strcmp(argv[1],"tcp")==0; + localPort = atoi(argv[2]); + + if( remoteIp != nullptr && remotePort == 0 ) + { + printf("A remote adddress '%s' was given but no remote port was given.", remoteIp); + goto errLabel; + } + + printf("style:%s local:%i to remote:%s %i\n", argv[1], localPort, cwStringNullGuard(remoteIp), remotePort); + + cw::socksrv::testMain( tcpFl, localPort, remoteIp, remotePort ); + + errLabel: + + return; } void socketMdnsTest( cw::object_t* cfg, int argc, const char* argv[] ) @@ -316,8 +337,7 @@ int main( int argc, const char* argv[] ) { "socketTcp", socketTestTcp }, { "socketSrvUdp", socketSrvUdpTest }, { "socketSrvTcp", socketSrvTcpTest }, - { "sockServer", sockServerTest }, - { "sockClient", sockClientTest }, + { "sockMgrTest", sockMgrTest }, { "socketMdns", socketMdnsTest }, { "dnssd", dnsSdTest },