cwSocket.h/cpp, main.cpp : Updates to socksrv::test().
This commit is contained in:
parent
be5ae7e63e
commit
49bec1142f
48
cwSocket.cpp
48
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);
|
||||
|
||||
@ -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,7 +1166,7 @@ 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;
|
||||
@ -1174,16 +1174,22 @@ cw::rc_t cw::socksrv::test( sock::portNumber_t localPort, const char* remoteAdd
|
||||
unsigned recvBufByteN = 2048;
|
||||
unsigned maxSocketN = 10;
|
||||
unsigned userId = 10;
|
||||
unsigned sockFlags = sock::kNonBlockingFl;
|
||||
|
||||
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);
|
||||
}
|
||||
|
10
cwSocket.h
10
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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
54
main.cpp
54
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 <cfg_fn> 'udp | tcp' <localPort> { <remote_ip> <remote_port> }\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 },
|
||||
|
Loading…
Reference in New Issue
Block a user