File: MSProtocolConnection.C

package info (click to toggle)
aplus-fsf 4.18.8-11
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 22,072 kB
  • ctags: 32,286
  • sloc: cpp: 176,290; ansic: 26,171; sh: 12,810; makefile: 2,342; lisp: 2,150
file content (574 lines) | stat: -rw-r--r-- 14,764 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#ifndef MSProtocolConnectionIMPLEMENTATION
#define MSProtocolConnectionIMPLEMENTATION

///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 1997-2001 Morgan Stanley Dean Witter & Co. All rights reserved. 
// See .../src/LICENSE for terms of distribution
//
//
///////////////////////////////////////////////////////////////////////////////


#include <errno.h>
#include <unistd.h>
#include <math.h>

#include <stdlib.h>
#include <stdarg.h>

#include <MSTypes/MSMethodCallback.H>
#include <MSIPC/MSProtocolConnection.H>
#include <MSTypes/MSMessageLog.H>

#if defined(MS_NEED_SYS_NERR_DECLARATION)
extern "C" int sys_nerr;
extern "C" char* sys_errlist[];
#endif

template <class Type>
MSProtocolConnection<Type>::MSProtocolConnection(const char *name_,
                           const char *host_,int port_) :
MSConnection(name_,0,MSConnection::Yes,1,16,AF_INET,SOCK_STREAM,0)
{
  init();
  _hostPort.set(host_,port_);
  _name=name_;
}

template <class Type>
MSProtocolConnection<Type>::MSProtocolConnection(const char *name_,
                           const MSString &serviceName_) :
MSConnection(name_,0,MSConnection::Yes,1,16,AF_INET,SOCK_STREAM,0)
{
  init();
  _service.establish(serviceName_);
  _hostPort.set(_service.host(),_service.port());
  _name=name_;
}

template <class Type>
MSProtocolConnection<Type>::MSProtocolConnection(const char *name_,int fd_) :
MSConnection(name_,0,MSConnection::No,1,16,AF_INET,SOCK_STREAM,0)
{
  init();
  _fd=fd_;
  _name=name_;
  if (establish()==MSTrue) acknowledge();
}

template <class Type>
void MSProtocolConnection<Type>::init(void)
{
  _readChannel=0;
  _writeChannel=0;
  _headBuffer=0;
  _readBuffer=0;
  _state=0;
  _burstMode=MSFalse;
  _timer=0;
  _readCB=0;
  _sentCB=0;
  _connectCB=0;
  _resetCB=0;
  _readCBData=0;
  _sentCBData=0;
  _connectCBData=0;
  _resetCBData=0;
  _syncErrorBuffer=0;
}

template <class Type>
MSProtocolConnection<Type>::~MSProtocolConnection(void) 
{
  _retry=MSConnection::No;
  close();
  cleanup();
}

// subclasses should provide their own send and read methods
// based on the particualars of the data.
template <class Type>
int MSProtocolConnection<Type>::send(const Type&)
{ return 0; }

template <class Type>
void MSProtocolConnection<Type>::doRead(void)
{}

template <class Type>
void MSProtocolConnection<Type>::doReadBurst(void)
{}

template <class Type>
MSBoolean MSProtocolConnection<Type>::setup(void)
{
  if (service().isValid()==MSTrue)
   {
    if (service().isReady()==MSFalse)
    {
      service().establish();
      hostPort().set(service().host(),service().port());
    }
   }
  _remoteName=(struct sockaddr *)hostPort().sockaddr_in(_remoteNamelen);
  return (_remoteName!=0)?MSTrue:MSFalse;  
}

template <class Type>
MSBoolean MSProtocolConnection<Type>::establish(void)
{
  int kaf=1;
  struct sockaddr sockname;
  int socklen=sizeof(struct sockaddr);

  // check to see if connection is really there 
#if defined(HAVE_SOCKLEN_T)
  if (getpeername(fd(),(struct sockaddr *)&sockname,(socklen_t *)&socklen)) 
#else
  if (getpeername(fd(),(struct sockaddr *)&sockname,&socklen)) 
#endif
   {
     MSMessageLog::warningMessage("MSProtocolConnection: getpeername failed: %s\n",
                                  (errno<sys_nerr)?sys_errlist[errno]:"unknown error");
     close();
     return MSFalse;
   }

  _readChannel=new MSChannel(name().string(),fd(),0,MSChannel::Read,
			 new MSMethodCallback<MSProtocolConnection<Type> >(this,
				&MSProtocolConnection<Type>::doReadCall)); 
  _writeChannel=new MSChannel(name().string(),fd(),0,MSChannel::Write,
			 new MSMethodCallback<MSProtocolConnection<Type> >(this,
				&MSProtocolConnection<Type>::doWriteCall)); 
  _headBuffer=new MSBuffer;
  _readBuffer=new MSBuffer;

#if !defined(MS_NO_FCNTL)
  fcntl(fd(),F_SETFD,1);  // turn on close-on-exec
#endif
  int rc=setsockopt(fd(),SOL_SOCKET,SO_KEEPALIVE,(const char *)&kaf,sizeof(int));
  if(rc==-1) 
    return MSFalse;

  _timer=new MSRegularTimer(0,0,
	     new MSMethodCallback<MSProtocolConnection<Type> >
		    (this,&MSProtocolConnection<Type>::doConnectCall));
  return MSTrue;
}

