diff --git a/cwFileSys.cpp b/cwFileSys.cpp index f647aff..6468f47 100644 --- a/cwFileSys.cpp +++ b/cwFileSys.cpp @@ -273,13 +273,12 @@ char* cw::filesys::expandPath( const char* dir ) cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr ) { unsigned n = 0; // char's in pathStr - unsigned dn = 0; // char's in the dir part - unsigned fn = 0; // char's in the name part - unsigned en = 0; // char's in the ext part - char* cp = nullptr; - pathPart_t* rp = nullptr; - - + unsigned dn = 0; // char's in the dir part + unsigned fn = 0; // char's in the name part + unsigned en = 0; // char's in the ext part + char* cp = nullptr; + pathPart_t* rp = nullptr; + if( pathStr==nullptr ) return nullptr; @@ -301,7 +300,7 @@ cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr ) if( n == 0 ) return nullptr; - + char buf[n+1]; buf[n] = 0; @@ -352,13 +351,13 @@ cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr ) n = sizeof(pathPart_t) + dn + fn + en + 3; // alloc memory - if((rp = mem::allocZ( n )) == nullptr ) + if((rp = (pathPart_t*)mem::allocZ( n )) == nullptr ) { cwLogError( kMemAllocFailRC, "Unable to allocate the file system path part record for '%s'.",pathStr); - return nullptr; + goto errLabel; } - // set the return pointers for ecmh of the parts + // set the return pointers for each of the parts rp->dirStr = (const char* )(rp + 1); rp->fnStr = rp->dirStr + dn + 1; rp->extStr = rp->fnStr + fn + 1; @@ -375,7 +374,7 @@ cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr ) // Get the trailing word again. // pathStr must be copied into a buf because basename() may // is allowed to change the values in its arg. - strncpy(buf,pathStr,n); + strcpy(buf,pathStr); cp = basename(buf); @@ -405,6 +404,7 @@ cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr ) if( en == 0 ) rp->extStr = nullptr; + errLabel: return rp; } diff --git a/cwMem.cpp b/cwMem.cpp index c782313..a501fbf 100644 --- a/cwMem.cpp +++ b/cwMem.cpp @@ -6,17 +6,17 @@ void* cw::mem::_alloc( void* p0, unsigned n, unsigned flags ) { - void* p = nullptr; // ptr to new block - unsigned p0N = 0; // size of existing block + void* p = nullptr; // ptr to new block + unsigned p0N = 0; // size of existing block unsigned* p0_1 = nullptr; // pointer to base of existing block - n += sizeof(unsigned); // add space for the size of the block + n += 2*sizeof(unsigned); // add space for the size of the block // if there is no existing block if( p0 != nullptr ) { // get a pointer to the base of the exsting block - p0_1 = ((unsigned*)p0) - 1; + p0_1 = ((unsigned*)p0) - 2; p0N = p0_1[0]; // get size of existing block @@ -49,7 +49,13 @@ void* cw::mem::_alloc( void* p0, unsigned n, unsigned flags ) p1[0] = n; // set size of new block // advance past the block size and return - return p1+1; + return p1+2; + + /* + n += 8; + char* p = (char*)calloc(1,n); + return p+8; + */ } @@ -96,6 +102,6 @@ void cw::mem::free( void* p ) { if( p != nullptr) { - ::free(static_cast(p)-1); + ::free(static_cast(p)-2); } } diff --git a/cwSpScBuf.cpp b/cwSpScBuf.cpp index ddd007b..a7a0992 100644 --- a/cwSpScBuf.cpp +++ b/cwSpScBuf.cpp @@ -12,10 +12,10 @@ namespace cw { typedef struct spsc_buf_str { - uint8_t* buf; - unsigned bufByteN; std::atomic w; // write ptr std::atomic r; // read ptr + uint8_t* buf; + unsigned bufByteN; } spsc_buf_t; // Note: r==w indicates an empty buffer. @@ -57,13 +57,13 @@ cw::rc_t cw::spsc_buf::create( handle_t& hRef, unsigned bufByteN ) return rc; spsc_buf_t* p = mem::allocZ(); + p->buf = mem::allocZ(bufByteN); p->bufByteN = bufByteN; - p->w = p->buf; - p->r = p->buf; - + p->w.store(p->buf); + p->r.store(p->buf); hRef.set(p); - + return rc; } @@ -387,13 +387,15 @@ cw::rc_t cw::spsc_buf::test() ctxArray[0].share = &share; ctxArray[1].id = 1; ctxArray[1].share = &share; - + share.readyFl.store(false,std::memory_order_release); // create the SPSC buffer if((rc = create( share.h, bufByteN )) != kOkRC ) + { return cwLogError(rc,"spsc_buf create failed."); - + } + // create the thread machine if((rc = thread_mach::create( h, _threadFunc, ctxArray, sizeof(ctx_t), ctxArrayN )) != kOkRC ) { @@ -407,9 +409,9 @@ cw::rc_t cw::spsc_buf::test() cwLogError(rc,"Thread machine start failed."); goto errLabel; } - + sleepMs(5000); - + errLabel: if((rc0 = thread_mach::destroy(h)) != kOkRC ) cwLogError(rc0,"Thread machine destroy failed."); diff --git a/cwSpScQueueTmpl.cpp b/cwSpScQueueTmpl.cpp index b30e279..c11b2df 100644 --- a/cwSpScQueueTmpl.cpp +++ b/cwSpScQueueTmpl.cpp @@ -163,6 +163,7 @@ cw::rc_t cw::testSpScQueueTmpl() goto errLabel; } + cwLogInfo("running for 1 minute ..."); sleepMs(60 * 1000); errLabel: diff --git a/cwUi.cpp b/cwUi.cpp index 369a416..af383f7 100644 --- a/cwUi.cpp +++ b/cwUi.cpp @@ -3,11 +3,11 @@ #include "cwCommonImpl.h" #include "cwMem.h" #include "cwThread.h" +#include "cwObject.h" #include "cwWebSock.h" #include "cwWebSockSvr.h" #include "cwText.h" #include "cwNumericConvert.h" -#include "cwObject.h" #include "cwUi.h" diff --git a/cwWebSockSvr.cpp b/cwWebSockSvr.cpp index 46084dd..98791fb 100644 --- a/cwWebSockSvr.cpp +++ b/cwWebSockSvr.cpp @@ -4,8 +4,10 @@ #include "cwMem.h" #include "cwWebSock.h" #include "cwThread.h" +#include "cwObject.h" #include "cwWebSockSvr.h" #include "cwText.h" +#include "cwFileSys.h" namespace cw { @@ -71,6 +73,8 @@ cw::rc_t cw::websockSrv::create( p->_timeOutMs = timeOutMs; + cwLogInfo("Listening on port:%i root dir:%s default page:%s\n", port, cwStringNullGuard(physRootDir), cwStringNullGuard(dfltHtmlPageFn)); + h.set(p); errLabel: @@ -162,12 +166,12 @@ namespace cw } -cw::rc_t cw::websockSrvTest() +cw::rc_t cw::websockSrvTest( const object_t* cfg ) { rc_t rc; websockSrv::handle_t h; - const char* physRootDir = "/home/kevin/src/cwtest/src/libcw/html/websockSrvTest"; - const char* dfltHtmlPageFn = "test_websocket.html"; + const char* physRootDirArg = nullptr; //"/home/kevin/src/cwtest/src/libcw/html/websockSrvTest"; + const char* dfltHtmlPageFn = nullptr; //"test_websocket.html"; unsigned timeOutMs = 50; int port = 5687; unsigned rcvBufByteN = 128; @@ -186,6 +190,11 @@ cw::rc_t cw::websockSrvTest() { "websocksrv_test_protocol",kWebsockSrvProtocolId,rcvBufByteN,xmtBufByteN} }; + + if((rc = cfg->getv("physRootDir",physRootDirArg,"dfltHtmlPageFn",dfltHtmlPageFn)) != kOkRC ) + return cwLogError(rc,"Args parse failed."); + + char* physRootDir = cw::filesys::expandPath(physRootDirArg); unsigned protocolN = sizeof(protocolA)/sizeof(protocolA[0]); diff --git a/cwWebSockSvr.h b/cwWebSockSvr.h index 96cbd24..fdab325 100644 --- a/cwWebSockSvr.h +++ b/cwWebSockSvr.h @@ -31,7 +31,7 @@ namespace cw { } - rc_t websockSrvTest(); + rc_t websockSrvTest( const object_t* cfg ); } #endif