|
a/src/utils/netcon.cpp |
|
b/src/utils/netcon.cpp |
|
... |
|
... |
57 |
#endif
|
57 |
#endif
|
58 |
|
58 |
|
59 |
#define MILLIS(OLD, NEW) ( (long)(((NEW).tv_sec - (OLD).tv_sec) * 1000 + \
|
59 |
#define MILLIS(OLD, NEW) ( (long)(((NEW).tv_sec - (OLD).tv_sec) * 1000 + \
|
60 |
((NEW).tv_usec - (OLD).tv_usec) / 1000))
|
60 |
((NEW).tv_usec - (OLD).tv_usec) / 1000))
|
61 |
|
61 |
|
|
|
62 |
// Static method
|
62 |
// Simplified interface to 'select()'. Only use one fd, for either
|
63 |
// Simplified interface to 'select()'. Only use one fd, for either
|
63 |
// reading or writing. This is only used when not using the
|
64 |
// reading or writing. This is only used when not using the
|
64 |
// selectloop() style of network i/o.
|
65 |
// selectloop() style of network i/o.
|
65 |
// Note that timeo == 0 does NOT mean wait forever but no wait at all.
|
66 |
// Note that timeo == 0 does NOT mean wait forever but no wait at all.
|
66 |
int Netcon::select1(int fd, int timeo, int write)
|
67 |
int Netcon::select1(int fd, int timeo, int write)
|
|
... |
|
... |
114 |
periodicmillis = ms;
|
115 |
periodicmillis = ms;
|
115 |
if (periodicmillis > 0)
|
116 |
if (periodicmillis > 0)
|
116 |
gettimeofday(&lasthdlcall, 0);
|
117 |
gettimeofday(&lasthdlcall, 0);
|
117 |
}
|
118 |
}
|
118 |
|
119 |
|
119 |
// set the appropriate timeout so that the select call returns in time
|
120 |
// Compute the appropriate timeout so that the select call returns in
|
120 |
// to call the periodic routine.
|
121 |
// time to call the periodic routine.
|
121 |
void periodictimeout(struct timeval *tv)
|
122 |
static void periodictimeout(struct timeval *tv)
|
122 |
{
|
123 |
{
|
123 |
// If periodic not set, the select call times out and we loop
|
124 |
// If periodic not set, the select call times out and we loop
|
124 |
// after a very long time (we'd need to pass NULL to select for an
|
125 |
// after a very long time (we'd need to pass NULL to select for an
|
125 |
// infinite wait, and I'm too lazy to handle it)
|
126 |
// infinite wait, and I'm too lazy to handle it)
|
126 |
if (periodicmillis <= 0) {
|
127 |
if (periodicmillis <= 0) {
|
|
... |
|
... |
141 |
tv->tv_usec = (millis % 1000) * 1000;
|
142 |
tv->tv_usec = (millis % 1000) * 1000;
|
142 |
}
|
143 |
}
|
143 |
|
144 |
|
144 |
// Check if it's time to call the handler. selectloop will return to
|
145 |
// Check if it's time to call the handler. selectloop will return to
|
145 |
// caller if it or we return 0
|
146 |
// caller if it or we return 0
|
146 |
int maybecallperiodic()
|
147 |
static int maybecallperiodic()
|
147 |
{
|
148 |
{
|
148 |
if (periodicmillis <= 0)
|
149 |
if (periodicmillis <= 0)
|
149 |
return 1;
|
150 |
return 1;
|
150 |
struct timeval mtv;
|
151 |
struct timeval mtv;
|
151 |
gettimeofday(&mtv, 0);
|
152 |
gettimeofday(&mtv, 0);
|
|
... |
|
... |
308 |
}
|
309 |
}
|
309 |
}
|
310 |
}
|
310 |
|
311 |
|
311 |
void Netcon::closeconn()
|
312 |
void Netcon::closeconn()
|
312 |
{
|
313 |
{
|
313 |
if (m_fd >= 0) {
|
314 |
if (m_ownfd && m_fd >= 0) {
|
314 |
close(m_fd);
|
315 |
close(m_fd);
|
|
|
316 |
}
|
315 |
m_fd = -1;
|
317 |
m_fd = -1;
|
316 |
}
|
318 |
m_ownfd = true;
|
317 |
}
|
319 |
}
|
318 |
|
320 |
|
319 |
char *Netcon::sterror()
|
321 |
char *Netcon::sterror()
|
320 |
{
|
322 |
{
|
321 |
return strerror(errno);
|
323 |
return strerror(errno);
|
|
... |
|
... |
416 |
}
|
418 |
}
|
417 |
|
419 |
|
418 |
// Receive at most cnt bytes (maybe less)
|
420 |
// Receive at most cnt bytes (maybe less)
|
419 |
int NetconData::receive(char *buf, int cnt, int timeo)
|
421 |
int NetconData::receive(char *buf, int cnt, int timeo)
|
420 |
{
|
422 |
{
|
421 |
LOGDEB2(("NetconData::receive: cnt %d timeo %d m_buf 0x%x m_bufbytes %d\n",
|
423 |
LOGDEB2(("NetconData::receive: cnt %d timeo %d m_buf 0x%x m_bufbytes %d\n",
|
422 |
cnt, timeo, m_buf, m_bufbytes));
|
424 |
cnt, timeo, m_buf, m_bufbytes));
|
423 |
if (m_fd < 0) {
|
425 |
if (m_fd < 0) {
|
424 |
LOGERR(("NetconData::receive: connection not opened\n"));
|
426 |
LOGERR(("NetconData::receive: connection not opened\n"));
|
425 |
return -1;
|
427 |
return -1;
|
426 |
}
|
428 |
}
|
|
|
429 |
int fromibuf = 0;
|
427 |
// Get whatever might have been left in the buffer by a previous
|
430 |
// Get whatever might have been left in the buffer by a previous
|
428 |
// getline, except if we're called to fill the buffer of course
|
431 |
// getline, except if we're called to fill the buffer of course
|
429 |
if (m_buf && m_bufbytes > 0 && (buf < m_buf || buf > m_buf + m_bufsize)) {
|
432 |
if (m_buf && m_bufbytes > 0 && (buf < m_buf || buf > m_buf + m_bufsize)) {
|
430 |
int frombuf = MIN(m_bufbytes, cnt);
|
433 |
fromibuf = MIN(m_bufbytes, cnt);
|
431 |
memcpy(buf, m_bufbase, frombuf);
|
434 |
memcpy(buf, m_bufbase, fromibuf);
|
432 |
m_bufbytes -= frombuf;
|
435 |
m_bufbytes -= fromibuf;
|
433 |
m_bufbase += frombuf;
|
436 |
m_bufbase += fromibuf;
|
434 |
cnt -= frombuf;
|
437 |
cnt -= fromibuf;
|
|
|
438 |
LOGDEB2(("NetconData::receive: transferred %d from mbuf\n", fromibuf));
|
435 |
if (cnt <= 0)
|
439 |
if (cnt <= 0)
|
436 |
return frombuf;
|
440 |
return fromibuf;
|
437 |
}
|
441 |
}
|
438 |
if (timeo > 0) {
|
442 |
if (timeo > 0) {
|
439 |
int ret = select1(m_fd, timeo);
|
443 |
int ret = select1(m_fd, timeo);
|
440 |
if (ret == 0) {
|
444 |
if (ret == 0) {
|
441 |
LOGDEB2(("NetconData::receive timed out\n"));
|
445 |
LOGDEB2(("NetconData::receive timed out\n"));
|
|
... |
|
... |
446 |
LOGSYSERR("NetconData::receive", "select", "");
|
450 |
LOGSYSERR("NetconData::receive", "select", "");
|
447 |
return -1;
|
451 |
return -1;
|
448 |
}
|
452 |
}
|
449 |
}
|
453 |
}
|
450 |
m_didtimo = 0;
|
454 |
m_didtimo = 0;
|
451 |
int flags = 0;
|
|
|
452 |
if ((cnt = read(m_fd, buf, cnt)) < 0) {
|
455 |
if ((cnt = read(m_fd, buf + fromibuf, cnt)) < 0) {
|
453 |
char fdcbuf[10];sprintf(fdcbuf, "%d", m_fd);
|
456 |
char fdcbuf[10];sprintf(fdcbuf, "%d", m_fd);
|
454 |
LOGSYSERR("NetconData::receive", "read", fdcbuf);
|
457 |
LOGSYSERR("NetconData::receive", "read", fdcbuf);
|
455 |
return -1;
|
458 |
return -1;
|
456 |
}
|
459 |
}
|
457 |
LOGDEB2(("NetconData::receive: normal return, cnt %d\n", cnt));
|
460 |
LOGDEB2(("NetconData::receive: normal return, cnt %d\n", cnt));
|
458 |
return cnt;
|
461 |
return fromibuf + cnt;
|
459 |
}
|
462 |
}
|
460 |
|
463 |
|
461 |
// Receive exactly cnt bytes (except for timeout)
|
464 |
// Receive exactly cnt bytes (except for timeout)
|
462 |
int NetconData::doreceive(char *buf, int cnt, int timeo)
|
465 |
int NetconData::doreceive(char *buf, int cnt, int timeo)
|
463 |
{
|
466 |
{
|
|
... |
|
... |
508 |
int maxtransf = MIN(m_bufbytes, cnt-1);
|
511 |
int maxtransf = MIN(m_bufbytes, cnt-1);
|
509 |
int nn = maxtransf;
|
512 |
int nn = maxtransf;
|
510 |
LOGDEB2(("Before loop, bufbytes %d, maxtransf %d, nn: %d\n",
|
513 |
LOGDEB2(("Before loop, bufbytes %d, maxtransf %d, nn: %d\n",
|
511 |
m_bufbytes, maxtransf, nn));
|
514 |
m_bufbytes, maxtransf, nn));
|
512 |
for (nn = maxtransf; nn > 0;) {
|
515 |
for (nn = maxtransf; nn > 0;) {
|
513 |
// This is not pretty but we want nn to be decremented for each
|
516 |
// This is not pretty but we want nn to be decremented for
|
514 |
// byte copied (even newline), and not become -1 if we go to the end
|
517 |
// each byte copied (even newline), and not become -1 if
|
515 |
// Better ways welcome!
|
518 |
// we go to the end. Better ways welcome!
|
516 |
nn--;
|
519 |
nn--;
|
517 |
if ((*cp++ = *m_bufbase++) == '\n')
|
520 |
if ((*cp++ = *m_bufbase++) == '\n')
|
518 |
break;
|
521 |
break;
|
519 |
}
|
522 |
}
|
520 |
// Update counts
|
523 |
// Update counts
|
|
... |
|
... |
655 |
|
658 |
|
656 |
|
659 |
|
657 |
int NetconCli::setconn(int fd)
|
660 |
int NetconCli::setconn(int fd)
|
658 |
{
|
661 |
{
|
659 |
LOGDEB2(("Netconcli::setconn: fd %d\n", fd));
|
662 |
LOGDEB2(("Netconcli::setconn: fd %d\n", fd));
|
660 |
|
|
|
661 |
closeconn();
|
663 |
closeconn();
|
662 |
|
664 |
|
663 |
m_fd = fd;
|
665 |
m_fd = fd;
|
|
|
666 |
m_ownfd = false;
|
664 |
setpeer("");
|
667 |
setpeer("");
|
665 |
|
668 |
|
666 |
return 0;
|
669 |
return 0;
|
667 |
}
|
670 |
}
|
668 |
|
671 |
|