template <class Type>
int MSProtocolConnection<Type>::readTheBuffer(MSBuffer *b_,int n_)
{
  int n; 
  if (isSet(MSProtocolConnection<Type>::Reset)==MSTrue) return 0;
  if ((n=b_->read(fd(),n_))<0) resetWithError(MSProtocolConnection<Type>::Read);
  else if (n>0) { set(MSProtocolConnection<Type>::Read); }
  return n;
}

template <class Type>
void MSProtocolConnection<Type>::readNotify(const Type &message_)
{
  if (readCallback()!=0) (*readCallback())(message_,readCallbackData());
}

template <class Type>
void MSProtocolConnection<Type>::sentNotify(int sent_)
{
  if (sentCallback()!=0) (*sentCallback())(sent_,sentCallbackData());
}

template <class Type>
void MSProtocolConnection<Type>::connectNotify(void)
{
  if (connectCallback()!=0) (*connectCallback())(connectCallbackData());
}

template <class Type>
void MSProtocolConnection<Type>::resetNotify(MSProtocolConnection<Type>::State state_)
{
  if (resetCallback()!=0) (*resetCallback())(state_,resetCallbackData());
}

template <class Type>
void MSProtocolConnection<Type>::sendTheBuffer(MSBuffer *b_)
{
  MSNodeItem *np=new MSNodeItem((void *)b_);
  np->insert(writeList()); // fifo for writing
}

template <class Type>
int MSProtocolConnection<Type>::writeTheBuffer(MSBuffer *b_,int n_)
{
  if (isSet(MSProtocolConnection<Type>::Reset)==MSTrue) return 0;
  int s=0,n=0;
  while ((n_>0)&&((n=b_->write(fd(),n_))>0)) { s+=n; n_-=n; }
  if (n<0) { resetWithError(MSProtocolConnection<Type>::Write); s=n;}
  return s;
}

template <class Type>
void MSProtocolConnection<Type>::doConnect(void)
{
  _timer=0;
  if (isSet(MSProtocolConnection<Type>::ReadPause)==MSFalse) readChannel()->enable();
  if (isSet(MSProtocolConnection<Type>::Reset)==MSTrue) unset(MSProtocolConnection<Type>::Reset);
  connectNotify();
}

template <class Type>
void MSProtocolConnection<Type>::writeReset(void)
{
  _timer=0;
  resetWithError(MSProtocolConnection<Type>::Write);
}

template <class Type>
void MSProtocolConnection<Type>::resetWithError(MSProtocolConnection<Type>::State state_)
{
  reset();
  resetNotify(state_);
}

template <class Type>
void MSProtocolConnection<Type>::cleanup(void)
{
  if (readChannel()!=0)  { delete _readChannel;  _readChannel=0; }
  if (headBuffer()!=0)   { delete _headBuffer;   _headBuffer=0; }
  if (readBuffer()!=0)   { delete _readBuffer;   _readBuffer=0; }
  if (writeChannel()!=0) { delete _writeChannel; _writeChannel=0; }
  if (_timer!=0) { delete _timer; _timer=0; }
  if(0!=_syncErrorBuffer){ delete _syncErrorBuffer; _syncErrorBuffer=0;}
  if (service().isValid()==MSTrue) { service().isReady(MSFalse); }

  MSNodeItem *hp=writeList();
  MSNodeItem *np;
  while((np=hp->next())!=hp)
   {
     delete (MSBuffer *)np->data();
     delete np;
   }
}

template <class Type>
void MSProtocolConnection<Type>::reset(void)
{
  set(MSProtocolConnection<Type>::Reset);
  close();
}

template <class Type>
int MSProtocolConnection<Type>::doWrite(void) { return doWrite(MSTrue); }

