This is Unofficial EPICS BASE Doxygen Site
cast_server.c File Reference
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "dbDefs.h"
#include "envDefs.h"
#include "epicsMutex.h"
#include "epicsTime.h"
#include "errlog.h"
#include "freeList.h"
#include "osiSock.h"
#include "taskwd.h"
#include "rsrv.h"
#include "server.h"
+ Include dependency graph for cast_server.c:

Go to the source code of this file.

Macros

#define epicsExportSharedSymbols
 
#define TIMEOUT   60.0 /* sec */
 

Functions

void cast_server (void *pParm)
 

Macro Definition Documentation

#define epicsExportSharedSymbols

Definition at line 48 of file cast_server.c.

#define TIMEOUT   60.0 /* sec */

Definition at line 52 of file cast_server.c.

Function Documentation

void cast_server ( void *  pParm)

Definition at line 116 of file cast_server.c.

117 {
118  rsrv_iface_config *conf = pParm;
119  int status;
120  int count=0;
121  int mysocket=0;
122  struct sockaddr_in new_recv_addr;
123  osiSocklen_t recv_addr_size;
124  osiSockIoctl_t nchars;
125  SOCKET recv_sock, reply_sock;
126  struct client *client;
127 
128  recv_addr_size = sizeof(new_recv_addr);
129 
130  reply_sock = conf->udp;
131 
132  /*
133  * setup new client structure but reuse old structure if
134  * possible
135  *
136  */
137  while ( TRUE ) {
138  client = create_client ( reply_sock, IPPROTO_UDP );
139  if ( client ) {
140  break;
141  }
142  epicsThreadSleep(300.0);
143  }
144  if (conf->startbcast) {
145  recv_sock = conf->udpbcast;
146  conf->bclient = client;
147  }
148  else {
149  recv_sock = conf->udp;
150  conf->client = client;
151  }
152  client->udpRecv = recv_sock;
153 
154  casAttachThreadToClient ( client );
155 
156  /*
157  * add placeholder for the first version message should it be needed
158  */
159  rsrv_version_reply ( client );
160 
161  /* these pointers become invalid after signaling casudp_startStopEvent */
162  conf = NULL;
163 
165 
166  while (TRUE) {
167  status = recvfrom (
168  recv_sock,
169  client->recv.buf,
170  client->recv.maxstk,
171  0,
172  (struct sockaddr *)&new_recv_addr,
173  &recv_addr_size);
174  if (status < 0) {
175  if (SOCKERRNO != SOCK_EINTR) {
176  char sockErrBuf[64];
178  sockErrBuf, sizeof ( sockErrBuf ) );
179  epicsPrintf ("CAS: UDP recv error: %s\n",
180  sockErrBuf);
181  epicsThreadSleep(1.0);
182  }
183 
184  } else {
185  size_t idx;
186  for(idx=0; casIgnoreAddrs[idx]; idx++)
187  {
188  if(new_recv_addr.sin_addr.s_addr==casIgnoreAddrs[idx]) {
189  status = -1; /* ignore */
190  break;
191  }
192  }
193  }
194 
195  if (status >= 0 && casudp_ctl == ctlRun) {
196  client->recv.cnt = (unsigned) status;
197  client->recv.stk = 0ul;
199 
201  client->seqNoOfReq = 0;
202 
203  /*
204  * If we are talking to a new client flush to the old one
205  * in case we are holding UDP messages waiting to
206  * see if the next message is for this same client.
207  */
208  if (client->send.stk>sizeof(caHdr)) {
209  status = memcmp(&client->addr,
210  &new_recv_addr, recv_addr_size);
211  if(status){
212  /*
213  * if the address is different
214  */
215  cas_send_dg_msg(client);
216  client->addr = new_recv_addr;
217  }
218  }
219  else {
220  client->addr = new_recv_addr;
221  }
222 
223  if (CASDEBUG>1) {
224  char buf[40];
225 
226  ipAddrToDottedIP (&client->addr, buf, sizeof(buf));
227  errlogPrintf ("CAS: cast server msg of %d bytes from addr %s\n",
228  client->recv.cnt, buf);
229  }
230 
231  if (CASDEBUG>2)
232  count = ellCount (&client->chanList);
233 
234  status = camessage ( client );
235  if(status == RSRV_OK){
236  if(client->recv.cnt !=
237  client->recv.stk){
238  char buf[40];
239 
240  ipAddrToDottedIP (&client->addr, buf, sizeof(buf));
241 
242  epicsPrintf ("CAS: partial (damaged?) UDP msg of %d bytes from %s ?\n",
243  client->recv.cnt - client->recv.stk, buf);
244 
245  epicsTimeToStrftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S",
246  &client->time_at_last_recv);
247  epicsPrintf ("CAS: message received at %s\n", buf);
248  }
249  }
250  else if (CASDEBUG>0){
251  char buf[40];
252 
253  ipAddrToDottedIP (&client->addr, buf, sizeof(buf));
254 
255  epicsPrintf ("CAS: invalid (damaged?) UDP request from %s ?\n", buf);
256 
257  epicsTimeToStrftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S",
258  &client->time_at_last_recv);
259  epicsPrintf ("CAS: message received at %s\n", buf);
260  }
261 
262  if (CASDEBUG>2) {
263  if ( ellCount (&client->chanList) ) {
264  errlogPrintf ("CAS: Fnd %d name matches (%d tot)\n",
265  ellCount(&client->chanList)-count,
266  ellCount(&client->chanList));
267  }
268  }
269  }
270 
271  /*
272  * allow messages to batch up if more are comming
273  */
274  nchars = 0; /* supress purify warning */
275  status = socket_ioctl(recv_sock, FIONREAD, &nchars);
276  if (status<0) {
277  errlogPrintf ("CA cast server: Unable to fetch N characters pending\n");
278  cas_send_dg_msg (client);
279  clean_addrq (client);
280  }
281  else if (nchars == 0) {
282  cas_send_dg_msg (client);
283  clean_addrq (client);
284  }
285  }
286 
287  /* ATM never reached, just a placeholder */
288 
289  if(!mysocket)
290  client->sock = INVALID_SOCKET; /* only one cast_server should destroy the reply socket */
291  destroy_client(client);
292  epicsSocketDestroy(recv_sock);
293 }
LIBCOM_API void epicsStdCall epicsSocketDestroy(SOCKET s)
Definition: osdSock.c:117
GLBLTYPE int CASDEBUG
Definition: server.h:190
void casAttachThreadToClient(struct client *pClient)
SOCKET udpbcast
Definition: server.h:163
struct message_buffer recv
Definition: server.h:81
#define INVALID_SOCKET
Definition: osdSock.h:32
int rsrv_version_reply(struct client *client)
Definition: camessage.c:2141
#define ellCount(PLIST)
Report the number of nodes in a list.
Definition: ellLib.h:84
unsigned cnt
Definition: server.h:70
struct client client
pvd::Status status
unsigned maxstk
Definition: server.h:68
int osiSocklen_t
Definition: osdSock.h:36
unsigned int startbcast
Definition: server.h:166
#define CA_UKN_MINOR_VERSION
Definition: caProto.h:29
#define NULL
Definition: catime.c:38
struct client * client
Definition: server.h:164
SOCKET sock
Definition: server.h:96
unsigned stk
Definition: server.h:67
struct sockaddr_in addr
Definition: server.h:89
void epicsSocketConvertErrnoToString(char *pBuf, unsigned bufSize)
void destroy_client(struct client *client)
#define socket_ioctl(A, B, C)
Definition: osdSock.h:34
LIBCOM_API size_t epicsStdCall epicsTimeToStrftime(char *pBuff, size_t bufLength, const char *pFormat, const epicsTimeStamp *pTS)
Convert epicsTimeStamp to string. See epicsTime::strftime()
Definition: epicsTime.cpp:1120
#define epicsEventSignal(ID)
A synonym for epicsEventTrigger().
Definition: epicsEvent.h:172
int osiSockIoctl_t
Definition: osdSock.h:35
Definition: server.h:169
Definition: server.h:76
struct message_buffer send
Definition: server.h:79
epicsTimeStamp time_at_last_recv
Definition: server.h:91
int epicsStdCall epicsTimeGetCurrent(epicsTimeStamp *pDest)
Get current time into *pDest.
#define RSRV_OK
Definition: rsrv.h:23
#define epicsPrintf
Definition: errlog.h:51
char * buf
Definition: server.h:65
int camessage(struct client *client)
Definition: camessage.c:2391
int SOCKET
Definition: osdSock.h:31
int errlogPrintf(const char *pFormat,...)
Definition: errlog.c:105
struct client * create_client(SOCKET sock, int proto)
GLBLTYPE epicsEventId casudp_startStopEvent
Definition: server.h:209
#define SOCKERRNO
Definition: osdSock.h:33
GLBLTYPE epicsUInt32 * casIgnoreAddrs
Definition: server.h:197
#define SOCK_EINTR
Definition: osdSock.h:64
#define TRUE
Definition: dbDefs.h:27
LIBCOM_API void epicsStdCall epicsThreadSleep(double seconds)
Block the calling thread for at least the specified time.
Definition: osdThread.c:790
void cas_send_dg_msg(struct client *pclient)
Definition: caserverio.c:173
unsigned minor_version_number
Definition: server.h:99
ELLLIST chanList
Definition: server.h:86
ca_uint32_t seqNoOfReq
Definition: server.h:100
SOCKET udpRecv
Definition: server.h:96
GLBLTYPE enum ctl casudp_ctl
Definition: server.h:212
unsigned epicsStdCall ipAddrToDottedIP(const struct sockaddr_in *paddr, char *pBuf, unsigned bufSize)
Definition: osiSock.c:144
struct client * bclient
Definition: server.h:164