cwTcpSocketTest.h/cpp: Test functions now use cfg. objects rather than hardcoded values.

This commit is contained in:
kevin 2024-11-30 12:19:43 -05:00
parent 703cc29014
commit 83946c1920
2 changed files with 107 additions and 44 deletions

View File

@ -2,6 +2,8 @@
#include "cwLog.h" #include "cwLog.h"
#include "cwCommonImpl.h" #include "cwCommonImpl.h"
#include "cwMem.h" #include "cwMem.h"
#include "cwTest.h"
#include "cwObject.h"
#include "cwThread.h" #include "cwThread.h"
@ -41,13 +43,13 @@ namespace cw
if((rc = receive( app->sockH, buf, app->recvBufByteN, &recvBufByteN, &fromAddr )) == kOkRC ) if((rc = receive( app->sockH, buf, app->recvBufByteN, &recvBufByteN, &fromAddr )) == kOkRC )
{ {
addrToString( &fromAddr, addrBuf ); addrToString( &fromAddr, addrBuf );
printf("%i %s from %s\n", recvBufByteN, buf, addrBuf ); cwLogPrint("%i %s from %s\n", recvBufByteN, buf, addrBuf );
} }
app->cbN += 1; app->cbN += 1;
if( app->cbN % 10 == 0) if( app->cbN % 10 == 0)
{ {
printf("."); cwLogPrint(".");
fflush(stdout); fflush(stdout);
} }
@ -67,7 +69,7 @@ namespace cw
{ {
if((rc = accept( app->sockH )) == kOkRC ) if((rc = accept( app->sockH )) == kOkRC )
{ {
printf("Server connected.\n"); cwLogPrint("Server connected.\n");
} }
} }
else else
@ -82,11 +84,11 @@ namespace cw
// if the server disconnects then recvBufByteN // if the server disconnects then recvBufByteN
if( !isConnected( app->sockH) ) if( !isConnected( app->sockH) )
{ {
printf("Disconnected."); cwLogPrint("Disconnected.");
} }
else else
{ {
printf("%i %s\n", recvBufByteN, buf ); cwLogPrint("%i %s\n", recvBufByteN, buf );
} }
} }
} }
@ -96,7 +98,7 @@ namespace cw
if( app->cbN % 10 == 0) if( app->cbN % 10 == 0)
{ {
// print '+' when the server is not connected. // print '+' when the server is not connected.
printf("%s", isConnected(app->sockH) == false ? "+" : "."); cwLogPrint("%s", isConnected(app->sockH) == false ? "+" : ".");
fflush(stdout); fflush(stdout);
} }
@ -106,16 +108,27 @@ namespace cw
} }
} }
cw::rc_t cw::net::socket::test( portNumber_t localPort, const char* remoteAddr, portNumber_t remotePort ) cw::rc_t cw::net::socket::test_udp( const object_t* cfg )
{ {
rc_t rc; rc_t rc = kOkRC;
unsigned timeOutMs = 100; unsigned timeOutMs = 100;
const unsigned sbufN = 31; const char* remoteAddr = "12.0.0.1";
portNumber_t remotePort = 5687;
portNumber_t localPort = 5688;
const unsigned sbufN = 31;
char sbuf[ sbufN+1 ]; char sbuf[ sbufN+1 ];
app_t app; app_t app;
app.cbN = 0; app.cbN = 0;
app.recvBufByteN = sbufN+1; app.recvBufByteN = sbufN+1;
if((rc = cfg->getv("localPort",localPort,
"remoteAddr",remoteAddr,
"remotePort",remotePort)) != kOkRC )
{
cwLogError(rc,"Arg. parse failed.");
goto errLabel;
}
if((rc = create(app.sockH,localPort, kBlockingFl,timeOutMs, NULL, kInvalidPortNumber )) != kOkRC ) if((rc = create(app.sockH,localPort, kBlockingFl,timeOutMs, NULL, kInvalidPortNumber )) != kOkRC )
return rc; return rc;
@ -126,12 +139,14 @@ cw::rc_t cw::net::socket::test( portNumber_t localPort, const char* remoteAddr,
if((rc = thread::unpause( app.threadH )) != kOkRC ) if((rc = thread::unpause( app.threadH )) != kOkRC )
goto errLabel; goto errLabel;
cwLogPrint("Type a message to send or 'quit' to exit.\n");
while( true ) while( true )
{ {
printf("? "); cwLogPrint("? ");
if( std::fgets(sbuf,sbufN,stdin) == sbuf ) if( std::fgets(sbuf,sbufN,stdin) == sbuf )
{ {
printf("Sending:%s",sbuf); cwLogPrint("Sending:%s",sbuf);
send(app.sockH, sbuf, strlen(sbuf)+1, remoteAddr, remotePort ); send(app.sockH, sbuf, strlen(sbuf)+1, remoteAddr, remotePort );
if( strcmp(sbuf,"quit\n") == 0) if( strcmp(sbuf,"quit\n") == 0)
@ -148,16 +163,21 @@ cw::rc_t cw::net::socket::test( portNumber_t localPort, const char* remoteAddr,
} }
cw::rc_t cw::net::socket::test_tcp( portNumber_t localPort, const char* remoteAddr, portNumber_t remotePort, bool dgramFl, bool serverFl ) cw::rc_t cw::net::socket::test_tcp( const object_t* cfg )
{ {
rc_t rc; rc_t rc = kOkRC;
unsigned timeOutMs = 100; unsigned timeOutMs = 100;
const unsigned sbufN = 31; const unsigned sbufN = 31;
char sbuf[ sbufN+1 ]; char sbuf[ sbufN+1 ];
app_t app; app_t app;
bool serverFl = false;
bool dgramFl = true;
bool streamFl = !dgramFl; bool streamFl = !dgramFl;
bool clientFl = !serverFl; bool clientFl = !serverFl;
unsigned flags = kTcpFl | kBlockingFl; unsigned flags = kTcpFl | kBlockingFl;
portNumber_t localPort = 5687;
const char* remoteAddr = "127.0.0.1";
portNumber_t remotePort = 5688;
app.remoteAddr = remoteAddr; app.remoteAddr = remoteAddr;
app.remotePort = remotePort; app.remotePort = remotePort;
@ -165,6 +185,22 @@ cw::rc_t cw::net::socket::test_tcp( portNumber_t localPort, const char* remoteAd
app.recvBufByteN = sbufN+1; app.recvBufByteN = sbufN+1;
app.serverFl = serverFl; app.serverFl = serverFl;
if((rc = cfg->getv("localPort",localPort,
"remoteAddr",remoteAddr,
"remotePort",remotePort,
"serverFl",serverFl,
"dgramFl",dgramFl,
"timeOutMs",timeOutMs )) != kOkRC )
{
rc = cwLogError(rc,"Arg. parse failed.");
goto errLabel;
}
streamFl = !dgramFl;
clientFl = !serverFl;
if( serverFl && streamFl ) if( serverFl && streamFl )
flags |= kListenFl; flags |= kListenFl;
@ -187,8 +223,9 @@ cw::rc_t cw::net::socket::test_tcp( portNumber_t localPort, const char* remoteAd
goto errLabel; goto errLabel;
} }
printf("Starting node ....\n"); cwLogPrint("Starting %s %s node ....\n",streamFl ? "TCP" : "UDP", serverFl ? "server" : "client");
cwLogPrint("'quit'=quit\n");
// start the thread // start the thread
if((rc = thread::unpause( app.threadH )) != kOkRC ) if((rc = thread::unpause( app.threadH )) != kOkRC )
goto errLabel; goto errLabel;
@ -196,7 +233,7 @@ cw::rc_t cw::net::socket::test_tcp( portNumber_t localPort, const char* remoteAd
while( true ) while( true )
{ {
printf("? "); cwLogPrint("? ");
if( std::fgets(sbuf,sbufN,stdin) == sbuf ) if( std::fgets(sbuf,sbufN,stdin) == sbuf )
{ {
if( strcmp(sbuf,"quit\n") == 0) if( strcmp(sbuf,"quit\n") == 0)
@ -205,13 +242,13 @@ cw::rc_t cw::net::socket::test_tcp( portNumber_t localPort, const char* remoteAd
if( streamFl ) if( streamFl )
{ {
// when using streams no remote address is necessary // when using streams no remote address is necessary
printf("Sending:%s",sbuf); cwLogPrint("Sending:%s",sbuf);
send(app.sockH, sbuf, strlen(sbuf)+1 ); send(app.sockH, sbuf, strlen(sbuf)+1 );
} }
else else
{ {
// when using dgrams the dest. address is required // when using dgrams the dest. address is required
printf("Sending:%s",sbuf); cwLogPrint("Sending:%s",sbuf);
send(app.sockH, sbuf, strlen(sbuf)+1, remoteAddr, remotePort); send(app.sockH, sbuf, strlen(sbuf)+1, remoteAddr, remotePort);
} }
} }
@ -249,7 +286,7 @@ namespace cw
char addrBuf[ INET_ADDRSTRLEN ]; char addrBuf[ INET_ADDRSTRLEN ];
socket::addrToString( fromAddr, addrBuf, INET_ADDRSTRLEN ); socket::addrToString( fromAddr, addrBuf, INET_ADDRSTRLEN );
p->cbN += 1; p->cbN += 1;
printf("%i %s %s\n", p->cbN, addrBuf, (const char*)data ); cwLogPrint("%i %s %s\n", p->cbN, addrBuf, (const char*)data );
} }
@ -257,15 +294,27 @@ namespace cw
} }
} }
cw::rc_t cw::net::srv::test_udp_srv( socket::portNumber_t localPort, const char* remoteAddr, socket::portNumber_t remotePort ) cw::rc_t cw::net::srv::test_udp_srv( const object_t* cfg )
{ {
rc_t rc; rc_t rc = kOkRC;
unsigned recvBufByteCnt = 1024; unsigned recvBufByteCnt = 1024;
unsigned timeOutMs = 100; unsigned timeOutMs = 100;
const unsigned sbufN = 31; socket::portNumber_t localPort = 5687;
char sbuf[ sbufN+1 ]; const char* remoteAddr = nullptr;
app_t app; socket::portNumber_t remotePort = 5688;
const unsigned sbufN = 31;
char sbuf[ sbufN+1 ];
app_t app;
app.cbN = 0; app.cbN = 0;
if((rc = cfg->getv("localPort",localPort,
"remoteAddr",remoteAddr,
"remotePort",remotePort)) != kOkRC )
{
rc = cwLogError(rc,"Arg. parse failed.");
goto errLabel;
}
if((rc = srv::create(app.srvH, if((rc = srv::create(app.srvH,
localPort, localPort,
@ -292,10 +341,10 @@ cw::rc_t cw::net::srv::test_udp_srv( socket::portNumber_t localPort, const char*
while( true ) while( true )
{ {
printf("? "); cwLogPrint("? ");
if( std::fgets(sbuf,sbufN,stdin) == sbuf ) if( std::fgets(sbuf,sbufN,stdin) == sbuf )
{ {
printf("Sending:%s",sbuf); cwLogPrint("Sending:%s",sbuf);
send(app.srvH, sbuf, strlen(sbuf)+1, remoteAddr, remotePort ); send(app.srvH, sbuf, strlen(sbuf)+1, remoteAddr, remotePort );
if( strcmp(sbuf,"quit\n") == 0) if( strcmp(sbuf,"quit\n") == 0)
@ -309,15 +358,28 @@ cw::rc_t cw::net::srv::test_udp_srv( socket::portNumber_t localPort, const char*
return rcSelect(rc,rc0); return rcSelect(rc,rc0);
} }
cw::rc_t cw::net::srv::test_tcp_srv( socket::portNumber_t localPort, const char* remoteAddr, socket::portNumber_t remotePort ) cw::rc_t cw::net::srv::test_tcp_srv( const object_t* cfg )
{ {
rc_t rc; rc_t rc = kOkRC;
unsigned recvBufByteCnt = 1024; unsigned recvBufByteCnt = 1024;
unsigned timeOutMs = 100; unsigned timeOutMs = 100;
const unsigned sbufN = 31; socket::portNumber_t localPort = 5687;
char sbuf[ sbufN+1 ]; const char* remoteAddr = nullptr;
app_t app; socket::portNumber_t remotePort = 5688;
const unsigned sbufN = 31;
char sbuf[ sbufN+1 ];
app_t app;
app.cbN = 0; app.cbN = 0;
if((rc = cfg->getv("localPort",localPort,
"remoteAddr",remoteAddr,
"remotePort",remotePort)) != kOkRC )
{
rc = cwLogError(rc,"Arg. parse failed.");
goto errLabel;
}
if((rc = srv::create(app.srvH, if((rc = srv::create(app.srvH,
localPort, localPort,
@ -329,18 +391,19 @@ cw::rc_t cw::net::srv::test_tcp_srv( socket::portNumber_t localPort, const char*
timeOutMs, timeOutMs,
remoteAddr, remoteAddr,
remotePort )) != kOkRC ) remotePort )) != kOkRC )
{
return rc; return rc;
}
if((rc = srv::start( app.srvH )) != kOkRC ) if((rc = srv::start( app.srvH )) != kOkRC )
goto errLabel; goto errLabel;
while( true ) while( true )
{ {
printf("? "); cwLogPrint("? ");
if( std::fgets(sbuf,sbufN,stdin) == sbuf ) if( std::fgets(sbuf,sbufN,stdin) == sbuf )
{ {
printf("Sending:%s",sbuf); cwLogPrint("Sending:%s",sbuf);
send(app.srvH, sbuf, strlen(sbuf)+1 ); send(app.srvH, sbuf, strlen(sbuf)+1 );
if( strcmp(sbuf,"quit\n") == 0) if( strcmp(sbuf,"quit\n") == 0)

View File

@ -7,14 +7,14 @@ namespace cw
{ {
namespace socket namespace socket
{ {
rc_t test( portNumber_t localPort, const char* remoteAddr, portNumber_t remotePort ); rc_t test_udp( const object_t* cfg );
rc_t test_tcp( portNumber_t localPort, const char* remoteAddr, portNumber_t remotePort, bool dgramFl, bool serverFl ); rc_t test_tcp( const object_t* cfg );
} }
namespace srv namespace srv
{ {
rc_t test_udp_srv( socket::portNumber_t localPort, const char* remoteAddr, socket::portNumber_t remotePort ); rc_t test_udp_srv( const object_t* cfg );
rc_t test_tcp_srv( socket::portNumber_t localPort, const char* remoteAddr, socket::portNumber_t remotePort ); rc_t test_tcp_srv( const object_t* cfg );
rc_t mdns_test(); rc_t mdns_test();
} }
} }