template <class Type>
int MSProtocolConnection<Type>::doWrite(MSBoolean sw_)
{
  if (isSet(MSProtocolConnection<Type>::Reset)==MSTrue) return 0;

  MSBoolean cont=MSTrue;
  int bytes=0,msgs=0;
  int c,s,n;
  MSNodeItem *hp=writeList();
  MSNodeItem *np;
  MSBuffer   *pBuffer;

  while (cont==MSTrue&&((np=hp->next())!=hp)&&isSet(MSProtocolConnection<Type>::WritePause)==MSFalse)
   {
     pBuffer=(MSBuffer *)np->data(); 
     c=pBuffer->put()-pBuffer->get(); 
     s=n=0;
     while ((c>0)&&((n=pBuffer->write(fd(),c))>0))
      {
	s+=n;
	c-=n;
	bytes+=n;
      }
     if (pBuffer->get()==pBuffer->put())
      {
        delete pBuffer;
	delete np;
        msgs++;
        unset(MSProtocolConnection<Type>::Write);
      }
     else 
      {
	cont=MSFalse;
        set(MSProtocolConnection<Type>::Write);
      }
     if (n<0)
      {
	_timer=new MSRegularTimer(0,0,
        new MSMethodCallback<MSProtocolConnection<Type> >
				  (this,&MSProtocolConnection<Type>::doWriteResetCall));
        set(MSProtocolConnection<Type>::Reset);
	if (sw_==MSTrue&&msgs!=0) { sentNotify(msgs); }
	return msgs;
      }
   }
  if (hp==hp->next()) writeChannel()->disable();
  if (sw_==MSTrue&&msgs!=0) { sentNotify(msgs); }
  return msgs;
}

template <class Type>
void MSProtocolConnection<Type>::
readCallback(MSProtocolConnection<Type>::ReadCallback pCallback_,void *pData_)
{
  _readCB=pCallback_;
  _readCBData=pData_;
}

template <class Type>
void MSProtocolConnection<Type>::
sentCallback(MSProtocolConnection<Type>::SentCallback pCallback_,void *pData_) 
{
  _sentCB=pCallback_;
  _sentCBData=pData_;
}

template <class Type>
void MSProtocolConnection<Type>::
connectCallback(MSProtocolConnection<Type>::ConnectCallback pCallback_,void *pData_) 
{
  _connectCB=pCallback_;
  _connectCBData=pData_;
}

template <class Type>
void MSProtocolConnection<Type>::
resetCallback(MSProtocolConnection<Type>::ResetCallback pCallback_,void *pData_) 
{
  _resetCB=pCallback_;
  _resetCBData=pData_;
}

template <class Type>
int MSProtocolConnection<Type>::syncError(int rc,const char *symbol_,const char *fmt_,...)
{
  va_list ap;

  if(0==_syncErrorBuffer) _syncErrorBuffer=new char[256];
  strcpy(_syncErrorBuffer,symbol_);
  va_start(ap,fmt_);
  (void)vsprintf(_syncErrorBuffer+20,fmt_,ap);
  va_end(ap);
  syncErrorReport();
  return rc;
}

template <class Type>
void MSProtocolConnection<Type>::syncErrorReport(void)
{
  MSMessageLog::infoMessage(_syncErrorBuffer+20);
}

template <class Type>
int MSProtocolConnection<Type>::doSyncWrite(void)
{
  if (isSet(MSProtocolConnection<Type>::Reset)==MSTrue) 
    return syncError(-1,"closed","Connection Not Open\n");;
  MSBoolean cont=MSTrue;
  int bytes=0;
  int c,s,n;
  MSNodeItem *hp=writeList();
  MSNodeItem *np;
  MSBuffer *pBuffer;
  while (cont==MSTrue&&((np=hp->next())!=hp))
   {
     pBuffer=(MSBuffer *)np->data(); 
     c=pBuffer->put()-pBuffer->get(); 
     s=n=0;
     while ((c>0)&&((n=pBuffer->write(fd(),c))>0))
      {
	s+=n;
	c-=n;
	bytes+=n;
      }
     if (pBuffer->get()==pBuffer->put())
      {
        delete pBuffer;
	delete np;
        unset(MSProtocolConnection<Type>::Write);
      }
     else
      {
	cont=MSFalse;
        set(MSProtocolConnection<Type>::Write);
      }
     if (n<0)
      {
	_timer=new MSRegularTimer(0,0,
                                  new MSMethodCallback<MSProtocolConnection<Type> >
				  (this,&MSProtocolConnection<Type>::doWriteResetCall));
        set(MSProtocolConnection<Type>::Reset);
	return -1;
      }
   }
  if (hp==hp->next())
   {
     if(writeChannel()->enabled()==MSTrue)
      {
        writeChannel()->disable();
      }
     return 1;
   }
  else
   {
     return 0;
   }
}

template <class Type>
int MSProtocolConnection<Type>::doSyncRead(Type &)
{ return 0; }

template <class Type>
int MSProtocolConnection<Type>::syncRead(Type &data_,double seconds_)
{
  return syncRead(data_,(int)(floor(seconds_)),
		  (int)(1000000.0*(seconds_-floor(seconds_))));
}

