47
47
/* Standard logging message
49
#define CONN_LOG_EX(subcode, level, message, status) \
50
CORE_LOGF_X(subcode, level, \
51
("%s (connector \"%s\", status \"%s\")", message, \
53
? conn->meta.get_type(conn->meta.c_get_type) \
54
: "Unknown", IO_StatusStr(status)))
56
#define CONN_LOG(subcode, level, descr) \
57
CONN_LOG_EX(subcode, level, descr, status)
49
#define CONN_LOG_EX(subcode, func_name, level, message, status) \
51
const char* ststr = status ? IO_StatusStr((EIO_Status) status) : ""; \
53
if ((EIO_Status) status == eIO_Timeout && timeout) { \
54
sprintf(stbuf, "%s[%u.%06u]", ststr, \
55
(unsigned int)(timeout->sec + timeout->usec / 1000000), \
56
(unsigned int) (timeout->usec % 1000000)); \
57
assert(strlen(stbuf) < sizeof(stbuf)); \
60
CORE_LOGF_X(subcode, level, \
61
("[CONN_" #func_name "(%s)] %s%s%s", \
63
? conn->meta.get_type(conn->meta.c_get_type) \
64
: "Unknown", message, \
65
ststr && *ststr ? ": " : "", \
66
ststr ? ststr : "")); \
69
#define CONN_LOG(s_c, f_n, lvl, msg) CONN_LOG_EX(s_c, f_n, lvl, msg, status)
71
/* Standard macros to verify that the passed connection handle is not NULL
72
* NB: "retval" must be either a valid EIO_Status or 0 (no status logged)
74
#define CONN_NOT_NULL_EX(subcode, func_name, retval) \
76
const STimeout* timeout = 0/*dummy*/; \
77
CONN_LOG_EX(subcode, func_name, eLOG_Error, \
78
"NULL connection handle", retval); \
83
#define CONN_NOT_NULL(s_c, f_n) CONN_NOT_NULL_EX(s_c, f_n, eIO_InvalidArg)
60
# define CONN_TRACE(descr) CONN_LOG(0, eLOG_Trace, descr)
86
# define CONN_TRACE(f_n, msg) CONN_LOG(0, f_n, eLOG_Trace, msg)
62
# define CONN_TRACE(descr) ((void) 0)
88
# define CONN_TRACE(f_n, msg) ((void) 0)
65
/* Standard macros to verify that the passed connection handle is not NULL
67
#define CONN_NOT_NULL_EX(subcode, func_name, status) \
69
CORE_LOG_X(subcode, eLOG_Error, "CONN_" #func_name \
70
"(conn, ...) -- NULL connection handle"); \
75
#define CONN_NOT_NULL(subcode, func_name) \
76
CONN_NOT_NULL_EX(subcode, func_name, eIO_InvalidArg)
79
92
/* Connection state
227
241
static EIO_Status s_Open
244
const STimeout* timeout;
230
245
EIO_Status status;
232
247
assert(conn->state == eCONN_Closed && conn->meta.list != 0);
234
249
/* call current connector's "OPEN" method */
235
status = conn->meta.open
236
? conn->meta.open(conn->meta.c_open,
237
conn->o_timeout == kDefaultTimeout ?
238
conn->meta.default_timeout : conn->o_timeout)
250
if (conn->meta.open) {
251
timeout = (conn->o_timeout == kDefaultTimeout
252
? conn->meta.default_timeout
254
status = conn->meta.open(conn->meta.c_open, timeout);
256
status = eIO_NotSupported;
257
timeout = 0/*dummy*/;
241
260
if (status != eIO_Success) {
242
CONN_LOG(5, eLOG_Error, "[CONN_Open] Cannot open connection");
261
CONN_LOG(5, Open, eLOG_Error, "Failed to open connection");
312
328
if (&conn->ww_timeout != timeout)
313
329
conn->ww_timeout = *timeout;
314
330
conn->w_timeout = &conn->ww_timeout;
316
332
conn->w_timeout = timeout;
320
status = eIO_InvalidArg;
321
CONN_LOG(9, eLOG_Error,
322
"[CONN_SetTimeout] Unknown event to set timeout for");
335
sprintf(errbuf, "Unknown event #%d to set timeout for", (int) event);
336
CONN_LOG_EX(9, SetTimeout, eLOG_Error, errbuf, eIO_InvalidArg);
337
return eIO_InvalidArg;
348
const STimeout* timeout;
335
351
CONN_NOT_NULL_EX(10, GetTimeout, 0);
339
return conn->o_timeout;
355
timeout = conn->o_timeout;
340
357
case eIO_ReadWrite:
341
CONN_LOG_EX(11, eLOG_Warning,
342
"[CONN_GetTimeout] ReadWrite timeout requested",
358
timeout = 0/*dummy*/;
359
CONN_LOG_EX(11, GetTimeout, eLOG_Warning,
360
"ReadWrite timeout requested", 0);
346
return conn->r_timeout;
363
timeout = conn->r_timeout;
348
return conn->w_timeout;
366
timeout = conn->w_timeout;
350
return conn->c_timeout;
369
timeout = conn->c_timeout;
352
CONN_LOG_EX(12, eLOG_Error,
353
"[CONN_GetTimeout] Unknown event to get timeout for",
373
sprintf(errbuf, "Unknown event #%d to get timeout for", (int) event);
374
CONN_LOG_EX(12, GetTimeout, eLOG_Error, errbuf, 0);
388
409
: eIO_NotSupported;
390
411
if (status != eIO_Success) {
391
if (status != eIO_Timeout) {
392
CONN_LOG(14, eLOG_Error, event == eIO_Read
393
? "[CONN_Wait] Error waiting on read"
394
: "[CONN_Wait] Error waiting on write");
395
} else if (!timeout || (timeout->sec | timeout->usec)) {
396
CONN_LOG(15, eLOG_Warning, event == eIO_Read
397
? "[CONN_Wait] Read timed out"
398
: "[CONN_Wait] Write timed out");
412
static const char* kErrMsg[] = { "Read event failed",
413
"Write event failed" };
418
level = eLOG_Warning;
419
else if (timeout->sec | timeout->usec)
425
level = event == eIO_Read ? eLOG_Trace : eLOG_Error;
428
level = eLOG_Warning;
434
CONN_LOG(14, Wait, level, kErrMsg[event != eIO_Read]);
409
444
size_t* n_written)
446
const STimeout* timeout;
412
447
EIO_Status status;
414
449
assert(*n_written == 0);
416
451
/* check if the write method is specified at all */
417
452
if (!conn->meta.write) {
453
timeout = 0/*dummy*/;
418
454
status = eIO_NotSupported;
419
CONN_LOG(16, eLOG_Error, "[CONN_Write] Unable to write data");
455
CONN_LOG(16, Write, eLOG_Error, "Cannot write data");
423
459
/* call current connector's "WRITE" method */
424
wto = (conn->w_timeout == kDefaultTimeout
425
? conn->meta.default_timeout : conn->w_timeout);
426
status = conn->meta.write(conn->meta.c_write, buf, size, n_written, wto);
460
timeout = (conn->w_timeout == kDefaultTimeout
461
? conn->meta.default_timeout
463
status = conn->meta.write(conn->meta.c_write, buf, size,
428
466
if (status != eIO_Success) {
429
467
if (*n_written) {
430
CONN_TRACE("[CONN_Write] Write error");
468
CONN_TRACE(Write, "Write error");
431
469
status = eIO_Success;
432
470
} else if (size) {
433
471
ELOG_Level level;
434
472
if (status != eIO_Timeout || conn->w_timeout == kDefaultTimeout)
435
473
level = eLOG_Error;
436
else if (wto && (wto->sec | wto->usec))
474
else if (timeout && (timeout->sec | timeout->usec))
437
475
level = eLOG_Warning;
439
477
level = eLOG_Trace;
440
CONN_LOG(17, level, "[CONN_Write] Cannot write data");
478
CONN_LOG(17, Write, level, "Unable to write data");
533
573
/* call current connector's "FLUSH" method */
534
574
if (!conn->meta.flush)
535
575
return eIO_Success;
536
status = conn->meta.flush(conn->meta.c_flush,
537
conn->w_timeout == kDefaultTimeout ?
538
conn->meta.default_timeout : conn->w_timeout);
576
timeout = (conn->w_timeout == kDefaultTimeout
577
? conn->meta.default_timeout
579
status = conn->meta.flush(conn->meta.c_flush, timeout);
539
581
if (status != eIO_Success)
540
CONN_LOG(21, eLOG_Warning, "[CONN_Flush] Cannot flush data");
582
CONN_LOG(21, Flush, eLOG_Warning, "Failed to flush");
552
594
int/*bool*/ peek)
596
const STimeout* timeout;
555
597
EIO_Status status;
557
599
assert(*n_read == 0);
559
601
/* check if the read method is specified at all */
560
602
if (!conn->meta.read) {
603
timeout = 0/*dummy*/;
561
604
status = eIO_NotSupported;
562
CONN_LOG(22, eLOG_Error, "[CONN_Read] Unable to read data");
605
CONN_LOG(22, Read, eLOG_Error, "Cannot read data");
566
609
/* read data from the internal peek buffer, if any */
569
? BUF_Peek(conn->buf, buf, size) : BUF_Read(conn->buf, buf, size);
612
? BUF_Peek(conn->buf, buf, size)
613
: BUF_Read(conn->buf, buf, size));
570
614
if (*n_read == size)
571
615
return eIO_Success;
572
616
buf = (char*) buf + *n_read;
575
619
/* read data from the connection */
577
621
size_t x_read = 0;
578
rto = (conn->r_timeout == kDefaultTimeout
579
? conn->meta.default_timeout : conn->r_timeout);
622
timeout = (conn->r_timeout == kDefaultTimeout
623
? conn->meta.default_timeout
580
625
/* call current connector's "READ" method */
581
626
status = conn->meta.read(conn->meta.c_read, buf, size - *n_read,
583
628
*n_read += x_read;
585
630
/* save data in the internal peek buffer */
586
631
if (peek && !BUF_Write(&conn->buf, buf, x_read)) {
587
CONN_LOG_EX(32, eLOG_Error, "[CONN_Read] Cannot save peek data",
632
CONN_LOG_EX(32, Read, eLOG_Error,
633
"Cannot save peek data", 0);
592
637
if (status != eIO_Success) {
594
CONN_TRACE("[CONN_Read] Read error");
639
CONN_TRACE(Read, "Read error");
595
640
status = eIO_Success;
596
641
} else if (size && status != eIO_Closed) {
597
642
ELOG_Level level;
598
643
if (status != eIO_Timeout || conn->r_timeout == kDefaultTimeout)
599
644
level = eLOG_Error;
600
else if (rto && (rto->sec | rto->usec))
645
else if (timeout && (timeout->sec | timeout->usec))
601
646
level = eLOG_Warning;
603
648
level = eLOG_Trace;
604
CONN_LOG(23, level, "[CONN_Read] Cannot read data");
649
CONN_LOG(23, Read, level, "Unable to read data");
743
791
if (i < x_read && !BUF_PushBack(&conn->buf, x_buf + i, x_read - i)) {
744
CONN_LOG_EX(33, eLOG_Error,
745
"[CONN_ReadLine] Cannot push extra data back",
792
const STimeout* timeout = 0;
793
CONN_LOG_EX(15, ReadLine, eLOG_Error,
794
"Cannot pushback extra data", 0);
748
796
if (done || len >= size)
846
894
FConnAsyncCleanup cleanup)
848
896
EIO_Status status;
897
const STimeout* timeout = 0/*dummy*/;
849
898
CONNECTOR x_connector = conn->connector;
850
899
SConnectorAsyncHandler* x_data = &conn->async_data;
852
901
CONN_NOT_NULL(29, WaitAsync);
854
903
/* perform connect, if not connected yet */
855
if (!conn->connected && (status = s_Connect(conn)) != eIO_Success)
904
if (!conn->connected && (status = s_Open(conn)) != eIO_Success)
858
907
/* reset previous handler, cleanup its data */
859
908
/* (call current connector's "WAIT_ASYNC" method with NULLs) */
860
status = x_connector->vtable.wait_async ?
861
x_connector->vtable.wait_async(x_connector->handle, 0, 0) :
909
status = (x_connector->vtable.wait_async
910
? x_connector->vtable.wait_async(x_connector->handle, 0, 0)
863
912
if (status != eIO_Success) {
864
CONN_LOG(30, eLOG_Error, "[CONN_WaitAsync] Cannot reset handler");
913
CONN_LOG(30, WaitAsync, eLOG_Error, "Cannot reset handler");
867
916
if (x_data->cleanup)
880
929
x_data->cleanup = cleanup;
882
931
status = x_connector->vtable.wait_async(x_connector->handle,
883
s_ConnectorAsyncHandler, x_data);
884
if (status != eIO_Success) {
885
CONN_LOG(31, eLOG_Error, "[CONN_WaitAsync] Cannot set new handler");
932
s_ConnectorAsyncHandler,
934
if (status != eIO_Success)
935
CONN_LOG(31, WaitAsync, eLOG_Error, "Cannot set new handler");
889
938
#endif /* IMPLEMENTED__CONN_WaitAsync */