Click here to Skip to main content
15,894,646 members
Articles / Programming Languages / C

A Generic C-Language TCP Client Application

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
9 May 2010CPOL4 min read 29.1K   793   20  
A library for writing simple TCP client applications
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>TcpClient2: Client.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.3 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="main.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
      <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
    </ul>
  </div>
<h1>Client.c</h1><a href="Client_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/// @source      Client.c</span>
<a name="l00002"></a>00002 <span class="comment"></span><span class="comment">/// @description Implementation of class Client.</span>
<a name="l00003"></a>00003 <span class="comment"></span><span class="comment">//  See licensing information in the file README.TXT.</span>
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00006"></a>00006 
<a name="l00007"></a>00007 <span class="comment">//  Application Description</span>
<a name="l00008"></a>00008 <span class="comment"></span>
<a name="l00009"></a>00009 <span class="comment">/** @mainpage A C-Language Library for Writing Generic TCP %Client Applications</span>
<a name="l00010"></a>00010 <span class="comment"></span>
<a name="l00011"></a>00011 <span class="comment">    A library for writing simple TCP client applications, written in the</span>
<a name="l00012"></a>00012 <span class="comment">    C-language with an object oriented approach, using the Berkeley sockets</span>
<a name="l00013"></a>00013 <span class="comment">    interface, and intended to be cross-platform.  @par</span>
<a name="l00014"></a>00014 <span class="comment"></span>
<a name="l00015"></a>00015 <span class="comment">    A &lt;b&gt; generic client &lt;/b&gt; written with this library is a simple TCP client</span>
<a name="l00016"></a>00016 <span class="comment">    application created following a specific pattern that is built around three</span>
<a name="l00017"></a>00017 <span class="comment">    concepts: &lt;b&gt; state&lt;/b&gt;, &lt;b&gt; event&lt;/b&gt; and &lt;b&gt; command&lt;/b&gt;. @par</span>
<a name="l00018"></a>00018 <span class="comment"></span>
<a name="l00019"></a>00019 <span class="comment">    &lt;b&gt; State &lt;/b&gt; is one of these     &lt;br&gt;</span>
<a name="l00020"></a>00020 <span class="comment">       (1) &lt;b&gt; CONNECTED_IDLE &lt;/b&gt;     &lt;br&gt;</span>
<a name="l00021"></a>00021 <span class="comment">       (2) &lt;b&gt; NOT_CONNECTED &lt;/b&gt;.     @par</span>
<a name="l00022"></a>00022 <span class="comment"></span>
<a name="l00023"></a>00023 <span class="comment">    &lt;b&gt; Command &lt;/b&gt; is one of these:  &lt;br&gt;</span>
<a name="l00024"></a>00024 <span class="comment">       (1) &lt;b&gt;CONNECT&lt;/b&gt;              &lt;br&gt;</span>
<a name="l00025"></a>00025 <span class="comment">       (2) &lt;b&gt;SEND&lt;/b&gt;                 &lt;br&gt;</span>
<a name="l00026"></a>00026 <span class="comment">       (3) &lt;b&gt;RECV&lt;/b&gt;                 &lt;br&gt;</span>
<a name="l00027"></a>00027 <span class="comment">       (4) &lt;b&gt;CLOSE&lt;/b&gt;.               @par</span>
<a name="l00028"></a>00028 <span class="comment"></span>
<a name="l00029"></a>00029 <span class="comment">    &lt;b&gt;Event&lt;/b&gt; is one of these:         &lt;br&gt;</span>
<a name="l00030"></a>00030 <span class="comment">       (1) &lt;b&gt;CONNECTION_CREATED&lt;/b&gt;      &lt;br&gt;</span>
<a name="l00031"></a>00031 <span class="comment">       (2) &lt;b&gt;CONNECTION_DESTROYED&lt;/b&gt;    &lt;br&gt;</span>
<a name="l00032"></a>00032 <span class="comment">       (3) &lt;b&gt;CONNECT_ERROR&lt;/b&gt;           &lt;br&gt;</span>
<a name="l00033"></a>00033 <span class="comment">       (4) &lt;b&gt;RECV_COMPLETE&lt;/b&gt;           &lt;br&gt;</span>
<a name="l00034"></a>00034 <span class="comment">       (5) &lt;b&gt;RECV_TIMEOUT&lt;/b&gt;            &lt;br&gt;</span>
<a name="l00035"></a>00035 <span class="comment">       (6) &lt;b&gt;SEND_COMPLETE&lt;/b&gt;.          @par</span>
<a name="l00036"></a>00036 <span class="comment"></span>
<a name="l00037"></a>00037 <span class="comment">    There are also two other entities involved: an instance of the GenericClient</span>
<a name="l00038"></a>00038 <span class="comment">    class, provided by the library, and the &lt;b&gt;application code&lt;/b&gt; written by</span>
<a name="l00039"></a>00039 <span class="comment">    the user of the library. The GenericClient class is informally derived from</span>
<a name="l00040"></a>00040 <span class="comment">    the Client class, which provides the basic methods for writing client</span>
<a name="l00041"></a>00041 <span class="comment">    applications. @par</span>
<a name="l00042"></a>00042 <span class="comment"></span>
<a name="l00043"></a>00043 <span class="comment">    The general flow of control in an application written with this library is</span>
<a name="l00044"></a>00044 <span class="comment">    like this: &lt;br&gt;</span>
<a name="l00045"></a>00045 <span class="comment">       (1) the &lt;b&gt; application code &lt;/b&gt; passes up a &lt;b&gt; command&lt;/b&gt; to</span>
<a name="l00046"></a>00046 <span class="comment">           the GenericClient instance, &lt;br&gt;</span>
<a name="l00047"></a>00047 <span class="comment">       (2) which processes the received &lt;b&gt; command&lt;/b&gt; and passes down an</span>
<a name="l00048"></a>00048 <span class="comment">           &lt;b&gt;event&lt;/b&gt; to the &lt;b&gt; application code&lt;/b&gt;; &lt;br&gt;</span>
<a name="l00049"></a>00049 <span class="comment">       (3) the &lt;b&gt; application code &lt;/b&gt; then processes the &lt;b&gt; event &lt;/b&gt;</span>
<a name="l00050"></a>00050 <span class="comment">           received from the GenericClient instance, and the cycle </span>
<a name="l00051"></a>00051 <span class="comment">           repeats again. @par</span>
<a name="l00052"></a>00052 <span class="comment"></span>
<a name="l00053"></a>00053 <span class="comment">    The GenericClient instance is driven by a state machine, which is described </span>
<a name="l00054"></a>00054 <span class="comment">    below in pseudo-code:</span>
<a name="l00055"></a>00055 <span class="comment"></span>
<a name="l00056"></a>00056 <span class="comment">    @code</span>
<a name="l00057"></a>00057 <span class="comment"></span>
<a name="l00058"></a>00058 <span class="comment">    1. if state is NOT_CONNECTED</span>
<a name="l00059"></a>00059 <span class="comment"></span>
<a name="l00060"></a>00060 <span class="comment">          // only accepts the CONNECT command</span>
<a name="l00061"></a>00061 <span class="comment">          do connect</span>
<a name="l00062"></a>00062 <span class="comment"></span>
<a name="l00063"></a>00063 <span class="comment">          if result is OK</span>
<a name="l00064"></a>00064 <span class="comment">             set state to CONNECTED_IDLE</span>
<a name="l00065"></a>00065 <span class="comment">             return event CONNECTION_CREATED</span>
<a name="l00066"></a>00066 <span class="comment">          else</span>
<a name="l00067"></a>00067 <span class="comment">             // remains in the state NOT_CONNECTED</span>
<a name="l00068"></a>00068 <span class="comment">             return event CONNECT_ERROR</span>
<a name="l00069"></a>00069 <span class="comment"></span>
<a name="l00070"></a>00070 <span class="comment">    2. if state is CONNECTED_IDLE</span>
<a name="l00071"></a>00071 <span class="comment"></span>
<a name="l00072"></a>00072 <span class="comment">          2.1 if command is SEND</span>
<a name="l00073"></a>00073 <span class="comment">              do send</span>
<a name="l00074"></a>00074 <span class="comment">              if result is OK</span>
<a name="l00075"></a>00075 <span class="comment">                 // remains in the state CONNECTED_IDLE</span>
<a name="l00076"></a>00076 <span class="comment">                 return event SEND_COMPLETE</span>
<a name="l00077"></a>00077 <span class="comment">              else</span>
<a name="l00078"></a>00078 <span class="comment">                set state to NOT_CONNECTED</span>
<a name="l00079"></a>00079 <span class="comment">                return event CONNECTION_DESTROYED</span>
<a name="l00080"></a>00080 <span class="comment"></span>
<a name="l00081"></a>00081 <span class="comment">          2.2 if command is RECV</span>
<a name="l00082"></a>00082 <span class="comment">              do recv</span>
<a name="l00083"></a>00083 <span class="comment">              if result is OK</span>
<a name="l00084"></a>00084 <span class="comment">                 // remains in the state CONNECTED_IDLE</span>
<a name="l00085"></a>00085 <span class="comment">                 return event RECV_COMPLETE</span>
<a name="l00086"></a>00086 <span class="comment">              else if occurred TIMEOUT</span>
<a name="l00087"></a>00087 <span class="comment">                 // remains in the state CONNECTED_IDLE</span>
<a name="l00088"></a>00088 <span class="comment">                 return event RECV_TIMEOUT</span>
<a name="l00089"></a>00089 <span class="comment">              else // error</span>
<a name="l00090"></a>00090 <span class="comment">                 set state to NOT_CONNECTED</span>
<a name="l00091"></a>00091 <span class="comment">                 return event CONNECTION_DESTROYED</span>
<a name="l00092"></a>00092 <span class="comment"></span>
<a name="l00093"></a>00093 <span class="comment">          2.3 if command is CLOSE</span>
<a name="l00094"></a>00094 <span class="comment">              do close</span>
<a name="l00095"></a>00095 <span class="comment">              set state to NOT_CONNECTED</span>
<a name="l00096"></a>00096 <span class="comment">              return event CONNECTION_DESTROYED</span>
<a name="l00097"></a>00097 <span class="comment">    @endcode</span>
<a name="l00098"></a>00098 <span class="comment"></span>
<a name="l00099"></a>00099 <span class="comment">    The &lt;b&gt; application code&lt;/b&gt; doesn&#39;t have to be concerned with how the</span>
<a name="l00100"></a>00100 <span class="comment">    GenericClient instance works; its only concern is how to handle the events</span>
<a name="l00101"></a>00101 <span class="comment">    it receives from the GenericClient instance. @par</span>
<a name="l00102"></a>00102 <span class="comment"></span>
<a name="l00103"></a>00103 <span class="comment">    The pseudo-code for the &lt;b&gt;application code&lt;/b&gt; is open, but generally will</span>
<a name="l00104"></a>00104 <span class="comment">    be like the following:</span>
<a name="l00105"></a>00105 <span class="comment"></span>
<a name="l00106"></a>00106 <span class="comment">    @verbatim</span>
<a name="l00107"></a>00107 <span class="comment"></span>
<a name="l00108"></a>00108 <span class="comment">    1. if event is CONNECTION_CREATED</span>
<a name="l00109"></a>00109 <span class="comment">          prepare message to send to the server</span>
<a name="l00110"></a>00110 <span class="comment">          pass the command SEND to the library</span>
<a name="l00111"></a>00111 <span class="comment"></span>
<a name="l00112"></a>00112 <span class="comment">    2. if event is SEND_COMPLETE</span>
<a name="l00113"></a>00113 <span class="comment">          update whatever controls needed by the application</span>
<a name="l00114"></a>00114 <span class="comment">          pass the command RECV to the library</span>
<a name="l00115"></a>00115 <span class="comment"></span>
<a name="l00116"></a>00116 <span class="comment">    3. if event is RECV_COMPLETE</span>
<a name="l00117"></a>00117 <span class="comment">          process reply received from the server</span>
<a name="l00118"></a>00118 <span class="comment">          prepare another message to send to the server</span>
<a name="l00119"></a>00119 <span class="comment">          pass the command SEND to the library</span>
<a name="l00120"></a>00120 <span class="comment"></span>
<a name="l00121"></a>00121 <span class="comment">    4. if event is RECV_TIMEOUT (*** optional ***)</span>
<a name="l00122"></a>00122 <span class="comment">          do whatever the application needs in case of timeout</span>
<a name="l00123"></a>00123 <span class="comment">          if business rules says try again</span>
<a name="l00124"></a>00124 <span class="comment">             prepare another message to send to the server</span>
<a name="l00125"></a>00125 <span class="comment">             pass the command SEND to the library</span>
<a name="l00126"></a>00126 <span class="comment">          else</span>
<a name="l00127"></a>00127 <span class="comment">             pass the command CLOSE to the library</span>
<a name="l00128"></a>00128 <span class="comment"></span>
<a name="l00129"></a>00129 <span class="comment">    5. if event is CONNECTION_DESTROYED</span>
<a name="l00130"></a>00130 <span class="comment">          // can be the result of SEND, RECV or CLOSE commands</span>
<a name="l00131"></a>00131 <span class="comment">          // (most errors will automatically destroy the connection)</span>
<a name="l00132"></a>00132 <span class="comment">          do whatever cleanup the application needs</span>
<a name="l00133"></a>00133 <span class="comment">          pass the command CONNECT to the library</span>
<a name="l00134"></a>00134 <span class="comment"></span>
<a name="l00135"></a>00135 <span class="comment">    6. if event is CONNECT_ERROR</span>
<a name="l00136"></a>00136 <span class="comment">          do whatever the application needs in this case</span>
<a name="l00137"></a>00137 <span class="comment">          if business rules says try again</span>
<a name="l00138"></a>00138 <span class="comment">             pass the command CONNECT to the library</span>
<a name="l00139"></a>00139 <span class="comment">          else</span>
<a name="l00140"></a>00140 <span class="comment">             end application</span>
<a name="l00141"></a>00141 <span class="comment"></span>
<a name="l00142"></a>00142 <span class="comment">    @endverbatim</span>
<a name="l00143"></a>00143 <span class="comment"></span>
<a name="l00144"></a>00144 <span class="comment">    Using the pattern described above, writing simple TCP client applications</span>
<a name="l00145"></a>00145 <span class="comment">    is a snap, because nowhere the TCP/IP networking code is seen. In practice,</span>
<a name="l00146"></a>00146 <span class="comment">    only the application code needs to be written. @par</span>
<a name="l00147"></a>00147 <span class="comment"></span>
<a name="l00148"></a>00148 <span class="comment">    The goal here is not writing production-grade applications, but small</span>
<a name="l00149"></a>00149 <span class="comment">    utilities for testing scenarios, for prototyping new functionalities, for</span>
<a name="l00150"></a>00150 <span class="comment">    finding application bugs in servers, etc. In these cases, speed of</span>
<a name="l00151"></a>00151 <span class="comment">    development is of paramount importance, because often the programs created</span>
<a name="l00152"></a>00152 <span class="comment">    are discardable, throwaway utilities, with limited scope and functionality,</span>
<a name="l00153"></a>00153 <span class="comment">    which do no justify spending too much time in their development. @par</span>
<a name="l00154"></a>00154 <span class="comment"></span>
<a name="l00155"></a>00155 <span class="comment"></span>
<a name="l00156"></a>00156 <span class="comment">    The C code that implements the pseudo-code above is something like this:</span>
<a name="l00157"></a>00157 <span class="comment">    @code</span>
<a name="l00158"></a>00158 <span class="comment"></span>
<a name="l00159"></a>00159 <span class="comment">    switch (genCli_waitEvent())</span>
<a name="l00160"></a>00160 <span class="comment">    {</span>
<a name="l00161"></a>00161 <span class="comment">       case CLI_EVT_CONNECTION_CREATED:</span>
<a name="l00162"></a>00162 <span class="comment">          prepareFirstMessage();</span>
<a name="l00163"></a>00163 <span class="comment">          genCli_send();</span>
<a name="l00164"></a>00164 <span class="comment">          break;</span>
<a name="l00165"></a>00165 <span class="comment"></span>
<a name="l00166"></a>00166 <span class="comment">       case CLI_EVT_RECV_COMPLETE:</span>
<a name="l00167"></a>00167 <span class="comment">          processServerReply();</span>
<a name="l00168"></a>00168 <span class="comment">          prepareAnotherMessage();</span>
<a name="l00169"></a>00169 <span class="comment">          genCli_send();</span>
<a name="l00170"></a>00170 <span class="comment">          break;</span>
<a name="l00171"></a>00171 <span class="comment"></span>
<a name="l00172"></a>00172 <span class="comment">       case CLI_EVT_SEND_COMPLETE:</span>
<a name="l00173"></a>00173 <span class="comment">          genCli_recv();</span>
<a name="l00174"></a>00174 <span class="comment">          break;</span>
<a name="l00175"></a>00175 <span class="comment"></span>
<a name="l00176"></a>00176 <span class="comment">       case CLI_EVT_RECV_TIMEOUT:</span>
<a name="l00177"></a>00177 <span class="comment">          prepareAnotherMessage();</span>
<a name="l00178"></a>00178 <span class="comment">          genCli_send();</span>
<a name="l00179"></a>00179 <span class="comment">          break;</span>
<a name="l00180"></a>00180 <span class="comment"></span>
<a name="l00181"></a>00181 <span class="comment">       case CLI_EVT_CONNECT_ERROR:</span>
<a name="l00182"></a>00182 <span class="comment">          printConnectionError();</span>
<a name="l00183"></a>00183 <span class="comment">          client_sleep(5);</span>
<a name="l00184"></a>00184 <span class="comment">          genCli_connect();</span>
<a name="l00185"></a>00185 <span class="comment">          break;</span>
<a name="l00186"></a>00186 <span class="comment"></span>
<a name="l00187"></a>00187 <span class="comment">       case CLI_EVT_CONNECTION_DESTROYED:</span>
<a name="l00188"></a>00188 <span class="comment">          printOperationError();</span>
<a name="l00189"></a>00189 <span class="comment">          genCli_connect();</span>
<a name="l00190"></a>00190 <span class="comment">          break;</span>
<a name="l00191"></a>00191 <span class="comment"></span>
<a name="l00192"></a>00192 <span class="comment">       default:</span>
<a name="l00193"></a>00193 <span class="comment">          printf(&quot;*invalid event %d\n&quot;, genCli_event());</span>
<a name="l00194"></a>00194 <span class="comment">          abort();</span>
<a name="l00195"></a>00195 <span class="comment">          break;</span>
<a name="l00196"></a>00196 <span class="comment">    } // switch</span>
<a name="l00197"></a>00197 <span class="comment"></span>
<a name="l00198"></a>00198 <span class="comment">    @endcode</span>
<a name="l00199"></a>00199 <span class="comment"></span>
<a name="l00200"></a>00200 <span class="comment">    Please refer to the &lt;b&gt;gen_client_1&lt;/b&gt; example project for more details.</span>
<a name="l00201"></a>00201 <span class="comment">    @par</span>
<a name="l00202"></a>00202 <span class="comment"></span>
<a name="l00203"></a>00203 <span class="comment">    The library also provides additional funcionality, presented by the Client</span>
<a name="l00204"></a>00204 <span class="comment">    class but in fact implemented by other classes (the Client class here acting</span>
<a name="l00205"></a>00205 <span class="comment">    as a façade). @par</span>
<a name="l00206"></a>00206 <span class="comment"></span>
<a name="l00207"></a>00207 <span class="comment">    The most important of the additional functionality is related to the Message</span>
<a name="l00208"></a>00208 <span class="comment">    class. It provides an encapsulation for the buffers used when exchanging</span>
<a name="l00209"></a>00209 <span class="comment">    messages between client and server, and also provides the framing that</span>
<a name="l00210"></a>00210 <span class="comment">    delimits messages on the wire. Please see the documentation for the Message</span>
<a name="l00211"></a>00211 <span class="comment">    class, for the Client class, and the &lt;b&gt;gen_client_1&lt;/b&gt; example that is</span>
<a name="l00212"></a>00212 <span class="comment">    shipped with this project. For the record, the more important methods</span>
<a name="l00213"></a>00213 <span class="comment">    provided by the Message class for the use of applications are</span>
<a name="l00214"></a>00214 <span class="comment">    (encapsulated by the Client class):</span>
<a name="l00215"></a>00215 <span class="comment">       @link Client::client_messageBuffer client_messageBuffer @endlink,</span>
<a name="l00216"></a>00216 <span class="comment">       @link Client::client_messageSize client_messageSize @endlink, and</span>
<a name="l00217"></a>00217 <span class="comment">       @link Client::client_setMessageSize client_setMessageSize @endlink.</span>
<a name="l00218"></a>00218 <span class="comment">       @par</span>
<a name="l00219"></a>00219 <span class="comment"></span>
<a name="l00220"></a>00220 <span class="comment">    There is also a Log class, that is used internally by the library but</span>
<a name="l00221"></a>00221 <span class="comment">    can also be used by applications. Its main methods are (again,</span>
<a name="l00222"></a>00222 <span class="comment">    encapsulated by the Client class):</span>
<a name="l00223"></a>00223 <span class="comment">       @link Client::client_logInfo  client_logInfo @endlink,</span>
<a name="l00224"></a>00224 <span class="comment">       @link Client::client_logWarn  client_logWarn @endlink,</span>
<a name="l00225"></a>00225 <span class="comment">       @link Client::client_logDebug client_logDebug @endlink,</span>
<a name="l00226"></a>00226 <span class="comment">       @link Client::client_logTrace client_logTrace @endlink,</span>
<a name="l00227"></a>00227 <span class="comment">       @link Client::client_logError client_logError @endlink and</span>
<a name="l00228"></a>00228 <span class="comment">       @link Client::client_logFatal client_logFatal @endlink.</span>
<a name="l00229"></a>00229 <span class="comment">       @par</span>
<a name="l00230"></a>00230 <span class="comment"></span>
<a name="l00231"></a>00231 <span class="comment">    There are some other classes used internally by the library, that may or</span>
<a name="l00232"></a>00232 <span class="comment">    may not be useful when writing the client applications: Mutex, Thread, Time</span>
<a name="l00233"></a>00233 <span class="comment">    and Timeout.</span>
<a name="l00234"></a>00234 <span class="comment"></span>
<a name="l00235"></a>00235 <span class="comment">    This library is written to inter-operate with servers written using the</span>
<a name="l00236"></a>00236 <span class="comment">    library described in this</span>
<a name="l00237"></a>00237 <span class="comment">    &lt;a href=&quot;http://www.codeproject.com/KB/IP/TCPServerFramework.aspx&quot;&gt;</span>
<a name="l00238"></a>00238 <span class="comment">    article&lt;/a&gt;. Specifically, the &lt;b&gt;gen_client_1&lt;/b&gt; example shipped</span>
<a name="l00239"></a>00239 <span class="comment">    with this project works with the &lt;b&gt; server_2 &lt;/b&gt; and &lt;b&gt; server_3 &lt;/b&gt;</span>
<a name="l00240"></a>00240 <span class="comment">    examples shipped with the article above.</span>
<a name="l00241"></a>00241 <span class="comment"></span>
<a name="l00242"></a>00242 <span class="comment">    To adapt the library to work with other servers it&#39;s very likely that the</span>
<a name="l00243"></a>00243 <span class="comment">    Message structure declared in the MessageImpl.h file will have to be </span>
<a name="l00244"></a>00244 <span class="comment">    changed. Specifically, refer to the on-the-wire format of the messages</span>
<a name="l00245"></a>00245 <span class="comment">    exchanged between the client and the server.</span>
<a name="l00246"></a>00246 <span class="comment">*/</span>
<a name="l00247"></a>00247 
<a name="l00248"></a>00248 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00249"></a>00249 
<a name="l00250"></a>00250 <span class="comment">// includes</span>
<a name="l00251"></a>00251 
<a name="l00252"></a>00252 <span class="comment">// common configuration options &amp; declarations</span>
<a name="l00253"></a>00253 <span class="preprocessor">#include &quot;<a class="code" href="config_8h.html" title="Common configuration options &amp;amp; declarations.">config.h</a>&quot;</span> <span class="comment">// always include first</span>
<a name="l00254"></a>00254 
<a name="l00255"></a>00255 <span class="comment">// C language includes</span>
<a name="l00256"></a>00256 <span class="preprocessor">#include &lt;assert.h&gt;</span>
<a name="l00257"></a>00257 <span class="preprocessor">#include &lt;stdlib.h&gt;</span> <span class="comment">// abort</span>
<a name="l00258"></a>00258 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00259"></a>00259 
<a name="l00260"></a>00260 <span class="comment">// Windows-specific includes</span>
<a name="l00261"></a>00261 <span class="preprocessor">#if PLATFORM(Windows)</span>
<a name="l00262"></a>00262 <span class="preprocessor"></span><span class="preprocessor">#pragma warning(disable: 4115)</span>
<a name="l00263"></a>00263 <span class="preprocessor"></span><span class="preprocessor">#include &lt;windows.h&gt;</span>
<a name="l00264"></a>00264 <span class="preprocessor">#endif</span>
<a name="l00265"></a>00265 <span class="preprocessor"></span>
<a name="l00266"></a>00266 <span class="comment">// Linux-specific includes</span>
<a name="l00267"></a>00267 <span class="preprocessor">#if PLATFORM(Linux)</span>
<a name="l00268"></a>00268 <span class="preprocessor"></span><span class="preprocessor">#include &lt;arpa/inet.h&gt;</span>  <span class="comment">// inet_ntoa</span>
<a name="l00269"></a>00269 <span class="preprocessor"> #endif</span>
<a name="l00270"></a>00270 <span class="preprocessor"></span>
<a name="l00271"></a>00271 <span class="comment">// framework includes</span>
<a name="l00272"></a>00272 <span class="preprocessor">#include &quot;<a class="code" href="Client_8h.html" title="Public interface for class Client.">Client.h</a>&quot;</span>        <span class="comment">/* client_xxx functions  */</span>
<a name="l00273"></a>00273 <span class="preprocessor">#include &quot;<a class="code" href="Socket_8h.html" title="Public interface for the Socket facility.">Socket.h</a>&quot;</span>        <span class="comment">/* socket_xxx functions  */</span>
<a name="l00274"></a>00274 <span class="preprocessor">#include &quot;<a class="code" href="Log_8h.html" title="Public interface for the Log facility.">util/Log.h</a>&quot;</span>      <span class="comment">/* log_xxx functions     */</span>
<a name="l00275"></a>00275 <span class="preprocessor">#include &quot;<a class="code" href="Timeout_8h.html" title="Public interface for the Timeout facility.">util/Timeout.h</a>&quot;</span>  <span class="comment">/* timeout_xxx functions */</span>
<a name="l00276"></a>00276 
<a name="l00277"></a>00277 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00278"></a>00278 
<a name="l00279"></a>00279 <span class="comment">// global declarations</span>
<a name="l00280"></a>00280 <span class="comment"></span>
<a name="l00281"></a>00281 <span class="comment">/// Client class.</span>
<a name="l00282"></a>00282 <span class="comment">/// This class provides convenience members and methods to write simple client</span>
<a name="l00283"></a>00283 <span class="comment">/// applications.</span>
<a name="l00284"></a>00284 <span class="comment">/// @class Client</span>
<a name="l00285"></a>00285 <span class="comment"></span><span class="comment"></span>
<a name="l00286"></a>00286 <span class="comment">/// Identification for the Log file</span>
<a name="l00287"></a>00287 <span class="comment">/// @private @memberof Client</span>
<a name="l00288"></a><a class="code" href="classClient.html#a7e2c66ccc38864c00befd65cefa28c00">00288</a> <span class="comment"></span><span class="keyword">static</span> <a class="code" href="config_8h.html#a0f128c3e0c83721af0220c00554e9c4e" title="const char abbreviation">cchar</a> <a class="code" href="classClient.html#a7e2c66ccc38864c00befd65cefa28c00" title="Identification for the Log file.">sourceID</a>[] = <span class="stringliteral">&quot;ZCL&quot;</span>;
<a name="l00289"></a>00289 <span class="comment"></span>
<a name="l00290"></a>00290 <span class="comment">/// Global counter used when sending messages</span>
<a name="l00291"></a>00291 <span class="comment">/// @private @memberof Client</span>
<a name="l00292"></a><a class="code" href="classClient.html#a258ded446f21a1edd363ab196974c202">00292</a> <span class="comment"></span><span class="keyword">static</span> <a class="code" href="config_8h.html#a91ad9478d81a7aaf2593e8d9c3d06a14" title="unsigned int abbreviation">uint</a> <a class="code" href="classClient.html#a258ded446f21a1edd363ab196974c202" title="Global counter used when sending messages.">orgSeqNo</a>;
<a name="l00293"></a>00293 <span class="comment"></span>
<a name="l00294"></a>00294 <span class="comment">/// Default server IP address in binary (127.1)</span>
<a name="l00295"></a>00295 <span class="comment">/// @private @memberof Client</span>
<a name="l00296"></a><a class="code" href="classClient.html#a6dee50ddcd3fa9bd16c33ea160d15ae6">00296</a> <span class="comment"></span><span class="keyword">static</span> <a class="code" href="config_8h.html#a91ad9478d81a7aaf2593e8d9c3d06a14" title="unsigned int abbreviation">uint</a> <a class="code" href="classClient.html#a6dee50ddcd3fa9bd16c33ea160d15ae6" title="Default server IP address in binary (127.1).">serverAddress</a> = 0x7F000001;
<a name="l00297"></a>00297 <span class="comment"></span>
<a name="l00298"></a>00298 <span class="comment">/// Default server IP address as string</span>
<a name="l00299"></a>00299 <span class="comment">/// @private @memberof Client</span>
<a name="l00300"></a><a class="code" href="classClient.html#ab60a34da8654480c6bba7ac1df2da8c3">00300</a> <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">char</span> <a class="code" href="classClient.html#ab60a34da8654480c6bba7ac1df2da8c3" title="Default server IP address as string.">serverAddressStr</a>[50] = <span class="stringliteral">&quot;127.1&quot;</span>;
<a name="l00301"></a>00301 <span class="comment"></span>
<a name="l00302"></a>00302 <span class="comment">/// Default server TCP port</span>
<a name="l00303"></a>00303 <span class="comment">/// @private @memberof Client</span>
<a name="l00304"></a><a class="code" href="classClient.html#ac289967d57409c9bfe8d380f777bca9e">00304</a> <span class="comment"></span><span class="keyword">static</span> <a class="code" href="config_8h.html#ab95f123a6c9bcfee6a343170ef8c5f69" title="unsigned short abbreviation">ushort</a> <a class="code" href="classClient.html#ac289967d57409c9bfe8d380f777bca9e" title="Default server TCP port.">servicePort</a> = 4000;
<a name="l00305"></a>00305 <span class="comment"></span>
<a name="l00306"></a>00306 <span class="comment">/// The client socket</span>
<a name="l00307"></a>00307 <span class="comment">/// @private @memberof Client</span>
<a name="l00308"></a><a class="code" href="classClient.html#adc5475d507e3d713ad405bbeb6eb41e7">00308</a> <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classClient.html#adc5475d507e3d713ad405bbeb6eb41e7" title="The client socket.">clientSocket</a>;
<a name="l00309"></a>00309 <span class="comment"></span>
<a name="l00310"></a>00310 <span class="comment">/// Default server reply timeout in seconds.</span>
<a name="l00311"></a>00311 <span class="comment">/// (0 means immediate, -1 means infinite).</span>
<a name="l00312"></a>00312 <span class="comment">/// @private @memberof Client</span>
<a name="l00313"></a><a class="code" href="classClient.html#a337c42b5e32a1f92420bd21afa6c1f85">00313</a> <span class="comment"></span><span class="keyword">static</span> <a class="code" href="config_8h.html#a91ad9478d81a7aaf2593e8d9c3d06a14" title="unsigned int abbreviation">uint</a> <a class="code" href="classClient.html#a337c42b5e32a1f92420bd21afa6c1f85" title="Default server reply timeout in seconds.">replyTimeout</a> = <a class="code" href="Timeout_8h.html#a3b78ef6eaf8bf61492b4f4d89bd5181c" title="No timeout.">TIMEOUT_WAIT_FOREVER</a>;
<a name="l00314"></a>00314 <span class="comment"></span>
<a name="l00315"></a>00315 <span class="comment">/// Default timeout in seconds when sending a message to the server.</span>
<a name="l00316"></a>00316 <span class="comment">/// In principle the client doesn&#39;t need to mess with this, but it&#39;s here anyway</span>
<a name="l00317"></a>00317 <span class="comment">/// @private @memberof Client</span>
<a name="l00318"></a><a class="code" href="classClient.html#ac6ebab6dbd29d69c58e999ec0273f074">00318</a> <span class="comment"></span><span class="keyword">static</span> <a class="code" href="config_8h.html#ab95f123a6c9bcfee6a343170ef8c5f69" title="unsigned short abbreviation">ushort</a> <a class="code" href="classClient.html#ac6ebab6dbd29d69c58e999ec0273f074" title="Default timeout in seconds when sending a message to the server.">sendTimeout</a> = 5;
<a name="l00319"></a>00319 <span class="comment"></span>
<a name="l00320"></a>00320 <span class="comment">/// State of the connection</span>
<a name="l00321"></a>00321 <span class="comment">/// @private @memberof Client</span>
<a name="l00322"></a><a class="code" href="classClient.html#ada1a895a5ff67fb36ed9a4765f2274d2">00322</a> <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classClient.html#ada1a895a5ff67fb36ed9a4765f2274d2" title="State of the connection.">isConnected</a>;
<a name="l00323"></a>00323 
<a name="l00324"></a>00324 <span class="comment">// ---------------</span>
<a name="l00325"></a>00325 
<a name="l00326"></a>00326 <span class="comment">// State of the client, valid only immediately after a socket operation</span>
<a name="l00327"></a>00327 <span class="comment"></span>
<a name="l00328"></a>00328 <span class="comment">/// Tells if the connection has been closed after an operation end in error.</span>
<a name="l00329"></a>00329 <span class="comment">/// @private @memberof Client</span>
<a name="l00330"></a><a class="code" href="classClient.html#ad3973a11a1a20211111f43cb17b5ee31">00330</a> <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classClient.html#ad3973a11a1a20211111f43cb17b5ee31" title="Tells if the connection has been closed after an operation end in error.">connectionClosed</a>;  
<a name="l00331"></a>00331 <span class="comment"></span>
<a name="l00332"></a>00332 <span class="comment">/// Tells if there has been a format error on the wire right after a receive</span>
<a name="l00333"></a>00333 <span class="comment">/// operation.</span>
<a name="l00334"></a>00334 <span class="comment">/// @private @memberof Client</span>
<a name="l00335"></a><a class="code" href="classClient.html#ada0babaa8c1187b1c28fac649bb6ecef">00335</a> <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classClient.html#ada0babaa8c1187b1c28fac649bb6ecef" title="Tells if there has been a format error on the wire right after a receive operation...">formatError</a>;
<a name="l00336"></a>00336 <span class="comment"></span>
<a name="l00337"></a>00337 <span class="comment">/// Contains the number of the error that occurred immediately after a socket</span>
<a name="l00338"></a>00338 <span class="comment">/// operation.                                                                     </span>
<a name="l00339"></a>00339 <span class="comment">/// @private @memberof Client</span>
<a name="l00340"></a><a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a">00340</a> <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a>;
<a name="l00341"></a>00341 <span class="comment"></span>
<a name="l00342"></a>00342 <span class="comment">/// Contains the type of the error that occurred immediately after a socket</span>
<a name="l00343"></a>00343 <span class="comment">/// operation. </span>
<a name="l00344"></a>00344 <span class="comment">/// @private @memberof Client</span>
<a name="l00345"></a><a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302">00345</a> <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a>;
<a name="l00346"></a>00346 
<a name="l00347"></a>00347 <span class="comment">// private interface with GenericClient (sync if changed in either place)</span>
<a name="l00348"></a>00348 <span class="comment">// see also client_errorType</span>
<a name="l00349"></a>00349 <span class="keyword">enum</span> <span class="comment">// for lastErrorType</span>
<a name="l00350"></a>00350 {
<a name="l00351"></a><a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55ba52dc8b1c53f593e8e1ed2da20b996a92">00351</a>    <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55ba52dc8b1c53f593e8e1ed2da20b996a92">ERRTYPE_CLOSED</a> = 1  ,  <span class="comment">// connection closed by server</span>
<a name="l00352"></a><a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55ba4617be5fd3ac8450ebbb6e5bfc4e17e7">00352</a>    <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55ba4617be5fd3ac8450ebbb6e5bfc4e17e7">ERRTYPE_SOCKET</a>      ,  <span class="comment">// socket error</span>
<a name="l00353"></a><a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55bac3981c1a31844f03343a22bc511e4f4b">00353</a>    <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55bac3981c1a31844f03343a22bc511e4f4b">ERRTYPE_FORMAT</a>      ,  <span class="comment">// invalid format on the wire</span>
<a name="l00354"></a><a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55babd24a4212144b1a877f17b8631302e1a">00354</a>    <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55babd24a4212144b1a877f17b8631302e1a">ERRTYPE_SENDTIMEOUT</a> ,  <span class="comment">// timeout in send</span>
<a name="l00355"></a>00355 };
<a name="l00356"></a>00356 <span class="comment"></span>
<a name="l00357"></a>00357 <span class="comment">/// Tells id there has been a timeout after a receive operations ends in</span>
<a name="l00358"></a>00358 <span class="comment">/// error.</span>
<a name="l00359"></a>00359 <span class="comment">/// @private @memberof Client</span>
<a name="l00360"></a><a class="code" href="classClient.html#ae6e3c294a22e2bdcc8a4893e193ce7d3">00360</a> <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classClient.html#ae6e3c294a22e2bdcc8a4893e193ce7d3" title="Tells id there has been a timeout after a receive operations ends in error.">timeoutOccurred</a>;
<a name="l00361"></a>00361 
<a name="l00362"></a>00362 <span class="comment">// ---------------</span>
<a name="l00363"></a>00363 <span class="comment"></span>
<a name="l00364"></a>00364 <span class="comment">/// Tells if tracing is enabled.</span>
<a name="l00365"></a>00365 <span class="comment">/// @private @memberof Client</span>
<a name="l00366"></a><a class="code" href="classClient.html#aede8ee1d904b3abd665f17607902ed4a">00366</a> <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">bool</span> <a class="code" href="classClient.html#aede8ee1d904b3abd665f17607902ed4a" title="Tells if tracing is enabled.">isTracing</a>;
<a name="l00367"></a>00367 
<a name="l00368"></a>00368 <span class="comment">// ---------------</span>
<a name="l00369"></a>00369 
<a name="l00370"></a>00370 <span class="comment">// internal helper functions</span><span class="comment"></span>
<a name="l00371"></a>00371 <span class="comment">/// @cond hides_from_doxygen</span>
<a name="l00372"></a>00372 <span class="comment"></span><span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classClient.html#ac1a0ff1d1c2453cc22c028da29fcb115" title="Receives a specific-length buffer from peer application, and checks for error.">recvBuffer</a>(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>*, <span class="keywordtype">int</span>, <a class="code" href="config_8h.html#a91ad9478d81a7aaf2593e8d9c3d06a14" title="unsigned int abbreviation">uint</a>, <span class="keywordtype">bool</span>);<span class="comment"></span>
<a name="l00373"></a>00373 <span class="comment">/// @endcond</span>
<a name="l00374"></a>00374 <span class="comment"></span>
<a name="l00375"></a>00375 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00376"></a>00376 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00377"></a>00377 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00378"></a>00378 <span class="comment"></span>
<a name="l00379"></a>00379 <span class="comment">/** Closes the connection.</span>
<a name="l00380"></a>00380 <span class="comment"></span>
<a name="l00381"></a>00381 <span class="comment">    @memberof Client</span>
<a name="l00382"></a>00382 <span class="comment">*/</span>
<a name="l00383"></a>00383 
<a name="l00384"></a><a class="code" href="classClient.html#ac1b8a1bcc592b90be708f53c6f3379c8">00384</a> TC2API <span class="keywordtype">void</span> <a class="code" href="classClient.html#ac1b8a1bcc592b90be708f53c6f3379c8" title="Closes the connection.">client_closeConnection</a>(<span class="keywordtype">void</span>)
<a name="l00385"></a>00385 {
<a name="l00386"></a>00386    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#ac1b8a1bcc592b90be708f53c6f3379c8" title="Closes the connection.">client_closeConnection</a>);
<a name="l00387"></a>00387    <a class="code" href="Log_8h.html#a619ac1ab71ab5d1c7e94e03ddbf16d3b" title="log informational record with function identification (see Log::log_fheader)">log_finfo</a>(<span class="stringliteral">&quot;closing the connection with the server&quot;</span>);
<a name="l00388"></a>00388 
<a name="l00389"></a>00389    socket_close(<a class="code" href="classClient.html#adc5475d507e3d713ad405bbeb6eb41e7" title="The client socket.">clientSocket</a>);
<a name="l00390"></a>00390    <a class="code" href="classClient.html#ada1a895a5ff67fb36ed9a4765f2274d2" title="State of the connection.">isConnected</a> = <span class="keyword">false</span>;
<a name="l00391"></a>00391 }
<a name="l00392"></a>00392 
<a name="l00393"></a>00393 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00394"></a>00394 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00395"></a>00395 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00396"></a>00396 <span class="comment"></span>
<a name="l00397"></a>00397 <span class="comment">/** Tries to connect with the server.</span>
<a name="l00398"></a>00398 <span class="comment"></span>
<a name="l00399"></a>00399 <span class="comment">    @memberof Client</span>
<a name="l00400"></a>00400 <span class="comment">*/</span>
<a name="l00401"></a>00401 
<a name="l00402"></a><a class="code" href="classClient.html#ab47c0c9a394979f9a438f0f57598b53c">00402</a> TC2API <span class="keywordtype">bool</span> <a class="code" href="classClient.html#ab47c0c9a394979f9a438f0f57598b53c" title="Tries to connect with the server.">client_connect</a>(<span class="keywordtype">void</span>)
<a name="l00403"></a>00403 {
<a name="l00404"></a>00404    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#ab47c0c9a394979f9a438f0f57598b53c" title="Tries to connect with the server.">client_connect</a>);
<a name="l00405"></a>00405    <a class="code" href="Log_8h.html#a619ac1ab71ab5d1c7e94e03ddbf16d3b" title="log informational record with function identification (see Log::log_fheader)">log_finfo</a>(<span class="stringliteral">&quot;trying to connect with the server %s&quot;</span>, <a class="code" href="classClient.html#ab60a34da8654480c6bba7ac1df2da8c3" title="Default server IP address as string.">serverAddressStr</a>);
<a name="l00406"></a>00406 
<a name="l00407"></a>00407    <a class="code" href="classClient.html#ada1a895a5ff67fb36ed9a4765f2274d2" title="State of the connection.">isConnected</a> = <span class="keyword">false</span>;
<a name="l00408"></a>00408 
<a name="l00409"></a>00409    <span class="keywordflow">if</span> (<a class="code" href="classSocket.html#a21a9ff688bda980aa3154c16069287f2" title="Connects a client socket with a server.">socket_connect</a>(<a class="code" href="classClient.html#adc5475d507e3d713ad405bbeb6eb41e7" title="The client socket.">clientSocket</a>, <a class="code" href="classClient.html#a6dee50ddcd3fa9bd16c33ea160d15ae6" title="Default server IP address in binary (127.1).">serverAddress</a>, <a class="code" href="classClient.html#ac289967d57409c9bfe8d380f777bca9e" title="Default server TCP port.">servicePort</a>))
<a name="l00410"></a>00410    {
<a name="l00411"></a>00411       <a class="code" href="Log_8h.html#a619ac1ab71ab5d1c7e94e03ddbf16d3b" title="log informational record with function identification (see Log::log_fheader)">log_finfo</a>(<span class="stringliteral">&quot;ok, connection successful&quot;</span>);
<a name="l00412"></a>00412       <a class="code" href="classClient.html#ada1a895a5ff67fb36ed9a4765f2274d2" title="State of the connection.">isConnected</a> = <span class="keyword">true</span>;
<a name="l00413"></a>00413       <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00414"></a>00414    }
<a name="l00415"></a>00415 
<a name="l00416"></a>00416    <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a> = socket_error();
<a name="l00417"></a>00417 
<a name="l00418"></a>00418    <a class="code" href="Log_8h.html#aa62b9042f25fb0b8205e108ed438a1a6" title="log error record with function identification (see Log::log_fheader)">log_ferror</a>(<span class="stringliteral">&quot;error %d trying to connect with the server&quot;</span>, <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a>);
<a name="l00419"></a>00419    <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00420"></a>00420 }
<a name="l00421"></a>00421 
<a name="l00422"></a>00422 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00423"></a>00423 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00424"></a>00424 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00425"></a>00425 <span class="comment"></span>
<a name="l00426"></a>00426 <span class="comment">/** Tells if the connection has been closed right after an operation</span>
<a name="l00427"></a>00427 <span class="comment">    (send/receive) ends in error.</span>
<a name="l00428"></a>00428 <span class="comment"></span>
<a name="l00429"></a>00429 <span class="comment">    @return</span>
<a name="l00430"></a>00430 <span class="comment">    true (connection has been closed) / false (connection has not been closed)</span>
<a name="l00431"></a>00431 <span class="comment"></span>
<a name="l00432"></a>00432 <span class="comment">    @memberof Client</span>
<a name="l00433"></a>00433 <span class="comment">*/</span>
<a name="l00434"></a>00434 
<a name="l00435"></a><a class="code" href="classClient.html#a59e06c6b46755936dc38237c8d2c4598">00435</a> TC2API <span class="keywordtype">bool</span> <a class="code" href="classClient.html#a59e06c6b46755936dc38237c8d2c4598" title="Tells if the connection has been closed right after an operation (send/receive) ends...">client_connectionClosed</a>(<span class="keywordtype">void</span>)
<a name="l00436"></a>00436 {
<a name="l00437"></a>00437    <span class="keywordflow">return</span> <a class="code" href="classClient.html#ad3973a11a1a20211111f43cb17b5ee31" title="Tells if the connection has been closed after an operation end in error.">connectionClosed</a>;
<a name="l00438"></a>00438 }
<a name="l00439"></a>00439 
<a name="l00440"></a>00440 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00441"></a>00441 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00442"></a>00442 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00443"></a>00443 <span class="comment"></span>
<a name="l00444"></a>00444 <span class="comment">/** Creates the client socket.</span>
<a name="l00445"></a>00445 <span class="comment"></span>
<a name="l00446"></a>00446 <span class="comment">    @post</span>
<a name="l00447"></a>00447 <span class="comment">    either succeeds or aborts the application</span>
<a name="l00448"></a>00448 <span class="comment"></span>
<a name="l00449"></a>00449 <span class="comment">    @memberof Client</span>
<a name="l00450"></a>00450 <span class="comment">*/</span>
<a name="l00451"></a>00451 
<a name="l00452"></a><a class="code" href="classClient.html#a2cb4ba780f0333535bccaf7c179909ab">00452</a> TC2API <span class="keywordtype">void</span> <a class="code" href="classClient.html#a2cb4ba780f0333535bccaf7c179909ab" title="Creates the client socket.">client_createClientSocket</a>(<span class="keywordtype">void</span>)
<a name="l00453"></a>00453 {
<a name="l00454"></a>00454    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#a2cb4ba780f0333535bccaf7c179909ab" title="Creates the client socket.">client_createClientSocket</a>);
<a name="l00455"></a>00455    <a class="code" href="Log_8h.html#a619ac1ab71ab5d1c7e94e03ddbf16d3b" title="log informational record with function identification (see Log::log_fheader)">log_finfo</a>(<span class="stringliteral">&quot;creating the client socket&quot;</span>);
<a name="l00456"></a>00456 
<a name="l00457"></a>00457    <a class="code" href="classClient.html#adc5475d507e3d713ad405bbeb6eb41e7" title="The client socket.">clientSocket</a> = <a class="code" href="classSocket.html#a626d07f2d347c9695c03ee56d5abae57" title="Creates a client TCP socket.">socket_createClientSocket</a>();
<a name="l00458"></a>00458 
<a name="l00459"></a>00459 <span class="comment">// don&#39;t do it for now </span>
<a name="l00460"></a>00460 <span class="comment">// socket_setNonBlocking(clientSocket);</span>
<a name="l00461"></a>00461 }
<a name="l00462"></a>00462 
<a name="l00463"></a>00463 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00464"></a>00464 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00465"></a>00465 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00466"></a>00466 <span class="comment"></span>
<a name="l00467"></a>00467 <span class="comment">/** Retrieves the value of the error that occurred immediately after a socket</span>
<a name="l00468"></a>00468 <span class="comment">    operation.</span>
<a name="l00469"></a>00469 <span class="comment"></span>
<a name="l00470"></a>00470 <span class="comment">    @return</span>
<a name="l00471"></a>00471 <span class="comment">    number of error that occurred immediately after a socket operation</span>
<a name="l00472"></a>00472 <span class="comment"></span>
<a name="l00473"></a>00473 <span class="comment">    @memberof Client</span>
<a name="l00474"></a>00474 <span class="comment">*/</span>
<a name="l00475"></a>00475 
<a name="l00476"></a><a class="code" href="classClient.html#a7b91cbaab1c8ebdaa38caed1f50b050a">00476</a> TC2API <a class="code" href="config_8h.html#a91ad9478d81a7aaf2593e8d9c3d06a14" title="unsigned int abbreviation">uint</a> <a class="code" href="classClient.html#a7b91cbaab1c8ebdaa38caed1f50b050a" title="Retrieves the value of the error that occurred immediately after a socket operation...">client_error</a>(<span class="keywordtype">void</span>)
<a name="l00477"></a>00477 {
<a name="l00478"></a>00478    <span class="keywordflow">return</span> <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a>;
<a name="l00479"></a>00479 }
<a name="l00480"></a>00480 
<a name="l00481"></a>00481 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00482"></a>00482 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00483"></a>00483 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00484"></a>00484 <span class="comment"></span>
<a name="l00485"></a>00485 <span class="comment">/** Retrieves the type of the error that occurred immediately after a socket</span>
<a name="l00486"></a>00486 <span class="comment">    operation.</span>
<a name="l00487"></a>00487 <span class="comment"></span>
<a name="l00488"></a>00488 <span class="comment">    @return</span>
<a name="l00489"></a>00489 <span class="comment">    type of the error (only for use of the class GenericClient)</span>
<a name="l00490"></a>00490 <span class="comment"></span>
<a name="l00491"></a>00491 <span class="comment">    @memberof Client</span>
<a name="l00492"></a>00492 <span class="comment">*/</span>
<a name="l00493"></a>00493 
<a name="l00494"></a><a class="code" href="classClient.html#a800b9dc4b73f58fa14a0f124f143f2d5">00494</a> TC2API <a class="code" href="config_8h.html#ab95f123a6c9bcfee6a343170ef8c5f69" title="unsigned short abbreviation">ushort</a> <a class="code" href="classClient.html#a800b9dc4b73f58fa14a0f124f143f2d5" title="Retrieves the type of the error that occurred immediately after a socket operation...">client_errorType</a>(<span class="keywordtype">void</span>)
<a name="l00495"></a>00495 {
<a name="l00496"></a>00496    <span class="keywordflow">return</span> <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a>;
<a name="l00497"></a>00497 }
<a name="l00498"></a>00498 
<a name="l00499"></a>00499 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00500"></a>00500 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00501"></a>00501 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00502"></a>00502 <span class="comment"></span>
<a name="l00503"></a>00503 <span class="comment">/** Reports if there has been a format error on the wire right after a receive</span>
<a name="l00504"></a>00504 <span class="comment">    operation.</span>
<a name="l00505"></a>00505 <span class="comment"></span>
<a name="l00506"></a>00506 <span class="comment">    @return</span>
<a name="l00507"></a>00507 <span class="comment">    true (a format error occurred) / false (a format error not occurred)</span>
<a name="l00508"></a>00508 <span class="comment"></span>
<a name="l00509"></a>00509 <span class="comment">    @memberof Client</span>
<a name="l00510"></a>00510 <span class="comment">*/</span>
<a name="l00511"></a>00511 
<a name="l00512"></a><a class="code" href="classClient.html#a802927ed10beeb0aa3034baba21abf14">00512</a> TC2API <span class="keywordtype">bool</span> <a class="code" href="classClient.html#a802927ed10beeb0aa3034baba21abf14" title="Reports if there has been a format error on the wire right after a receive operation...">client_formatError</a>(<span class="keywordtype">void</span>)
<a name="l00513"></a>00513 {
<a name="l00514"></a>00514    <span class="keywordflow">return</span> <a class="code" href="classClient.html#ada0babaa8c1187b1c28fac649bb6ecef" title="Tells if there has been a format error on the wire right after a receive operation...">formatError</a>;
<a name="l00515"></a>00515 }
<a name="l00516"></a>00516 
<a name="l00517"></a>00517 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00518"></a>00518 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00519"></a>00519 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00520"></a>00520 <span class="comment"></span>
<a name="l00521"></a>00521 <span class="comment">/** Initializes the Client façade class.</span>
<a name="l00522"></a>00522 <span class="comment"></span>
<a name="l00523"></a>00523 <span class="comment">    @memberof Client</span>
<a name="l00524"></a>00524 <span class="comment">*/</span>
<a name="l00525"></a>00525 
<a name="l00526"></a><a class="code" href="classClient.html#a1b7cef854eb0b6febc2f315def5e4c22">00526</a> TC2API <span class="keywordtype">void</span> <a class="code" href="classClient.html#a1b7cef854eb0b6febc2f315def5e4c22" title="Initializes the Client façade class.">client_init</a>(<span class="keywordtype">void</span>)
<a name="l00527"></a>00527 {
<a name="l00528"></a>00528    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#a1b7cef854eb0b6febc2f315def5e4c22" title="Initializes the Client façade class.">client_init</a>);
<a name="l00529"></a>00529    <a class="code" href="Log_8h.html#a619ac1ab71ab5d1c7e94e03ddbf16d3b" title="log informational record with function identification (see Log::log_fheader)">log_finfo</a>(<span class="stringliteral">&quot;Client class inicialization&quot;</span>);
<a name="l00530"></a>00530 
<a name="l00531"></a>00531    <a class="code" href="classSocket.html#a8fe6ea62ef1e4d30acd1fadeaa3df74d" title="Initializes the Socket class.">socket_init</a>();
<a name="l00532"></a>00532 }
<a name="l00533"></a>00533 
<a name="l00534"></a>00534 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00535"></a>00535 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00536"></a>00536 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00537"></a>00537 <span class="comment"></span>
<a name="l00538"></a>00538 <span class="comment">/** Checks if the client is connected to the server.</span>
<a name="l00539"></a>00539 <span class="comment"></span>
<a name="l00540"></a>00540 <span class="comment">    @return</span>
<a name="l00541"></a>00541 <span class="comment">    true (is connected) / false (is not connected)</span>
<a name="l00542"></a>00542 <span class="comment"></span>
<a name="l00543"></a>00543 <span class="comment">    @memberof Client</span>
<a name="l00544"></a>00544 <span class="comment">*/</span>
<a name="l00545"></a>00545 
<a name="l00546"></a><a class="code" href="classClient.html#a01fe6e28b2b7463eb375bfd2c31a316b">00546</a> TC2API <span class="keywordtype">bool</span> <a class="code" href="classClient.html#a01fe6e28b2b7463eb375bfd2c31a316b" title="Checks if the client is connected to the server.">client_isConnected</a>(<span class="keywordtype">void</span>)
<a name="l00547"></a>00547 {
<a name="l00548"></a>00548    <span class="keywordflow">return</span> <a class="code" href="classClient.html#ada1a895a5ff67fb36ed9a4765f2274d2" title="State of the connection.">isConnected</a>;
<a name="l00549"></a>00549 }
<a name="l00550"></a>00550 
<a name="l00551"></a>00551 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00552"></a>00552 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00553"></a>00553 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00554"></a>00554 <span class="comment"></span>
<a name="l00555"></a>00555 <span class="comment">/** Receives a message from the server.</span>
<a name="l00556"></a>00556 <span class="comment"></span>
<a name="l00557"></a>00557 <span class="comment">    @param</span>
<a name="l00558"></a>00558 <span class="comment">    msg : address of a Message structure which will be used to store data</span>
<a name="l00559"></a>00559 <span class="comment">    received from the server</span>
<a name="l00560"></a>00560 <span class="comment"></span>
<a name="l00561"></a>00561 <span class="comment">    @return</span>
<a name="l00562"></a>00562 <span class="comment">    true (reception successful) / false (reception failure)</span>
<a name="l00563"></a>00563 <span class="comment"></span>
<a name="l00564"></a>00564 <span class="comment">    @memberof Client</span>
<a name="l00565"></a>00565 <span class="comment">*/</span>
<a name="l00566"></a>00566 
<a name="l00567"></a><a class="code" href="classClient.html#a21671dd01411a1478cc6d2ad4eeba468">00567</a> TC2API <span class="keywordtype">bool</span> <a class="code" href="classClient.html#a21671dd01411a1478cc6d2ad4eeba468" title="Receives a message from the server.">client_recvMessage</a>(<a class="code" href="structMessage.html" title="Message data structure.">Message</a>* msg)
<a name="l00568"></a>00568 {
<a name="l00569"></a>00569    <span class="keywordtype">int</span> ret;
<a name="l00570"></a>00570    <a class="code" href="structTimeout.html" title="Structure representing timeout.">Timeout</a> timeout;
<a name="l00571"></a>00571    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#a21671dd01411a1478cc6d2ad4eeba468" title="Receives a message from the server.">client_recvMessage</a>);
<a name="l00572"></a>00572    <span class="keyword">static</span> <a class="code" href="config_8h.html#a0f128c3e0c83721af0220c00554e9c4e" title="const char abbreviation">cchar</a> msgclose[] = <span class="stringliteral">&quot;now closing connection&quot;</span>;
<a name="l00573"></a>00573 <span class="comment"></span>
<a name="l00574"></a>00574 <span class="comment">   /// @cond hides_from_doxygen</span>
<a name="l00575"></a>00575 <span class="comment"></span><span class="preprocessor">   #define REM_TIME timeout_remaining(&amp;timeout)</span>
<a name="l00576"></a>00576 <span class="preprocessor"></span><span class="comment">   /// @endcond</span>
<a name="l00577"></a>00577 <span class="comment"></span>
<a name="l00578"></a>00578    assert(msg);
<a name="l00579"></a>00579 
<a name="l00580"></a>00580    <a class="code" href="structTimeout.html#a3c9170878cfae51d9dbf162d3fdcde55" title="Initializes a Timeout structure.">timeout_init</a>(&amp;timeout, <a class="code" href="classClient.html#a337c42b5e32a1f92420bd21afa6c1f85" title="Default server reply timeout in seconds.">replyTimeout</a>);
<a name="l00581"></a>00581 
<a name="l00582"></a>00582    <span class="comment">// tries to receive the message&#39;s first part</span>
<a name="l00583"></a>00583    <a class="code" href="Log_8h.html#ab4478c2e145cf2707099a4676a81a2d6" title="log debug record with function identification (see Log::log_fheader)">log_fdebug</a>(<span class="stringliteral">&quot;trying to receive %d bytes (first part of message)&quot;</span>,
<a name="l00584"></a>00584       <a class="code" href="structMessage.html#ab2c30f86b55850b3e0859fc37b9e6aa1" title="Retrieves the size of the part 1 of a Message being received.">message_size1</a>());
<a name="l00585"></a>00585 
<a name="l00586"></a>00586    ret = <a class="code" href="classClient.html#ac1a0ff1d1c2453cc22c028da29fcb115" title="Receives a specific-length buffer from peer application, and checks for error.">recvBuffer</a>(<a class="code" href="classClient.html#adc5475d507e3d713ad405bbeb6eb41e7" title="The client socket.">clientSocket</a>, <a class="code" href="structMessage.html#ad82725c7d518ca0e3759d5aa1d517233" title="Retrieves the address of on-the-wire contents of this Message.">message_start</a>(msg), <a class="code" href="structMessage.html#ab2c30f86b55850b3e0859fc37b9e6aa1" title="Retrieves the size of the part 1 of a Message being received.">message_size1</a>(),
<a name="l00587"></a>00587       REM_TIME, <span class="keyword">true</span> <span class="comment">/*first part of message*/</span>);
<a name="l00588"></a>00588 
<a name="l00589"></a>00589    <span class="keywordflow">if</span> (ret != (<span class="keywordtype">int</span>)<a class="code" href="structMessage.html#ab2c30f86b55850b3e0859fc37b9e6aa1" title="Retrieves the size of the part 1 of a Message being received.">message_size1</a>())
<a name="l00590"></a>00590    {
<a name="l00591"></a>00591       <a class="code" href="Log_8h.html#aa62b9042f25fb0b8205e108ed438a1a6" title="log error record with function identification (see Log::log_fheader)">log_ferror</a>(<span class="stringliteral">&quot;error when receiving first part of message from server&quot;</span>);
<a name="l00592"></a>00592 
<a name="l00593"></a>00593       <span class="comment">// if timeout and not one byte has arrived then don&#39;t close connection</span>
<a name="l00594"></a>00594       <span class="keywordflow">if</span> (<a class="code" href="classClient.html#ae6e3c294a22e2bdcc8a4893e193ce7d3" title="Tells id there has been a timeout after a receive operations ends in error.">timeoutOccurred</a> &amp;&amp; (<a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> != <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55bac3981c1a31844f03343a22bc511e4f4b">ERRTYPE_FORMAT</a>))
<a name="l00595"></a>00595          <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00596"></a>00596       <span class="keywordflow">goto</span> ERROR_WITH_CLOSE;
<a name="l00597"></a>00597    }
<a name="l00598"></a>00598 
<a name="l00599"></a>00599    <span class="comment">// consistency check</span>
<a name="l00600"></a>00600    <span class="keywordflow">if</span> (!<a class="code" href="structMessage.html#a8813f1306e704c9c7b59cc25b104f418" title="Checks if the Message prefix is valid.">message_isValidPrefix</a>(msg))
<a name="l00601"></a>00601    {
<a name="l00602"></a>00602       <a class="code" href="Log_8h.html#aa62b9042f25fb0b8205e108ed438a1a6" title="log error record with function identification (see Log::log_fheader)">log_ferror</a>(<span class="stringliteral">&quot;invalid message prefix [%c] received from server&quot;</span>,
<a name="l00603"></a>00603          <a class="code" href="structMessage.html#a8f0de8a9e37b91bc8f3855950a581cc9" title="Retrieves the Message prefix.">message_prefix</a>(msg));
<a name="l00604"></a>00604       <span class="keywordflow">goto</span> FORMAT_ERROR_WITH_CLOSE;
<a name="l00605"></a>00605    }
<a name="l00606"></a>00606 
<a name="l00607"></a>00607    <span class="comment">// consistency check</span>
<a name="l00608"></a>00608    <span class="keywordflow">if</span> (<a class="code" href="structMessage.html#a752d83635cc98231e3e5f96b0fecba24" title="Retrieves the size of the data area of this Message.">message_size</a>(msg) &gt; <a class="code" href="structMessage.html#ad79085024874f86561123dba96ae34ae" title="Retrieves the value configured for the maximum data area size of a message.">message_maxBodySize</a>())
<a name="l00609"></a>00609    {
<a name="l00610"></a>00610       <a class="code" href="Log_8h.html#aa62b9042f25fb0b8205e108ed438a1a6" title="log error record with function identification (see Log::log_fheader)">log_ferror</a>(<span class="stringliteral">&quot;invalid message size [%d] received from server&quot;</span>,
<a name="l00611"></a>00611           <a class="code" href="structMessage.html#a752d83635cc98231e3e5f96b0fecba24" title="Retrieves the size of the data area of this Message.">message_size</a>(msg));
<a name="l00612"></a>00612       <span class="keywordflow">goto</span> FORMAT_ERROR_WITH_CLOSE;
<a name="l00613"></a>00613    }
<a name="l00614"></a>00614 
<a name="l00615"></a>00615    <span class="comment">// tries to receive the message&#39;s second part</span>
<a name="l00616"></a>00616    <a class="code" href="Log_8h.html#ab4478c2e145cf2707099a4676a81a2d6" title="log debug record with function identification (see Log::log_fheader)">log_fdebug</a>(<span class="stringliteral">&quot;trying to receive %d bytes (second part of message)&quot;</span>,
<a name="l00617"></a>00617       <a class="code" href="structMessage.html#ae5c0beb7f40308b7f2e8477dd7f3df68" title="Retrieves the size of the part 2 of a Message being received.">message_size2</a>(msg));
<a name="l00618"></a>00618 
<a name="l00619"></a>00619    ret = <a class="code" href="classClient.html#ac1a0ff1d1c2453cc22c028da29fcb115" title="Receives a specific-length buffer from peer application, and checks for error.">recvBuffer</a>(<a class="code" href="classClient.html#adc5475d507e3d713ad405bbeb6eb41e7" title="The client socket.">clientSocket</a>, <a class="code" href="structMessage.html#ad82725c7d518ca0e3759d5aa1d517233" title="Retrieves the address of on-the-wire contents of this Message.">message_start</a>(msg)+<a class="code" href="structMessage.html#ab2c30f86b55850b3e0859fc37b9e6aa1" title="Retrieves the size of the part 1 of a Message being received.">message_size1</a>(),
<a name="l00620"></a>00620       <a class="code" href="structMessage.html#ae5c0beb7f40308b7f2e8477dd7f3df68" title="Retrieves the size of the part 2 of a Message being received.">message_size2</a>(msg), REM_TIME, <span class="keyword">false</span> <span class="comment">/*not first part of message*/</span>);
<a name="l00621"></a>00621 
<a name="l00622"></a>00622    <span class="keywordflow">if</span> (ret != (<span class="keywordtype">int</span>)<a class="code" href="structMessage.html#ae5c0beb7f40308b7f2e8477dd7f3df68" title="Retrieves the size of the part 2 of a Message being received.">message_size2</a>(msg))
<a name="l00623"></a>00623    {
<a name="l00624"></a>00624       <a class="code" href="Log_8h.html#aa62b9042f25fb0b8205e108ed438a1a6" title="log error record with function identification (see Log::log_fheader)">log_ferror</a>(<span class="stringliteral">&quot;error when receiving seconf part of message from server&quot;</span>);
<a name="l00625"></a>00625       <span class="keywordflow">goto</span> ERROR_WITH_CLOSE;
<a name="l00626"></a>00626    }
<a name="l00627"></a>00627 
<a name="l00628"></a>00628    <span class="comment">// consistency check</span>
<a name="l00629"></a>00629    <span class="keywordflow">if</span> (!<a class="code" href="structMessage.html#a8524606b8cb2c28dc55892ef87425f8f" title="Checks if the Message suffix is valid.">message_isValidSuffix</a>(msg))
<a name="l00630"></a>00630    {
<a name="l00631"></a>00631       <a class="code" href="Log_8h.html#aa62b9042f25fb0b8205e108ed438a1a6" title="log error record with function identification (see Log::log_fheader)">log_ferror</a>(<span class="stringliteral">&quot;invalid message suffix [%c] received from server&quot;</span>,
<a name="l00632"></a>00632          <a class="code" href="structMessage.html#ab5c8cf6a29744dcbf5358f47235f8939" title="Retrives the message suffix.">message_suffix</a>(msg));
<a name="l00633"></a>00633       <span class="keywordflow">goto</span> FORMAT_ERROR_WITH_CLOSE;
<a name="l00634"></a>00634    }
<a name="l00635"></a>00635 
<a name="l00636"></a>00636    <a class="code" href="Log_8h.html#ab4478c2e145cf2707099a4676a81a2d6" title="log debug record with function identification (see Log::log_fheader)">log_fdebug</a>(<span class="stringliteral">&quot;received %d bytes from server, body:%d orgId:%d orgSeq:%d&quot;</span>,
<a name="l00637"></a>00637       <a class="code" href="structMessage.html#ab2c30f86b55850b3e0859fc37b9e6aa1" title="Retrieves the size of the part 1 of a Message being received.">message_size1</a>()+<a class="code" href="structMessage.html#ae5c0beb7f40308b7f2e8477dd7f3df68" title="Retrieves the size of the part 2 of a Message being received.">message_size2</a>(msg), <a class="code" href="structMessage.html#a752d83635cc98231e3e5f96b0fecba24" title="Retrieves the size of the data area of this Message.">message_size</a>(msg),
<a name="l00638"></a>00638       <a class="code" href="structMessage.html#a02ce321c76ba95032a6894b5096893a9" title="Retrieves the source identification of this Message.">message_orgId</a>(msg), <a class="code" href="structMessage.html#a6b67b118a73c8dbcfbdf806b17812ff5" title="Retrieves the sequence number of the Message that was set by the client.">message_orgSeqNo</a>(msg));
<a name="l00639"></a>00639 
<a name="l00640"></a>00640    <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00641"></a>00641 
<a name="l00642"></a>00642 FORMAT_ERROR_WITH_CLOSE:
<a name="l00643"></a>00643    <a class="code" href="classClient.html#ada0babaa8c1187b1c28fac649bb6ecef" title="Tells if there has been a format error on the wire right after a receive operation...">formatError</a> = <span class="keyword">true</span>;
<a name="l00644"></a>00644    <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> = <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55bac3981c1a31844f03343a22bc511e4f4b">ERRTYPE_FORMAT</a>;
<a name="l00645"></a>00645 ERROR_WITH_CLOSE:
<a name="l00646"></a>00646    <a class="code" href="Log_8h.html#a58100c42301c30599a1d148e9771646d" title="log warning record with function identification (see Log::log_fheader)">log_fwarn</a>(msgclose);
<a name="l00647"></a>00647    <a class="code" href="classClient.html#ac1b8a1bcc592b90be708f53c6f3379c8" title="Closes the connection.">client_closeConnection</a>();
<a name="l00648"></a>00648    <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00649"></a>00649 }
<a name="l00650"></a>00650 
<a name="l00651"></a>00651 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00652"></a>00652 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00653"></a>00653 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00654"></a>00654 <span class="comment"></span>
<a name="l00655"></a>00655 <span class="comment">/** Tries to send a message to the server.</span>
<a name="l00656"></a>00656 <span class="comment"></span>
<a name="l00657"></a>00657 <span class="comment">    @param</span>
<a name="l00658"></a>00658 <span class="comment">    msg : address of a Message structure with the data that will be sent to the</span>
<a name="l00659"></a>00659 <span class="comment">    server</span>
<a name="l00660"></a>00660 <span class="comment"></span>
<a name="l00661"></a>00661 <span class="comment">    @return</span>
<a name="l00662"></a>00662 <span class="comment">    true (operation successful) / false (operation failed)</span>
<a name="l00663"></a>00663 <span class="comment"></span>
<a name="l00664"></a>00664 <span class="comment">    @memberof Client</span>
<a name="l00665"></a>00665 <span class="comment">*/</span>
<a name="l00666"></a>00666 
<a name="l00667"></a><a class="code" href="classClient.html#ad12e0cdab8fa5ba7aa64865ec0b23b38">00667</a> TC2API <span class="keywordtype">bool</span> <a class="code" href="classClient.html#ad12e0cdab8fa5ba7aa64865ec0b23b38" title="Tries to send a message to the server.">client_sendMessage</a>(<a class="code" href="structMessage.html" title="Message data structure.">Message</a>* msg)
<a name="l00668"></a>00668 {
<a name="l00669"></a>00669    <span class="keywordtype">int</span> ret, size;
<a name="l00670"></a>00670    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#ad12e0cdab8fa5ba7aa64865ec0b23b38" title="Tries to send a message to the server.">client_sendMessage</a>);
<a name="l00671"></a>00671 
<a name="l00672"></a>00672    assert(msg);
<a name="l00673"></a>00673 
<a name="l00674"></a>00674    <span class="comment">// to make it easier to find this message in the server&#39;s log</span>
<a name="l00675"></a>00675    <a class="code" href="structMessage.html#aab8fc2edae92cb1126c49962fa6bf5bc" title="Sets the source identification of this Message.">message_setOrgId</a>(msg, <a class="code" href="Thread_8h.html#af3c1431be1989bc994d540a5dbb584c8" title="The current process id provided by the operating system.">thread_pid</a>());
<a name="l00676"></a>00676    <a class="code" href="structMessage.html#a8e43c34b2690c17b30abb2b196a20ca1" title="Sets the local sequence number of this Message.">message_setOrgSeqNo</a>(msg, <a class="code" href="classClient.html#a258ded446f21a1edd363ab196974c202" title="Global counter used when sending messages.">orgSeqNo</a>++);
<a name="l00677"></a>00677 
<a name="l00678"></a>00678    size = <a class="code" href="structMessage.html#ab2c30f86b55850b3e0859fc37b9e6aa1" title="Retrieves the size of the part 1 of a Message being received.">message_size1</a>() + <a class="code" href="structMessage.html#ae5c0beb7f40308b7f2e8477dd7f3df68" title="Retrieves the size of the part 2 of a Message being received.">message_size2</a>(msg);
<a name="l00679"></a>00679 
<a name="l00680"></a>00680    <span class="comment">// clears indicators</span>
<a name="l00681"></a>00681    <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a> = 0;
<a name="l00682"></a>00682    <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> = 0;
<a name="l00683"></a>00683    <a class="code" href="classClient.html#ae6e3c294a22e2bdcc8a4893e193ce7d3" title="Tells id there has been a timeout after a receive operations ends in error.">timeoutOccurred</a> = <span class="keyword">false</span>;
<a name="l00684"></a>00684 
<a name="l00685"></a>00685    <span class="comment">// tries to send the message</span>
<a name="l00686"></a>00686    <a class="code" href="Log_8h.html#ab4478c2e145cf2707099a4676a81a2d6" title="log debug record with function identification (see Log::log_fheader)">log_fdebug</a>(<span class="stringliteral">&quot;sending %d bytes to server, body:%d orgId:%d orgSeq:%d&quot;</span>,
<a name="l00687"></a>00687       size, <a class="code" href="structMessage.html#a752d83635cc98231e3e5f96b0fecba24" title="Retrieves the size of the data area of this Message.">message_size</a>(msg), <a class="code" href="structMessage.html#a02ce321c76ba95032a6894b5096893a9" title="Retrieves the source identification of this Message.">message_orgId</a>(msg), <a class="code" href="structMessage.html#a6b67b118a73c8dbcfbdf806b17812ff5" title="Retrieves the sequence number of the Message that was set by the client.">message_orgSeqNo</a>(msg));
<a name="l00688"></a>00688 
<a name="l00689"></a>00689    ret = <a class="code" href="classSocket.html#a9b2b0db924dff614c6920af6b9879193" title="Sends an exact number of bytes to the peer application.">socket_sendBuffer</a>(<a class="code" href="classClient.html#adc5475d507e3d713ad405bbeb6eb41e7" title="The client socket.">clientSocket</a>, <a class="code" href="structMessage.html#ad82725c7d518ca0e3759d5aa1d517233" title="Retrieves the address of on-the-wire contents of this Message.">message_start</a>(msg), size, <a class="code" href="classClient.html#ac6ebab6dbd29d69c58e999ec0273f074" title="Default timeout in seconds when sending a message to the server.">sendTimeout</a>,
<a name="l00690"></a>00690       <a class="code" href="classClient.html#aede8ee1d904b3abd665f17607902ed4a" title="Tells if tracing is enabled.">isTracing</a>);
<a name="l00691"></a>00691 
<a name="l00692"></a>00692    <span class="keywordflow">if</span> (ret == size)
<a name="l00693"></a>00693    {
<a name="l00694"></a>00694       <a class="code" href="Log_8h.html#ab4478c2e145cf2707099a4676a81a2d6" title="log debug record with function identification (see Log::log_fheader)">log_fdebug</a>(<span class="stringliteral">&quot;ok, message sent successfully&quot;</span>);
<a name="l00695"></a>00695       <span class="keywordflow">return</span> <span class="keyword">true</span>; <span class="comment">// ok</span>
<a name="l00696"></a>00696    }
<a name="l00697"></a>00697 
<a name="l00698"></a>00698    <span class="keywordflow">if</span> (ret == <a class="code" href="Socket_8h.html#ad38171878628c67def4c5bca6a6caf76" title="Generic timeout Socket error.">E_SOCKET_TIMEOUT</a>)
<a name="l00699"></a>00699    {
<a name="l00700"></a>00700       <a class="code" href="Log_8h.html#a58100c42301c30599a1d148e9771646d" title="log warning record with function identification (see Log::log_fheader)">log_fwarn</a>(<span class="stringliteral">&quot;timeout occurred when writing socket&quot;</span>);
<a name="l00701"></a>00701       <a class="code" href="classClient.html#ae6e3c294a22e2bdcc8a4893e193ce7d3" title="Tells id there has been a timeout after a receive operations ends in error.">timeoutOccurred</a> = <span class="keyword">true</span>;
<a name="l00702"></a>00702       <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> = <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55babd24a4212144b1a877f17b8631302e1a">ERRTYPE_SENDTIMEOUT</a>;
<a name="l00703"></a>00703    }
<a name="l00704"></a>00704 
<a name="l00705"></a>00705    <span class="keywordflow">else</span> <span class="comment">// if (ret == SOCKET_ERROR)</span>
<a name="l00706"></a>00706    {
<a name="l00707"></a>00707       <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a> = socket_error();
<a name="l00708"></a>00708       <a class="code" href="Log_8h.html#aa62b9042f25fb0b8205e108ed438a1a6" title="log error record with function identification (see Log::log_fheader)">log_ferror</a>(<span class="stringliteral">&quot;error %d when writing socket&quot;</span>, <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a>);
<a name="l00709"></a>00709       <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> = <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55ba4617be5fd3ac8450ebbb6e5bfc4e17e7">ERRTYPE_SOCKET</a>;
<a name="l00710"></a>00710    }
<a name="l00711"></a>00711 
<a name="l00712"></a>00712    <a class="code" href="Log_8h.html#a58100c42301c30599a1d148e9771646d" title="log warning record with function identification (see Log::log_fheader)">log_fwarn</a>(<span class="stringliteral">&quot;now closing connection&quot;</span>);
<a name="l00713"></a>00713    <a class="code" href="classClient.html#ac1b8a1bcc592b90be708f53c6f3379c8" title="Closes the connection.">client_closeConnection</a>();
<a name="l00714"></a>00714 
<a name="l00715"></a>00715    <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00716"></a>00716 }
<a name="l00717"></a>00717 
<a name="l00718"></a>00718 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00719"></a>00719 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00720"></a>00720 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00721"></a>00721 <span class="comment"></span>
<a name="l00722"></a>00722 <span class="comment">/** Configures how many seconds to wait for a reply from the server.</span>
<a name="l00723"></a>00723 <span class="comment"></span>
<a name="l00724"></a>00724 <span class="comment">    @param [in]</span>
<a name="l00725"></a>00725 <span class="comment">    seconds : how many seconds to wait for a reply from the server.</span>
<a name="l00726"></a>00726 <span class="comment"></span>
<a name="l00727"></a>00727 <span class="comment">    @memberof Client</span>
<a name="l00728"></a>00728 <span class="comment">*/</span>
<a name="l00729"></a>00729 
<a name="l00730"></a><a class="code" href="classClient.html#aff7978dd9e2e6d96ac19e377a18616e5">00730</a> TC2API <span class="keywordtype">void</span> <a class="code" href="classClient.html#aff7978dd9e2e6d96ac19e377a18616e5" title="Configures how many seconds to wait for a reply from the server.">client_setReplyTimeout</a>(<a class="code" href="config_8h.html#ab95f123a6c9bcfee6a343170ef8c5f69" title="unsigned short abbreviation">ushort</a> seconds)
<a name="l00731"></a>00731 {
<a name="l00732"></a>00732    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#aff7978dd9e2e6d96ac19e377a18616e5" title="Configures how many seconds to wait for a reply from the server.">client_setReplyTimeout</a>);
<a name="l00733"></a>00733    <a class="code" href="Log_8h.html#a619ac1ab71ab5d1c7e94e03ddbf16d3b" title="log informational record with function identification (see Log::log_fheader)">log_finfo</a>(<span class="stringliteral">&quot;reply timeout: %d seconds&quot;</span>, seconds);
<a name="l00734"></a>00734 
<a name="l00735"></a>00735    <a class="code" href="classClient.html#a337c42b5e32a1f92420bd21afa6c1f85" title="Default server reply timeout in seconds.">replyTimeout</a> = seconds;
<a name="l00736"></a>00736 }
<a name="l00737"></a>00737 
<a name="l00738"></a>00738 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00739"></a>00739 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00740"></a>00740 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00741"></a>00741 <span class="comment"></span>
<a name="l00742"></a>00742 <span class="comment">/** Configures the server IP address.</span>
<a name="l00743"></a>00743 <span class="comment"></span>
<a name="l00744"></a>00744 <span class="comment">    @param</span>
<a name="l00745"></a>00745 <span class="comment">    ipAddress : IP address of the server</span>
<a name="l00746"></a>00746 <span class="comment"></span>
<a name="l00747"></a>00747 <span class="comment">    @memberof Client</span>
<a name="l00748"></a>00748 <span class="comment">*/</span>
<a name="l00749"></a>00749 
<a name="l00750"></a><a class="code" href="classClient.html#af468f4e75580ec6ab7a8abd967183032">00750</a> TC2API <span class="keywordtype">void</span> <a class="code" href="classClient.html#af468f4e75580ec6ab7a8abd967183032" title="Configures the server IP address.">client_setServerAddress</a>(<a class="code" href="config_8h.html#a0f128c3e0c83721af0220c00554e9c4e" title="const char abbreviation">cchar</a>* ipAddress)
<a name="l00751"></a>00751 {
<a name="l00752"></a>00752    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#af468f4e75580ec6ab7a8abd967183032" title="Configures the server IP address.">client_setServerAddress</a>);
<a name="l00753"></a>00753    <a class="code" href="Log_8h.html#a619ac1ab71ab5d1c7e94e03ddbf16d3b" title="log informational record with function identification (see Log::log_fheader)">log_finfo</a>(<span class="stringliteral">&quot;using server address: %s&quot;</span>, ipAddress);
<a name="l00754"></a>00754 
<a name="l00755"></a>00755    strncpy(<a class="code" href="classClient.html#ab60a34da8654480c6bba7ac1df2da8c3" title="Default server IP address as string.">serverAddressStr</a>, ipAddress, <span class="keyword">sizeof</span>(<a class="code" href="classClient.html#ab60a34da8654480c6bba7ac1df2da8c3" title="Default server IP address as string.">serverAddressStr</a>)-1);
<a name="l00756"></a>00756    <a class="code" href="classClient.html#a6dee50ddcd3fa9bd16c33ea160d15ae6" title="Default server IP address in binary (127.1).">serverAddress</a> = inet_addr(<a class="code" href="classClient.html#ab60a34da8654480c6bba7ac1df2da8c3" title="Default server IP address as string.">serverAddressStr</a>);
<a name="l00757"></a>00757 }
<a name="l00758"></a>00758 
<a name="l00759"></a>00759 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00760"></a>00760 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00761"></a>00761 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00762"></a>00762 <span class="comment"></span>
<a name="l00763"></a>00763 <span class="comment">/** Configures the server TCP port.</span>
<a name="l00764"></a>00764 <span class="comment"></span>
<a name="l00765"></a>00765 <span class="comment">    @param</span>
<a name="l00766"></a>00766 <span class="comment">    port : service port on which the server is waiting.</span>
<a name="l00767"></a>00767 <span class="comment"></span>
<a name="l00768"></a>00768 <span class="comment">    @memberof Client</span>
<a name="l00769"></a>00769 <span class="comment">*/</span>
<a name="l00770"></a>00770 
<a name="l00771"></a><a class="code" href="classClient.html#a3358a12decc93d997ef2a27110173a01">00771</a> TC2API <span class="keywordtype">void</span> <a class="code" href="classClient.html#a3358a12decc93d997ef2a27110173a01" title="Configures the server TCP port.">client_setServicePort</a>(<a class="code" href="config_8h.html#ab95f123a6c9bcfee6a343170ef8c5f69" title="unsigned short abbreviation">ushort</a> port)
<a name="l00772"></a>00772 {
<a name="l00773"></a>00773    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#a3358a12decc93d997ef2a27110173a01" title="Configures the server TCP port.">client_setServicePort</a>);
<a name="l00774"></a>00774    <a class="code" href="Log_8h.html#a619ac1ab71ab5d1c7e94e03ddbf16d3b" title="log informational record with function identification (see Log::log_fheader)">log_finfo</a>(<span class="stringliteral">&quot;using port: %d&quot;</span>, port);
<a name="l00775"></a>00775 
<a name="l00776"></a>00776    <a class="code" href="classClient.html#ac289967d57409c9bfe8d380f777bca9e" title="Default server TCP port.">servicePort</a> = port;
<a name="l00777"></a>00777 }
<a name="l00778"></a>00778 
<a name="l00779"></a>00779 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00780"></a>00780 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00781"></a>00781 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00782"></a>00782 <span class="comment"></span>
<a name="l00783"></a>00783 <span class="comment">/** Enables or disables tracing.</span>
<a name="l00784"></a>00784 <span class="comment"></span>
<a name="l00785"></a>00785 <span class="comment">    @param</span>
<a name="l00786"></a>00786 <span class="comment">    flag : true/false</span>
<a name="l00787"></a>00787 <span class="comment"></span>
<a name="l00788"></a>00788 <span class="comment">    @memberof Client</span>
<a name="l00789"></a>00789 <span class="comment">*/</span>
<a name="l00790"></a>00790 
<a name="l00791"></a><a class="code" href="classClient.html#a46c987e1550f39b919ea87969ac08f9e">00791</a> TC2API <span class="keywordtype">void</span> <a class="code" href="classClient.html#a46c987e1550f39b919ea87969ac08f9e" title="Enables or disables tracing.">client_setTrace</a>(<span class="keywordtype">bool</span> flag)
<a name="l00792"></a>00792 {
<a name="l00793"></a>00793    <a class="code" href="classClient.html#aede8ee1d904b3abd665f17607902ed4a" title="Tells if tracing is enabled.">isTracing</a> = flag;
<a name="l00794"></a>00794 }
<a name="l00795"></a>00795 
<a name="l00796"></a>00796 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00797"></a>00797 <span class="comment">// PUBLIC INTERFACE</span>
<a name="l00798"></a>00798 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00799"></a>00799 <span class="comment"></span>
<a name="l00800"></a>00800 <span class="comment">/** Reports if there has been a timeout after a receive operations ends in</span>
<a name="l00801"></a>00801 <span class="comment">    error.</span>
<a name="l00802"></a>00802 <span class="comment"></span>
<a name="l00803"></a>00803 <span class="comment">    @return</span>
<a name="l00804"></a>00804 <span class="comment">    true (timeout occurred) / false (timeout not occurred)</span>
<a name="l00805"></a>00805 <span class="comment"></span>
<a name="l00806"></a>00806 <span class="comment">    @memberof Client</span>
<a name="l00807"></a>00807 <span class="comment">*/</span>
<a name="l00808"></a>00808 
<a name="l00809"></a><a class="code" href="classClient.html#abd9d963db56bc1578cb78c5db866c786">00809</a> TC2API <span class="keywordtype">bool</span> <a class="code" href="classClient.html#abd9d963db56bc1578cb78c5db866c786" title="Reports if there has been a timeout after a receive operations ends in error.">client_timeout</a>(<span class="keywordtype">void</span>)
<a name="l00810"></a>00810 {
<a name="l00811"></a>00811    <span class="keywordflow">return</span> (<a class="code" href="classClient.html#ae6e3c294a22e2bdcc8a4893e193ce7d3" title="Tells id there has been a timeout after a receive operations ends in error.">timeoutOccurred</a> &amp;&amp; (<a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> == <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55bac3981c1a31844f03343a22bc511e4f4b">ERRTYPE_FORMAT</a>));
<a name="l00812"></a>00812 }
<a name="l00813"></a>00813 
<a name="l00814"></a>00814 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00815"></a>00815 <span class="comment">// INTERNAL HELPER FUNCTIONS</span>
<a name="l00816"></a>00816 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00817"></a>00817 <span class="comment"></span>
<a name="l00818"></a>00818 <span class="comment">/** Receives a specific-length buffer from peer application, and checks for</span>
<a name="l00819"></a>00819 <span class="comment">    error.</span>
<a name="l00820"></a>00820 <span class="comment"></span>
<a name="l00821"></a>00821 <span class="comment">    @return</span>
<a name="l00822"></a>00822 <span class="comment">    number of bytes received, or error</span>
<a name="l00823"></a>00823 <span class="comment"></span>
<a name="l00824"></a>00824 <span class="comment">    @retval</span>
<a name="l00825"></a>00825 <span class="comment">    the number of bytes received, or error</span>
<a name="l00826"></a>00826 <span class="comment"></span>
<a name="l00827"></a>00827 <span class="comment">    @retval</span>
<a name="l00828"></a>00828 <span class="comment">     0 : (SOCKET_CLOSED) connection was closed by the server.</span>
<a name="l00829"></a>00829 <span class="comment"></span>
<a name="l00830"></a>00830 <span class="comment">    @retval</span>
<a name="l00831"></a>00831 <span class="comment">    -1 : (SOCKET_ERROR) an error occurred.</span>
<a name="l00832"></a>00832 <span class="comment"></span>
<a name="l00833"></a>00833 <span class="comment">    @retval</span>
<a name="l00834"></a>00834 <span class="comment">    -2 : (SOCKET_TIMEOUT) not all bytes arrived within the specified time</span>
<a name="l00835"></a>00835 <span class="comment">    interval.</span>
<a name="l00836"></a>00836 <span class="comment"></span>
<a name="l00837"></a>00837 <span class="comment">    @private @memberof Client</span>
<a name="l00838"></a>00838 <span class="comment">*/</span>
<a name="l00839"></a>00839 
<a name="l00840"></a><a class="code" href="classClient.html#ac1a0ff1d1c2453cc22c028da29fcb115">00840</a> <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classClient.html#ac1a0ff1d1c2453cc22c028da29fcb115" title="Receives a specific-length buffer from peer application, and checks for error.">recvBuffer</a>(<span class="keywordtype">int</span> socket, <span class="keywordtype">char</span>* buf, <span class="keywordtype">int</span> len, <span class="keywordtype">unsigned</span> nSeconds,
<a name="l00841"></a>00841    <span class="keywordtype">bool</span> firstPart)
<a name="l00842"></a>00842 {
<a name="l00843"></a>00843    <span class="keywordtype">int</span> ret;
<a name="l00844"></a>00844    <a class="code" href="Log_8h.html#aba64819ead3a2defc230ec7ccf655c4e" title="standard source file name for inclusion in log">log_func</a>(<a class="code" href="classClient.html#ac1a0ff1d1c2453cc22c028da29fcb115" title="Receives a specific-length buffer from peer application, and checks for error.">recvBuffer</a>);
<a name="l00845"></a>00845 
<a name="l00846"></a>00846    <span class="comment">// clears indicators</span>
<a name="l00847"></a>00847    <a class="code" href="classClient.html#ad3973a11a1a20211111f43cb17b5ee31" title="Tells if the connection has been closed after an operation end in error.">connectionClosed</a> = <span class="keyword">false</span>;
<a name="l00848"></a>00848    <a class="code" href="classClient.html#ada0babaa8c1187b1c28fac649bb6ecef" title="Tells if there has been a format error on the wire right after a receive operation...">formatError</a> = <span class="keyword">false</span>;
<a name="l00849"></a>00849    <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a> = 0;
<a name="l00850"></a>00850    <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> = 0;
<a name="l00851"></a>00851    <a class="code" href="classClient.html#ae6e3c294a22e2bdcc8a4893e193ce7d3" title="Tells id there has been a timeout after a receive operations ends in error.">timeoutOccurred</a> = <span class="keyword">false</span>;
<a name="l00852"></a>00852 
<a name="l00853"></a>00853    ret = <a class="code" href="classSocket.html#adb0941e937d3c17ee2204586cae338c4" title="Receives an exact number of bytes from the peer application.">socket_recvBuffer</a>(socket, buf, len, nSeconds, <a class="code" href="classClient.html#aede8ee1d904b3abd665f17607902ed4a" title="Tells if tracing is enabled.">isTracing</a>);
<a name="l00854"></a>00854 
<a name="l00855"></a>00855    <span class="keywordflow">if</span> (ret == len) <span class="comment">// most likely result</span>
<a name="l00856"></a>00856       <span class="keywordflow">goto</span> END;
<a name="l00857"></a>00857 
<a name="l00858"></a>00858    <span class="keywordflow">if</span> (ret == <a class="code" href="Socket_8h.html#a72e38dd6cde0964f3a83c1c19d284bd9" title="Socket closed on reception.">E_SOCKET_CLOSED</a>) <span class="comment">// second most likely result</span>
<a name="l00859"></a>00859    {
<a name="l00860"></a>00860       <a class="code" href="Log_8h.html#a58100c42301c30599a1d148e9771646d" title="log warning record with function identification (see Log::log_fheader)">log_fwarn</a>(<span class="stringliteral">&quot;connection closed when reading socket&quot;</span>);
<a name="l00861"></a>00861       <a class="code" href="classClient.html#ad3973a11a1a20211111f43cb17b5ee31" title="Tells if the connection has been closed after an operation end in error.">connectionClosed</a> = <span class="keyword">true</span>;
<a name="l00862"></a>00862       <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> = <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55ba52dc8b1c53f593e8e1ed2da20b996a92">ERRTYPE_CLOSED</a>;
<a name="l00863"></a>00863       <span class="keywordflow">goto</span> END;
<a name="l00864"></a>00864    }
<a name="l00865"></a>00865 
<a name="l00866"></a>00866    <span class="keywordflow">if</span> (ret == <a class="code" href="Socket_8h.html#ad38171878628c67def4c5bca6a6caf76" title="Generic timeout Socket error.">E_SOCKET_TIMEOUT</a>) <span class="comment">// third most likely result</span>
<a name="l00867"></a>00867    {
<a name="l00868"></a>00868       <a class="code" href="Log_8h.html#a58100c42301c30599a1d148e9771646d" title="log warning record with function identification (see Log::log_fheader)">log_fwarn</a>(<span class="stringliteral">&quot;timeout occurred when reading socket&quot;</span>);
<a name="l00869"></a>00869       <span class="comment">// timeout not considered error if not one byte received,</span>
<a name="l00870"></a>00870       <span class="comment">// only considered error if occurs int the middle of a message</span>
<a name="l00871"></a>00871       <span class="keywordflow">if</span> (!firstPart || <a class="code" href="classSocket.html#aa52c2cece2efbec8249847514a5c65fb" title="Retrieves the partial number of bytes received up until a timeout.">socket_partialLenReceived</a>())
<a name="l00872"></a>00872          <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> = <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55bac3981c1a31844f03343a22bc511e4f4b">ERRTYPE_FORMAT</a>;
<a name="l00873"></a>00873       <a class="code" href="classClient.html#ae6e3c294a22e2bdcc8a4893e193ce7d3" title="Tells id there has been a timeout after a receive operations ends in error.">timeoutOccurred</a> = <span class="keyword">true</span>;
<a name="l00874"></a>00874       <span class="keywordflow">goto</span> END;
<a name="l00875"></a>00875    }
<a name="l00876"></a>00876 
<a name="l00877"></a>00877    <span class="keywordflow">if</span> (ret == <a class="code" href="Socket_8h.html#a114f76181c8d260f8c67f73736a6f232" title="Generic Socket error definition.">E_SOCKET_ERROR</a>) <span class="comment">// fourth most likely result</span>
<a name="l00878"></a>00878    {
<a name="l00879"></a>00879       <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a> = socket_error();
<a name="l00880"></a>00880       <a class="code" href="Log_8h.html#aa62b9042f25fb0b8205e108ed438a1a6" title="log error record with function identification (see Log::log_fheader)">log_ferror</a>(<span class="stringliteral">&quot;error %d when reading socket&quot;</span>, <a class="code" href="classClient.html#ab825728c14e12d04922f751c3f67cb0a" title="Contains the number of the error that occurred immediately after a socket operation...">lastSocketError</a>);
<a name="l00881"></a>00881       <a class="code" href="classClient.html#ab8d2b8c93dc01b7a6ff54c8a06198302" title="Contains the type of the error that occurred immediately after a socket operation...">lastErrorType</a> = <a class="code" href="Client_8c.html#a06fc87d81c62e9abb8790b6e5713c55ba4617be5fd3ac8450ebbb6e5bfc4e17e7">ERRTYPE_SOCKET</a>;
<a name="l00882"></a>00882       <span class="keywordflow">goto</span> END;
<a name="l00883"></a>00883    }
<a name="l00884"></a>00884 
<a name="l00885"></a>00885    <span class="comment">// should not happen (!)</span>
<a name="l00886"></a>00886    abort();
<a name="l00887"></a>00887 
<a name="l00888"></a>00888 END:
<a name="l00889"></a>00889    <span class="keywordflow">return</span> ret;
<a name="l00890"></a>00890 }
<a name="l00891"></a>00891 
<a name="l00892"></a>00892 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00893"></a>00893 <span class="comment">// the end</span>
</pre></div></div>
<hr class="footer"/><address style="text-align: right;"><small>Generated on Sat May 8 16:16:35 2010 for TcpClient2 by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
zvx
Software Developer
Brazil Brazil
I'm a long-time software developer living in Brazil.

I've been developing software for retail and banking automation in C/C++ for many years now. In the old days I even did some COBOL programming, and some assembly for the 8080.

My experience ranges from low level software such as interface code for serial devices for DOS and Windows (bar code scanners, printers, cash dispensers, etc) and goes to writing end user applications for POS terminals and bank ATMs. In between I've done a great deal of TCP/IP programming using the basic Berkeley sockets interface, which is my main interest nowadays.

Comments and Discussions