template <class Type>
int MSProtocolConnection<Type>::syncRead(Type &data_,int seconds_,int microseconds_,MSBoolean isAbsolute_)
{
  if (isSet(MSProtocolConnection<Type>::Reset)==MSTrue) 
    return syncError(-1,"closed","Connection Not Open.\n");
  struct timeval timeout, now, tvp;
  if (isAbsolute_==MSTrue)
  {
    if (microseconds_<0) 
      return syncError(-1,"timeval","Negative Absolute Time\n");
    tvp.tv_sec=seconds_;
    tvp.tv_usec=microseconds_;
  }
  else 
  {
    gettimeofday(&now,NULL);
    timeout.tv_sec=seconds_;
    timeout.tv_usec=microseconds_;
    tvsum(&now,&timeout,&tvp);
  }
  if(readChannel()==0)
    return syncError(-1,"nochan","No Read Channel\n");
  return syncReadSelectLoop(data_,&tvp);
}

template <class Type>
int MSProtocolConnection<Type>::syncSend(const Type &,double)
{ return syncError(-1,"bogus","syncSend Is Not Defined\n."); }

template <class Type>
int MSProtocolConnection<Type>::syncSend(const Type &,int,int,MSBoolean)
{ return syncError(-1,"bogus","syncSend Is Not Defined\n."); }

template <class Type>
int MSProtocolConnection<Type>::syncReadSelectLoop(Type &data_,
						   struct timeval *timeout_)
{
  int rc=0,result=0;
  struct timeval timeLeft, *tvp;

  if (timeout_ != (struct timeval *)0) 
  {
    tvp=&timeLeft;
    tvnorm(timeout_);
    tvdiff(timeout_,tod(),tvp);
    if (tvp->tv_sec<0||tvp->tv_usec<0) tvp->tv_sec=tvp->tv_usec=0;
  }
  else tvp=NULL;

  for(;;)
  {
    if(readChannel()==0) 
      return syncError(-1,"readchan","Lost Read Channel\n");
    rc=MSChannel::select(fd(),MSChannel::Read,tvp);
    if(rc<0)
    {
      if (EINTR==errno)
	return syncError(-1,"interrupt","select() received an interrupt\n");
      else if (EIO==errno)
	return syncError(-1,"fdsisset","unexpected event from select\n");
      else
	return syncError(-1,"select","select() returned %d, errno %d\n",
			 rc, errno);
    }
    else if (rc>0)
    {
      if ((result=doSyncRead(data_)) != 0 ) break;
    }
    if (tvp != (struct timeval *)0) 
    {
      tvdiff(timeout_,tod(),tvp);
      if (tvp->tv_sec<0||tvp->tv_usec<0) tvp->tv_sec=tvp->tv_usec=0;
      if (tvp->tv_sec==0 && tvp->tv_usec==0)
       {
         return syncError(0,"timeout","Sync read loop timed out...\n");
       }
    }
  }
  return result;
}

template <class Type>
int MSProtocolConnection<Type>::syncWriteSelectLoop(struct timeval *timeout_)
{
  long result=0;
  long rc=0;
  struct timeval timeLeft, *tvp;

  if (timeout_!=(struct timeval *)0) 
  {
    tvp=&timeLeft;
    tvnorm(timeout_);
    tvdiff(timeout_,tod(),tvp);
    if (tvp->tv_sec<0||tvp->tv_usec<0) tvp->tv_sec=tvp->tv_usec=0;
  }
  else tvp=NULL;
  
  for (;;) 
  {
    if(writeChannel()==0) 
      return syncError(-1,"writechan","Lost Write Channel\n");
    rc=MSChannel::select(fd(),MSChannel::Write,tvp);
    if(rc<0)
    {
      if (errno==EINTR) 
	return syncError(-1,"interrupt", "select() received an interrupt\n");
      else if (errno==EIO) 
	return syncError(-1,"fdsisset","unexpected event broke select()\n");
      else 
	return syncError(-1,"select",
			 "select() returned %d, errno %d\n",rc,errno);
    }
    else if(rc>0)
    {
      if ((result=doSyncWrite()) < 0) 
	return syncError(-1,"syncwrite","reset during sync write\n");
      else if (result>0) break;
    }
    if (tvp != NULL)
    {
      tvdiff(timeout_,tod(),tvp);
      if (tvp->tv_sec<0||tvp->tv_usec<0) tvp->tv_sec=tvp->tv_usec=0;
      if (tvp->tv_sec==0 && tvp->tv_usec==0)
       {
         if(writeChannel()->enabled()==MSFalse)
          {
            writeChannel()->enable();
          }
         return syncError(0,"timeout", "Sync write loop timed out...\n");
       }
    }
  }
  return result;
}

#endif