botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
bsProxyUtils.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: BotSense
4 //
5 // Program: bsProxy
6 //
7 // File: bsProxyUtils.c
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2010-08-20 11:36:38 -0600 (Fri, 20 Aug 2010) $
12  * $Rev: 568 $
13  *
14  * \brief \h_botsense bsProxy utilities.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2010-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 // Permission is hereby granted, without written agreement and without
24 // license or royalty fees, to use, copy, modify, and distribute this
25 // software and its documentation for any purpose, provided that
26 // (1) The above copyright notice and the following two paragraphs
27 // appear in all copies of the source code and (2) redistributions
28 // including binaries reproduces these notices in the supporting
29 // documentation. Substantial modifications to this software may be
30 // copyrighted by their authors and need not follow the licensing terms
31 // described here, provided that the new terms are clearly indicated in
32 // all files where they apply.
33 //
34 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
35 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
36 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
37 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
38 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
39 // THE POSSIBILITY OF SUCH DAMAGE.
40 //
41 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
42 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
44 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
45 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
46 //
47 ////////////////////////////////////////////////////////////////////////////////
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <errno.h>
53 #include <pthread.h>
54 
55 #include "rnr/rnrconfig.h"
56 #include "rnr/log.h"
57 #include "rnr/netmsgs.h"
58 
59 #include "botsense/BotSense.h"
60 #include "botsense/bsProxyMsgs.h"
61 
62 #include "bsProxy.h"
63 
64 
65 // ---------------------------------------------------------------------------
66 // Public Interface
67 // ---------------------------------------------------------------------------
68 
69 
70 /*!
71  * \brief Calculate the elapsed time between the given time mark and this call.
72  *
73  * \param pTvMark Pointer to timeval holding time mark.
74  *
75  * \return
76  * Number of microseconds elasped. If the marked time is invalid or the current
77  * time cannot be ascertained, UINT_MAX is returned.
78  */
79 uint_t timer_elapsed(struct timeval *pTvMark)
80 {
81  struct timeval tvEnd, tvDelta;
82 
83  timer_mark(&tvEnd);
84 
85  if( !timerisset(pTvMark) || !timerisset(&tvEnd) )
86  {
87  return UINT_MAX;
88  }
89 
90  tvDelta.tv_sec = tvEnd.tv_sec - pTvMark->tv_sec;
91  if( tvEnd.tv_usec < pTvMark->tv_usec )
92  {
93  tvDelta.tv_sec--;
94  tvEnd.tv_usec += 1000000;
95  }
96  tvDelta.tv_usec = tvEnd.tv_usec - pTvMark->tv_usec;
97 
98  return (uint_t)(tvDelta.tv_sec * 1000000 + tvDelta.tv_usec);
99 }
100 
101 #ifdef LOG
102 /*!
103  * \brief Log message header.
104  *
105  * \param sPreface Preface string.
106  * \param pMsgHdr Pointer to message header structure.
107  */
108 void BsProxyLogMsgHdr(const char *sPreface, BsProxyMsgHdr_T *pMsgHdr)
109 {
110  FILE *fp;
111  const NMMsgDef_T *pMsgDef;
112  const char *sMsgName;
113 
114  fp = LOG_GET_LOGFP();
115 
116  if( pMsgHdr != NULL )
117  {
118  if( pMsgHdr->m_hdrVConn == BSPROXY_VCONN_SERVER )
119  {
120  pMsgDef = BsProxyLookupMsgDef((BsProxyMsgId_T)pMsgHdr->m_hdrMsgId);
121  sMsgName = pMsgDef!=NULL? pMsgDef->m_sMsgName: "";
122  }
123  else
124  {
125  sMsgName = "";
126  }
127  fprintf(fp, "%s MsgHdr = {\n", sPreface);
128  fprintf(fp, " Magic: 0x%04x\n", pMsgHdr->m_hdrMagic);
129  fprintf(fp, " Tid: %u\n", (uint_t)(pMsgHdr->m_hdrTid));
130  fprintf(fp, " VConn: %u\n", (uint_t)(pMsgHdr->m_hdrVConn));
131  fprintf(fp, " MsgId: %u %s\n", pMsgHdr->m_hdrMsgId, sMsgName);
132  fprintf(fp, " BodyLen: %u\n", pMsgHdr->m_hdrBodyLen);
133  fprintf(fp, "}\n");
134  }
135  else
136  {
137  fprintf(fp, "%s MsgHdr: (null)\n", sPreface);
138  }
139 }
140 #endif // LOG
141 
<b><i>BotSense</i></b> bsProxy IP server declarations.
const NMMsgDef_T * BsProxyLookupMsgDef(BsProxyMsgId_T eMsgId)
Look up the message definition associated with the message id.
Definition: bsProxyMsgs.c:874
BotSense client application - bsProxy server-terminated core messages.
#define BSPROXY_VCONN_SERVER
handle for server-terminated msgs
Definition: BotSense.h:140
uint_t timer_elapsed(struct timeval *pTvMark)
Calculate the elapsed time between the given time mark and this call.
Definition: bsProxyUtils.c:79
ushort_t m_hdrBodyLen
message body length
Definition: BotSense.h:284
ushort_t m_hdrMsgId
message id (vConnection unique)
Definition: BotSense.h:283
ushort_t m_hdrMagic
"unique" magic pattern
Definition: BotSense.h:280
void BsProxyLogMsgHdr(const char *sPreface, BsProxyMsgHdr_T *pMsgHdr)
Log message header.
Definition: bsProxyUtils.c:108
BsProxyMsgId_T
Definition: bsProxyMsgs.h:35
BotSense Proxy Message Header Structure.
Definition: BotSense.h:278
byte_t m_hdrTid
transaction id
Definition: BotSense.h:281
static void timer_mark(struct timeval *pTvMark)
Mark the current time. Resolution is microseconds.
Definition: bsLibClient.c:340
byte_t m_hdrVConn
virtual connection handle (server unique)
Definition: BotSense.h:282
<b><i>BotSense</i></b> package top-level, unifying header declarations.