cwEuCon.h/cpp : Added an application thread.

This commit is contained in:
kevin.larke 2020-04-07 17:40:08 -04:00
parent c6ccaefcdb
commit 1fe3e6d841
2 changed files with 687 additions and 622 deletions

View File

@ -295,8 +295,6 @@
namespace cw
{
namespace net
{
namespace eucon
{
@ -606,7 +604,7 @@ namespace cw
const char* name = (const char*)(u+6);
const char* label = "MC Mix - ";
printf("%.*s|%i\n", name[0], name+1, strlen(label) );
printf("%.*s|%li\n", name[0], name+1, strlen(label) );
// if this a 'MC Mix' DNS-SD SRV reply
if( strncmp(label, name+1, strlen(label)) == 0 )
@ -620,12 +618,12 @@ namespace cw
}
}
}
}
}
cw::rc_t cw::net::eucon::create( handle_t& hRef, const args_t& args )
cw::rc_t cw::eucon::create( handle_t& hRef, const args_t& args )
{
rc_t rc = kOkRC;
unsigned udpFlags = sock::kBlockingFl | sock::kReuseAddrFl | sock::kReusePortFl | sock::kMultiCastTtlFl | sock::kMultiCastLoopFl;
@ -675,7 +673,7 @@ cw::rc_t cw::net::eucon::create( handle_t& hRef, const args_t& args )
return rc;
}
cw::rc_t cw::net::eucon::destroy( handle_t& hRef )
cw::rc_t cw::eucon::destroy( handle_t& hRef )
{
rc_t rc = kOkRC;
@ -691,7 +689,7 @@ cw::rc_t cw::net::eucon::destroy( handle_t& hRef )
}
cw::rc_t cw::net::eucon::exec( handle_t h, unsigned sockTimeOutMs )
cw::rc_t cw::eucon::exec( handle_t h, unsigned sockTimeOutMs )
{
rc_t rc = kOkRC;
eucon_t* p = _handleToPtr(h);
@ -717,19 +715,77 @@ cw::rc_t cw::net::eucon::exec( handle_t h, unsigned sockTimeOutMs )
}
bool quitFl = false;
void sighandler(int sig)
namespace cw
{
printf("Signal caught!\n");
quitFl = true;
namespace eucon
{
typedef struct app_str
{
bool quitFl;
handle_t h;
thread::handle_t thH;
unsigned sockTimeOutMs;
} app_t;
bool appThreadFunc( void* arg )
{
app_t* app = static_cast<app_t*>(arg);
exec(app->h,app->sockTimeOutMs);
return true;
}
cw::rc_t cw::net::eucon::test()
rc_t appBegin( const args_t& args, app_t& app )
{
rc_t rc = kOkRC;
handle_t h;
// Create the EuCon controller
if((rc = create( app.h, args )) != kOkRC )
return cwLogError(rc,"Unable to create EuCon server.");
// Create the application thread
if((rc = thread::create( app.thH, appThreadFunc, &app )) != kOkRC )
return cwLogError(rc,"App thread create failed.");
// Start the application thread
if((rc = thread::unpause( app.thH )) != kOkRC )
return cwLogError(rc,"App thread start failed.");
app.sockTimeOutMs = args.sockTimeOutMs;
return rc;
}
rc_t appEnd( app_t& app )
{
rc_t rc = kOkRC;
// Destroy the application thread
if((rc = thread::destroy(app.thH)) != kOkRC )
cwLogError(rc,"EuCon app thread destroy failed.");
// Destroy the EuCon controller
if((rc = destroy(app.h)) != kOkRC )
cwLogError(rc,"Eucon app object destroy failed.");
return rc;
}
}
}
cw::rc_t cw::eucon::test()
{
rc_t rc = kOkRC;
app_t app;
args_t args;
const unsigned sbufN = 31;
char sbuf[ sbufN+1 ];
memset(&args,0,sizeof(app));
args.recvBufByteN = 4096;
args.mdnsIP = "224.0.0.251";
@ -740,17 +796,29 @@ cw::rc_t cw::net::eucon::test()
args.maxSockN = 50;
// Create the EuCon controller
if((rc = create( h, args )) != kOkRC )
return cwLogError(rc,"Unable to create EuCon server.");
if((rc = appBegin(args,app)) != kOkRC )
return 1;
cwLogInfo("EuCon controller waiting ...");
while( !quitFl )
printf("'quit' to exit\n");
// readline loop
while( true )
{
exec(h,args.sockTimeOutMs);
printf("? ");
if( std::fgets(sbuf,sbufN,stdin) == sbuf )
{
printf("Sending:%s",sbuf);
if( strcmp(sbuf,"quit\n") == 0)
break;
}
}
rc = destroy(h);
appEnd(app);
return rc;
}

View File

@ -2,8 +2,6 @@
#define cwEuConHost_h
namespace cw
{
namespace net
{
namespace eucon
{
@ -33,7 +31,6 @@ namespace cw
rc_t test();
}
}
}
#endif