56 #include <sys/select.h> 61 #include "rnr/rnrconfig.h" 63 #include "rnr/dlistvoid.h" 64 #include "rnr/dliststr.h" 66 #include "rnr/sockset.h" 67 #include "rnr/netmsgs.h" 94 #define BSPROXY_TUNE_T_RECV 1000000 95 #define BSPROXY_TUNE_T_SEND 500000
96 #define BSPROXY_TUNE_T_SELECT 5000000
103 #define BSPROXY_FD_SETSIZE 256 105 #define BSPROXY_FD_SETSIZE FD_SETSIZE 274 #define BSPROXY_LOG_WARN(hndClient, ecode, wfmt, ...) \ 275 LOGDIAG2("Warning: %s: %s: %s(ecode=%d): " wfmt, \ 276 ServerHasName(), ClientHasName(hndClient), \ 277 bsStrError(ecode), ((ecode)>=0? (ecode): -(ecode)), \ 288 #define BSPROXY_LOG_ERROR(hndClient, ecode, efmt, ...) \ 289 LOGERROR("%s: %s: %s(ecode=%d): " efmt, \ 290 ServerHasName(), ClientHasName(hndClient), \ 291 bsStrError(ecode), (ecode>=0? ecode: -ecode), \ 302 #define BSPROXY_LOG_NMERROR(hndClient, nmecode, efmt, ...) \ 303 BSPROXY_LOG_ERROR(hndClient, BS_ECODE_BAD_MSG, "%s(nmecode=%d): " efmt, \ 304 nmStrError(nmecode), (nmecode>=0? nmecode: -nmecode), \ 314 #define BSPROXY_LOG_SYSERROR(hndClient, efmt, ...) \ 315 LOGERROR("%s: %s: %s(ecode=%d): %s(errno=%d)" efmt, \ 316 ServerHasName(), ClientHasName(hndClient), \ 317 bsStrError(BS_ECODE_SYS), BS_ECODE_SYS, \ 318 strerror(errno), errno, ##__VA_ARGS__) 330 #define BSPROXY_SEND_ERROR_RSP(hndClient, hndVConn, uTid, ecode, efmt, ...) \ 333 BSPROXY_LOG_ERROR(hndClient, ecode, "req_vconn=%u: " efmt, \ 334 hndVConn, ##__VA_ARGS__); \ 335 ClientSendErrorRsp(hndClient, uTid, ecode, efmt, ##__VA_ARGS__); \ 348 #define BSPROXY_SEND_NMERROR_RSP(hndClient, hndVConn, uTid, nmecode, efmt, ...) \ 351 BSPROXY_LOG_NMERROR(hndClient, nmecode, "req_vconn=%u: " efmt, \ 352 hndVConn, ##__VA_ARGS__); \ 353 ClientSendErrorRsp(hndClient, uTid, BS_ECODE_BAD_MSG, efmt, \ 366 #define BSPROXY_SEND_SYSERROR_RSP(hndClient, hndVConn, uTid, efmt, ...) \ 369 BSPROXY_LOG_SYSERROR(hndClient, "req_vconn=%u: " efmt, \ 370 hndVConn, ##__VA_ARGS__); \ 371 ClientSendErrorRsp(hndClient, uTid, BS_ECODE_SYS, \ 372 "%s(errno=%d): vconn=%u:" efmt, \ 373 strerror(errno), errno, hndVConn, ##__VA_ARGS__); \ 384 #define BSPROXY_LOG_REQ(hndClient, pMsgHdr) \ 387 if( LOGABLE(LOG_LEVEL_DIAG2) ) \ 389 LOGDIAG2("%s: %s:", ServerHasName(), ClientHasName(hndClient)); \ 390 BsProxyLogMsgHdr("Rx Req", pMsgHdr); \ 394 #define BSPROXY_LOG_REQ(hndClient, pMsgHdr) 404 #define BSPROXY_LOG_RSP(hndClient, pMsgHdr) \ 407 if( LOGABLE(LOG_LEVEL_DIAG2) ) \ 409 LOGDIAG2("%s: %s:", ServerHasName(), ClientHasName(hndClient)); \ 410 BsProxyLogMsgHdr("Tx Rsp", pMsgHdr); \ 414 #define BSPROXY_LOG_RSP(hndClient, pMsgHdr) 428 #define BSPROXY_CHK_VCONN_HND(hndVConn) \ 429 (((int)(hndVConn) >= BSPROXY_VCONN_MIN) && \ 430 ((int)(hndVConn) <= BSPROXY_VCONN_MAX)) 441 #define BSPROXY_CHK_MOD_VCONN_HND(hndVConn) \ 442 (((int)(hndVConn) >= BSPROXY_VCONN_MOD_MIN) && \ 443 ((int)(hndVConn) <= BSPROXY_VCONN_MOD_MAX)) 458 if( gettimeofday(pTvMark, NULL) != OK )
460 LOGSYSERROR(
"gettimeofday()");
497 return SocketAttrGetLocalName(
ServerGet()->m_pServerSockListener);
523 return (
int)hndClient;
622 if( pClient != NULL )
696 const char *sDevName,
697 const char *sModName,
int ClientRecvReq(BsProxyClientHnd_T hndClient, BsProxyMsgHdr_T *pMsgHdr, byte_t **addrBuf)
Receive a request message from client.
uint_t BsMsgId_T
client message id type [0-64k].
struct _bsproxy_th_req * BsProxyThReq_P
service thread request
int ClientSendErrorRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid, int nECode, const char *sErrFmt,...)
Send an error response to the client.
BsProxyClientState_T m_eClientState
client state
Socket_T * m_pServerSockListener
server listener socket
BsModTraceFunc_T * BsModTraceFunc_P
pointer to msg trace func.
BsModExitFunc_P m_fnModExit
module exit
BsProxyModIF_T * ModGetLoadedIF(const char *sModUri)
Get the defined interface from a previously loaded i/f module.
BsProxyClientCtl_T * ClientAcquire(BsProxyClientHnd_T hndClient)
Acquire client, locking it from other threads.
int VConnOpenDev(BsProxyClientHnd_T hndClient, const char *sDevName, const char *sModName, byte_t argbuf[], size_t uArgLen, bool_t bTrace)
Open a virtual device connection.
int m_rd
module resource descriptor
BotSense client application - bsProxy server-terminated core messages.
uint_t m_uRefCnt
vconn reference count for this thread
BsProxyThCtl_T * ThCreateServerThread()
Create the special server service thread.
void VConnRelease(BsVConnHnd_T hndVConn)
Release the locked virtual client.
BsModRequestFunc_T * BsModRequestFunc_P
pointer to request func.
BsModCloseFunc_T * BsModCloseFunc_P
pointer to close function
BsProxyClientCtl_T * m_pServerRegClient[256]
registered clients
char * ThNewDevUri(const char *sDevName)
Convert the device name to a quasi Uniform Resource Id.
byte_t * m_bufReq
packed request message buffer
uint_t m_uServerRegClientCount
num of reg. clients
uint_t BsTid_T
client transaction id type [0-255].
Socket_T * m_pClientSock
client opened TCP socket
BsProxyThCtl_T * ThCreateDevThread(const char *sDevUri)
Create a device service thread.
BsModInfoFunc_T * BsModInfoFunc_P
pointer to mod info function
BsProxyThCtl_T * m_pThCtl
service thread
void VConnDelete(BsProxyVConn_T *pVConn)
Delete allocated virtual connection entry.
uint_t timer_elapsed(struct timeval *pTvMark)
Calculate the elapsed time between the given time mark and this call.
void ClientRelease(BsProxyClientHnd_T hndClient)
Release the locked client.
INLINE_IN_H int ServerHasPort()
Get the <b><i>BotSense</i></b> server's listening port.
bool_t m_bBusy
client is [not] busy
BsModExitFunc_T * BsModExitFunc_P
pointer to exit function
#define BSPROXY_FD_SETSIZE
server socket set size
int VConnOpenServer()
Open special server virtual connection.
INLINE_IN_H const char * ServerHasName()
Get the <b><i>BotSense</i></b> server's official name.
INLINE_IN_H BsProxyServerCtl_T * ServerGet()
Get the <b><i>BotSense</i></b> server.
pthread_t m_thread
the service thread
bool_t m_bServerTrace
do [not] trace server messages
BsModCloseFunc_P m_fnModClose
device close
struct _bsproxy_th_ctl BsProxyThCtl_T
int BsProxyClientHnd_T
bsProxy server client handle
size_t m_uReqLen
request buffer length
char * ModNewModUri(const char *sModName)
Convert the module name to a quasi Uniform Resource Id.
int VConnClose(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn)
Close the virtual connection.
INLINE_IN_H BsProxyClientHnd_T ServerClientSd2Hnd(int sd)
Convert the <b><i>BotSense</i></b> server client socket descriptor to client handle.
void VConnOneTimeInit()
The bsProxy virtual connections one-time initialization.
INLINE_IN_H int ServerClientHnd2Sd(BsProxyClientHnd_T hndClient)
Convert the <b><i>BotSense</i></b> server client handle to client socket descriptor.
BsProxyClientHnd_T m_hndClient
proxied client handle
BsModInfoFunc_P m_fnModInfo
static info
int ClientSendOkRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid)
Send an ok response to the client.
const char * m_sServerName
server's name
pthread_mutex_t m_mutexSync
synchronization mutex used by condition
void ThDestroyThread(BsProxyThCtl_T *pThCtl)
Destroy service thread.
void BsProxyLogMsgHdr(const char *sPreface, BsProxyMsgHdr_T *pMsgHdr)
Log message header.
<b><i>BotSense</i></b> client library declarations.
pthread_mutex_t m_mutex
server mutex lock
pthread_cond_t m_condSync
synchronization condition
BsModOpenFunc_T * BsModOpenFunc_P
pointer to open function
INLINE_IN_H bool_t ClientGetTraceState(BsProxyClientHnd_T hndClient)
Get the <b><i>BotSense</i></b> client's server-terminated message trace state.
BsProxyVConn_T * VConnAcquire(BsVConnHnd_T hndVConn)
Acquire virtual connection, locking it from other threads.
disconnected or fatal, not deleted
SockSet_T * m_pServerSockSet
server socket set
DListVoid_T * m_queue
thread request queue
BsProxyClientHnd_T m_hndClient
proxied client handle
BsProxyModIF_T * ModLoad(const char *sModUri)
Load the interface module.
BsModTraceFunc_P m_fnModTrace
tracing
INLINE_IN_H BsProxyClientCtl_T * ServerGetClient(BsProxyClientHnd_T hndClient)
Get the <b><i>BotSense</i></b> server client.
uint_t m_uRefCnt
vconn ref count for this client
BsProxyThState_T
Service thread states.
thread is not fully initialized
BsProxyVConn_T * VConnGet(BsVConnHnd_T hndVConn)
Get the virtual connection entry associated with the handle.
void ClientSetState(BsProxyClientCtl_T *pClient, BsProxyClientState_T eNewState)
Set client's state.
BsProxyThReq_T * ThDequeue(BsProxyThCtl_T *pThCtl)
Dequeue a request for the given service thread.
uint_t m_uRefCnt
vconn reference count for this module
const char * m_sModUri
module Uniform Resource Id
void ClientOneTimeInit()
The <b><i>BotSense</i></b> bsProxy server one-time client control initialization.
BsVConnHnd_T m_hndVConn
virtual connection handle
BotSense Proxy Message Header Structure.
BsVConnHnd_T m_hndVConn
self reference
bool_t m_bBusy
virtual connection is [not] busy
thread is running (nominal state)
<b><i>BotSense</i></b> bsProxy Dynamically Linked Library module interface.
struct _bsproxy_th_req BsProxyThReq_T
INLINE_IN_H const char * ServerHasAddr()
Get the <b><i>BotSense</i></b> server's listening address.
BsModInitFunc_P m_fnModInit
module init
BsProxyThReq_T * ThReqNew(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufReq[], size_t uReqLen)
Allocate and initalized a new service thread request.
int(* m_fnRequest)(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufReq[], size_t uReqLen)
Service thread request handler.
BsProxyClientCtl_T * ClientNew(Socket_T *pSockClient)
Create new client control structure.
int m_nPort
listen port number
void ThReqDelete(BsProxyThReq_T *pThReq)
Delete service thread request.
static void timer_mark(struct timeval *pTvMark)
Mark the current time. Resolution is microseconds.
int ClientSendVErrorRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid, int nECode, const char *sErrFmt, va_list ap)
Send va_list error response to the client.
int ThQueue(BsProxyThCtl_T *pThCtl, BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufReq[], size_t uReqLen)
Queue a request for the given service thread.
BsProxyThState_T m_eState
thread state
void ThOneTimeInit()
The service thread one-time global initialization.
const char * m_sClientName
client's (remote) name
INLINE_IN_H void ClientSetTraceState(BsProxyClientHnd_T hndClient, bool_t bTrace)
Set the <b><i>BotSense</i></b> client's server-terminated message trace state.
BsMsgId_T m_uMsgId
message id
int ClientSendRsp(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufRsp[], size_t uRspSize)
Send module-specific response to the client.
struct _bsproxy_th_ctl * BsProxyThCtl_P
service thread control
void ModOneTimeInit(DListStr_T *pDListLibPath)
The bsProxy interface module handling one-time initialization.
resyncing server with client
BsModOpenFunc_P m_fnModOpen
device open
BsModInitFunc_T * BsModInitFunc_P
pointer to init function
INLINE_IN_H const char * ClientHasName(BsProxyClientHnd_T hndClient)
Get the <b><i>BotSense</i></b> client official name.
const char * m_sDevUri
proxied device URI or server
void ClientDelete(BsProxyClientCtl_T *pClient)
Delete a client with the server.
BsProxyModIF_T * m_pModIF
interface module I/F
void * m_dllHandle
dynamic library loader handle
BsTid_T m_uTid
request-response transaction id
<b><i>BotSense</i></b> package top-level, unifying header declarations.
BsProxyVConn_T * VConnNew()
Create a new, allocated virtual connection entry.
int BsVConnHnd_T
virtual connection handle type
int ClientSendServerRsp(BsProxyClientHnd_T hndClient, BsTid_T uTid, BsProxyMsgId_T uMsgId, void *pMsgRsp)
Send a server-terminated response message to client.
void ModUnload(BsProxyModIF_T *pModIF)
Unload the interface module.
BsProxyServerCtl_T * BsServerCtl
The server control block.
char * m_sAddr
listen local address or hostname
int ServerRequest(BsProxyClientHnd_T hndClient, BsVConnHnd_T hndVConn, BsTid_T uTid, BsMsgId_T uMsgId, byte_t bufReq[], size_t uReqLen)
Server service thread request handler.
int m_nServerPort
server's listener port
INLINE_IN_H const char * ClientThisHasName(BsProxyClientCtl_T *pClient)
Get this <b><i>BotSense</i></b> client official name.
BsModRequestFunc_P m_fnModRequest
module request