![]() |
botsense
3.2.0
RoadNarrows Client-Server Proxied Services Framework
|
BotSense bsProxy client routines. More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/new.h"
#include "rnr/sock.h"
#include "rnr/netmsgs.h"
#include "botsense/BotSense.h"
#include "botsense/bsProxyMsgs.h"
#include "bsProxy.h"
Go to the source code of this file.
Macros | |
#define | CLIENT_HND(pClient) ServerClientSd2Hnd(SocketAttrGetSd(pClient->m_pClientSock)) |
Convert pointer to client to client handle. More... | |
Functions | |
static void | ClientLockBusy () |
Lock client's global busy mutual exclusion. | |
static void | ClientUnlockBusy () |
Unlock client's global busy mutual exclusion. | |
static bool_t | ClientBusyTryLock () |
Try to lock client's global busy mutual exclusion. More... | |
static void | ClientBroadcastNotBusy () |
Broadcast that a client has been freed or deleted. More... | |
static void | ClientWaitNotBusy () |
Wait on a client to become free. | |
static void | fdset_nowarn (int fd, fd_set *pset) |
FD_SET() wrapper with no annoying warnings. More... | |
static ssize_t | ClientRead (BsProxyClientCtl_T *pClient, byte_t buf[], size_t uCount) |
Read count bytes from socket. More... | |
static ssize_t | ClientWrite (BsProxyClientCtl_T *pClient, byte_t buf[], size_t uCount) |
Write count bytes to socket. More... | |
static ssize_t | ClientResync (BsProxyClientCtl_T *pClient, byte_t bufHdr[]) |
Resync to client message stream. More... | |
static void | ClientFlushInput (BsProxyClientCtl_T *pClient, size_t uCount) |
Flush receive stream. More... | |
void | ClientOneTimeInit () |
The BotSense bsProxy server one-time client control initialization. | |
BsProxyClientCtl_T * | ClientAcquire (BsProxyClientHnd_T hndClient) |
Acquire client, locking it from other threads. More... | |
void | ClientRelease (BsProxyClientHnd_T hndClient) |
Release the locked client. More... | |
BsProxyClientCtl_T * | ClientNew (Socket_T *pSockClient) |
Create new client control structure. More... | |
void | ClientDelete (BsProxyClientCtl_T *pClient) |
Delete a client with the server. More... | |
void | ClientSetState (BsProxyClientCtl_T *pClient, BsProxyClientState_T eNewState) |
Set client's state. More... | |
static int | ClientRecvHdr (BsProxyClientCtl_T *pClient, BsProxyMsgHdr_T *pMsgHdr) |
Receive client request header. More... | |
static int | ClientRecvBody (BsProxyClientCtl_T *pClient, byte_t buf[], size_t uBodyLen) |
Receive client request message body. More... | |
int | ClientRecvReq (BsProxyClientHnd_T hndClient, BsProxyMsgHdr_T *pMsgHdr, byte_t **addrBuf) |
Receive a request message from client. More... | |
int | ClientSendOkRsp (BsProxyClientHnd_T hndClient, BsTid_T uTid) |
Send an ok response to the client. More... | |
int | ClientSendErrorRsp (BsProxyClientHnd_T hndClient, BsTid_T uTid, int nECode, const char *sErrFmt,...) |
Send an error response to the client. More... | |
int | ClientSendVErrorRsp (BsProxyClientHnd_T hndClient, BsTid_T uTid, int nECode, const char *sErrFmt, va_list ap) |
Send va_list error response to the client. More... | |
int | ClientSendServerRsp (BsProxyClientHnd_T hndClient, BsTid_T uTid, BsProxyMsgId_T uMsgId, void *pMsgRsp) |
Send a server-terminated response message to client. More... | |
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. More... | |
Variables | |
static pthread_mutex_t | BsClientBusyMutex |
busy mutex | |
static pthread_cond_t | BsClientBusyCond |
busy condition | |
BotSense bsProxy client routines.
Definition in file bsProxyClient.c.
#define CLIENT_HND | ( | pClient | ) | ServerClientSd2Hnd(SocketAttrGetSd(pClient->m_pClientSock)) |
Convert pointer to client to client handle.
pClient |
Definition at line 81 of file bsProxyClient.c.
Referenced by ClientRecvBody(), and ClientRecvHdr().
BsProxyClientCtl_T* ClientAcquire | ( | BsProxyClientHnd_T | hndClient | ) |
Acquire client, locking it from other threads.
The calling thread is blocked until the client is availble or has been deleted.
hndClient | Client handle. |
Definition at line 546 of file bsProxyClient.c.
References ClientLockBusy(), ClientUnlockBusy(), ClientWaitNotBusy(), BsProxyClientCtl_T::m_bBusy, and ServerGetClient().
Referenced by BsProxyClientUnregister(), ClientRecvReq(), ClientSendRsp(), ClientSetTraceState(), ReqDevClose(), and ReqDevOpen().
|
inlinestatic |
Broadcast that a client has been freed or deleted.
A broadcast will unblock all threads currently blocked on the busy condition variable. Only those threads waiting on the client whose condition has changed will run. If multiple threads are waiting on the same client, then only one is schedule to run.
Definition at line 136 of file bsProxyClient.c.
References BsClientBusyCond.
Referenced by ClientRelease().
|
inlinestatic |
Try to lock client's global busy mutual exclusion.
Definition at line 123 of file bsProxyClient.c.
References BsClientBusyMutex.
void ClientDelete | ( | BsProxyClientCtl_T * | pClient | ) |
Delete a client with the server.
pClient |
Definition at line 621 of file bsProxyClient.c.
References BsProxyClientCtl_T::m_pClientSock, and BsProxyClientCtl_T::m_sClientName.
Referenced by BsProxyClientUnregister(), and ClientSetTraceState().
|
static |
Flush receive stream.
pClient |
Definition at line 501 of file bsProxyClient.c.
References ClientRead().
Referenced by ClientRecvHdr().
BsProxyClientCtl_T* ClientNew | ( | Socket_T * | pSockClient | ) |
Create new client control structure.
pSockClient | Accecpted client socket. |
Definition at line 602 of file bsProxyClient.c.
References BsProxyClientStateNominal, BsProxyClientCtl_T::m_bServerTrace, BsProxyClientCtl_T::m_eClientState, BsProxyClientCtl_T::m_pClientSock, BsProxyClientCtl_T::m_sClientName, and BsProxyClientCtl_T::m_uRefCnt.
Referenced by BsProxyClientRegister(), and ClientSetTraceState().
|
static |
Read count bytes from socket.
pClient |
Definition at line 188 of file bsProxyClient.c.
References BS_ECODE_BAD_RECV, BS_ECODE_SERVER_BAD_CLIENT, BSPROXY_LOG_SYSERROR, BSPROXY_TUNE_T_RECV, BsProxyClientStateZombie, ClientThisHasName(), fdset_nowarn(), ServerClientSd2Hnd(), timer_elapsed(), and timer_mark().
Referenced by ClientFlushInput(), ClientRecvBody(), ClientRecvHdr(), and ClientResync().
|
static |
Receive client request message body.
Sanity checks are performed on the message.
pClient |
Definition at line 798 of file bsProxyClient.c.
References BS_ECODE_BAD_RECV, BS_ECODE_MSG_FRAG, BSPROXY_LOG_ERROR, BsProxyClientStateReSync, CLIENT_HND, ClientRead(), and ClientSetState().
Referenced by ClientRecvReq().
|
static |
Receive client request header.
Sanity checks are performed on the header.
pClient |
< normal operation
< resyncing server with client
< disconnected/bad state, but not deleted
Definition at line 702 of file bsProxyClient.c.
References BS_ECODE_BAD_RECV, BS_ECODE_MSG_BAD_HDR, BS_ECODE_MSG_FRAG, BS_ECODE_MSG_TOO_BIG, BS_ECODE_SERVER_BAD_CLIENT, BSPROXY_LOG_ERROR, BSPROXY_MSG_BODY_MAX, BSPROXY_MSG_HDR_LEN, BSPROXY_MSG_MAGIC, BsProxyClientStateNominal, BsProxyClientStateReSync, BsProxyClientStateZombie, CLIENT_HND, ClientFlushInput(), ClientRead(), ClientResync(), ClientSetState(), BsProxyClientCtl_T::m_eClientState, BsProxyMsgHdr_T::m_hdrBodyLen, and BsProxyMsgHdr_T::m_hdrMagic.
Referenced by ClientRecvReq().
int ClientRecvReq | ( | BsProxyClientHnd_T | hndClient, |
BsProxyMsgHdr_T * | pMsgHdr, | ||
byte_t ** | addrBuf | ||
) |
Receive a request message from client.
hndClient |
Definition at line 836 of file bsProxyClient.c.
References BS_ECODE_SERVER_BAD_CLIENT, BSPROXY_LOG_REQ, BsProxyClientStateNominal, ClientAcquire(), ClientRecvBody(), ClientRecvHdr(), ClientRelease(), ClientSetState(), ClientThisHasName(), BsProxyClientCtl_T::m_eClientState, BsProxyMsgHdr_T::m_hdrBodyLen, and ServerHasName().
Referenced by BsProxyServer(), and ClientSetTraceState().
void ClientRelease | ( | BsProxyClientHnd_T | hndClient | ) |
Release the locked client.
A broadcast is sent to all blocking threads on the freed event.
hndClient | Client handle. |
Definition at line 574 of file bsProxyClient.c.
References ClientBroadcastNotBusy(), ClientLockBusy(), ClientUnlockBusy(), BsProxyClientCtl_T::m_bBusy, and ServerGetClient().
Referenced by ClientRecvReq(), ClientSendRsp(), ClientSetTraceState(), ReqDevClose(), and ReqDevOpen().
|
static |
Resync to client message stream.
If a client receieve stream becomes unsynchronized, the message headers are aliased to some offset into the stream. To resync, bsProxy searches the stream for the magic bytes in the message header. Once found, the reset of the header is read.
pClient |
Definition at line 436 of file bsProxyClient.c.
References BSPROXY_MSG_HDR_LEN, BSPROXY_MSG_MAGIC, and ClientRead().
Referenced by ClientRecvHdr().
int ClientSendErrorRsp | ( | BsProxyClientHnd_T | hndClient, |
BsTid_T | uTid, | ||
int | nECode, | ||
const char * | sErrFmt, | ||
... | |||
) |
Send an error response to the client.
hndClient |
Definition at line 960 of file bsProxyClient.c.
References ClientSendVErrorRsp().
Referenced by ClientSetTraceState().
int ClientSendOkRsp | ( | BsProxyClientHnd_T | hndClient, |
BsTid_T | uTid | ||
) |
Send an ok response to the client.
hndClient |
Definition at line 936 of file bsProxyClient.c.
References BSPROXY_MSG_HDR_LEN, BSPROXY_VCONN_SERVER, BsProxyMsgIdRspOk, and ClientSendRsp().
Referenced by ClientSetTraceState(), ModCbSendOkRsp(), ReqDevClose(), ReqMsgTrace(), and ReqSetLogging().
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.
hndClient |
Definition at line 1084 of file bsProxyClient.c.
References BS_ECODE_BAD_SEND, BS_ECODE_INTERNAL, BS_ECODE_SERVER_BAD_CLIENT, BSPROXY_LOG_ERROR, BSPROXY_LOG_RSP, BSPROXY_LOG_SYSERROR, BSPROXY_MSG_HDR_LEN, BSPROXY_MSG_MAGIC, ClientAcquire(), ClientRelease(), ClientWrite(), BsProxyMsgHdr_T::m_hdrBodyLen, BsProxyMsgHdr_T::m_hdrMagic, BsProxyMsgHdr_T::m_hdrMsgId, BsProxyMsgHdr_T::m_hdrTid, and BsProxyMsgHdr_T::m_hdrVConn.
Referenced by ClientSendOkRsp(), ClientSendServerRsp(), ClientSetTraceState(), and ModCbSendRsp().
int ClientSendServerRsp | ( | BsProxyClientHnd_T | hndClient, |
BsTid_T | uTid, | ||
BsProxyMsgId_T | uMsgId, | ||
void * | pMsgRsp | ||
) |
Send a server-terminated response message to client.
The response message header must contain the information except for the body length which will be automatically calculated.
hndClient |
Definition at line 1025 of file bsProxyClient.c.
References BS_ECODE_INTERNAL, BSPROXY_BUF_BODY, BSPROXY_LOG_ERROR, BSPROXY_LOG_NMERROR, BSPROXY_MSG_MAX_LEN, BSPROXY_VCONN_SERVER, BsProxyLookupMsgDef(), BsProxyPackMsg(), ClientGetTraceState(), and ClientSendRsp().
Referenced by ClientSendVErrorRsp(), ClientSetTraceState(), ReqDevOpen(), ReqGetVConnInfo(), ReqGetVConnList(), ReqGetVersion(), and ReqLoopback().
int ClientSendVErrorRsp | ( | BsProxyClientHnd_T | hndClient, |
BsTid_T | uTid, | ||
int | nECode, | ||
const char * | sErrFmt, | ||
va_list | ap | ||
) |
Send va_list error response to the client.
hndClient |
Definition at line 986 of file bsProxyClient.c.
References BSPROXY_RSPERR_EMSG_LEN, BsProxyMsgIdRspErr, ClientSendServerRsp(), BsProxyRspErr_T::m_ecode, and BsProxyRspErr_T::m_emsg.
Referenced by ClientSendErrorRsp(), ClientSetTraceState(), and ModCbSendErrorRsp().
void ClientSetState | ( | BsProxyClientCtl_T * | pClient, |
BsProxyClientState_T | eNewState | ||
) |
Set client's state.
pClient |
< disconnected but not deleted
< normal operation
< resyncing server with client
Definition at line 643 of file bsProxyClient.c.
References BsProxyClientStateNominal, BsProxyClientStateReSync, BsProxyClientStateZombie, ClientThisHasName(), BsProxyClientCtl_T::m_eClientState, BsProxyClientCtl_T::m_sClientName, and ServerHasName().
Referenced by BsProxyClientUnregister(), ClientRecvBody(), ClientRecvHdr(), ClientRecvReq(), and ClientSetTraceState().
|
static |
Write count bytes to socket.
pClient |
Definition at line 311 of file bsProxyClient.c.
References BS_ECODE_BAD_SEND, BS_ECODE_SERVER_BAD_CLIENT, BSPROXY_LOG_SYSERROR, BSPROXY_TUNE_T_SEND, BsProxyClientStateZombie, ClientThisHasName(), fdset_nowarn(), BsProxyClientCtl_T::m_eClientState, BsProxyClientCtl_T::m_pClientSock, ServerClientSd2Hnd(), timer_elapsed(), and timer_mark().
Referenced by ClientSendRsp().
|
inlinestatic |
FD_SET() wrapper with no annoying warnings.
fd | File descriptor to add to set. |
pset | Pointer to fd set. |
Definition at line 172 of file bsProxyClient.c.
Referenced by ClientRead(), and ClientWrite().