From b416cb5a65ff75843e46de51449a8b02d6933bab Mon Sep 17 00:00:00 2001 From: Kevin Larke Date: Sat, 6 Dec 2014 14:24:48 -0800 Subject: [PATCH] cmUdpPort.h/c : Added cmUdpTest() and cmUdpTestV(). --- cmUdpPort.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ cmUdpPort.h | 6 ++-- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/cmUdpPort.c b/cmUdpPort.c index db84574..b4d7b05 100644 --- a/cmUdpPort.c +++ b/cmUdpPort.c @@ -612,3 +612,91 @@ const cmChar_t* cmUdpHostName( cmUdpH_t h ) p->hnameBuf[HOST_NAME_MAX] = 0; return p->hnameBuf; } + +//======================================================================================= +void cmUdpCallbackTest( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr ) +{ + printf("%s\n",data); +} + + +cmUdpRC_t cmUdpTest( cmCtx_t* ctx, const char* remoteIpAddr, cmUdpPort_t port ) +{ + cmUdpRC_t rc = kOkUdpRC; + cmUdpH_t h = cmUdpNullHandle; + cmErr_t* err = &ctx->err; + unsigned recvBufByteCnt = 4096; + unsigned recvTimeOutMs = 50; + unsigned udpFlags = kNonBlockingUdpFl | kBroadcastUdpFl | kNoQueueUdpFl; + unsigned strN = 1024; + char str[ strN ]; + struct sockaddr_in addr; + + // allocate the UDP port + if((rc = cmUdpAlloc(ctx, &h )) != kOkUdpRC ) + return cmErrMsg(err,rc,"UDP port allocate failed."); + + // initialize the UDP port + if((rc = cmUdpInit( h, port, udpFlags, cmUdpCallbackTest, NULL, NULL, 0, recvBufByteCnt, recvTimeOutMs )) != kOkUdpRC ) + { + rc = cmErrMsg(err,rc,"UPD port initialzation failed."); + goto errLabel; + } + + // convert the IP address to a sockaddr_in + if((rc = cmUdpInitAddr(h, remoteIpAddr, port, &addr )) != kOkUdpRC ) + { + rc = cmErrMsg(err,rc,"IP address conversion failed."); + goto errLabel; + } + + + // start up the UDP port listening thread + if((rc = cmUdpEnableListen(h, true )) != kOkUdpRC ) + { + rc = cmErrMsg(err,rc,"UDP port switch to listen mode failed."); + goto errLabel; + } + + printf("q=quit"); + + while(1) + { + printf("$"); + fflush(stdout); + scanf("%s",str); + + if( strlen(str) > 0 ) + { + if( strcmp(str,"q")==0 ) + break; + + if((rc = cmUdpSendTo(h,str,strlen(str)+1,&addr)) != kOkUdpRC ) + { + rc = cmErrMsg(err,rc,"UDP send failed."); + goto errLabel; + } + } + } + + + errLabel: + if((rc = cmUdpFree(&h)) != kOkUdpRC ) + rc = cmErrMsg(err,rc,"UDP port free failed."); + + return rc; +} + + +cmUdpRC_t cmUdpTestV( cmCtx_t* ctx, unsigned argc, const char* argv[]) +{ + if( argc < 3 ) + return cmErrMsg(&ctx->err,kTestFailUdpRC,"cmUdpTestV usage: argv[1]= argv[2]="); + + const char* ipAddr = argv[1]; + cmUdpPort_t port = atoi(argv[2]); + + cmRptPrintf(&ctx->rpt,"UDP Test Sending To: %s port:%i\n",ipAddr,port); + + return cmUdpTest(ctx,ipAddr,port); +} diff --git a/cmUdpPort.h b/cmUdpPort.h index 827ab44..3a91487 100644 --- a/cmUdpPort.h +++ b/cmUdpPort.h @@ -27,7 +27,8 @@ extern "C" { kBufTooSmallUdpRC, kHostNameFailUdpRC, kInvalidPortNumbUdpRC, - kTimeOutUdpRC + kTimeOutUdpRC, + kTestFailUdpRC }; typedef cmRC_t cmUdpRC_t; @@ -136,7 +137,8 @@ extern "C" { const cmChar_t* cmUdpHostName( cmUdpH_t h ); - + cmUdpRC_t cmUdpTest( cmCtx_t* ctx, const char* remoteIpAddr, cmUdpPort_t port ); + cmUdpRC_t cmUdpTestV( cmCtx_t* ctx, unsigned argc, const char* argv[]); #ifdef __cplusplus }