aboutsummaryrefslogtreecommitdiff
path: root/src/stream/stream_api.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2012-04-07 13:00:46 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2012-04-07 13:00:46 +0000
commit06dc842c43cd3e35aa166bb025a7093bbc0cac7e (patch)
treea81f0b60d2f592925298a7cb9503c94d517f9796 /src/stream/stream_api.c
parenta94d4a2677b856ab2dc68414c6f7e95a50943b76 (diff)
downloadgnunet-06dc842c43cd3e35aa166bb025a7093bbc0cac7e.tar.gz
gnunet-06dc842c43cd3e35aa166bb025a7093bbc0cac7e.zip
-added STREAM_shutdown & test case for shutdown
Diffstat (limited to 'src/stream/stream_api.c')
-rw-r--r--src/stream/stream_api.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/stream/stream_api.c b/src/stream/stream_api.c
index 92c1093f7..535850de2 100644
--- a/src/stream/stream_api.c
+++ b/src/stream/stream_api.c
@@ -232,6 +232,11 @@ struct GNUNET_STREAM_Socket
232 struct GNUNET_STREAM_IOReadHandle *read_handle; 232 struct GNUNET_STREAM_IOReadHandle *read_handle;
233 233
234 /** 234 /**
235 * The shutdown handle associated with this socket
236 */
237 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
238
239 /**
235 * Buffer for storing received messages 240 * Buffer for storing received messages
236 */ 241 */
237 void *receive_buffer; 242 void *receive_buffer;
@@ -443,6 +448,16 @@ struct GNUNET_STREAM_ShutdownHandle
443 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR 448 * Which operation to shutdown? SHUT_RD, SHUT_WR or SHUT_RDWR
444 */ 449 */
445 int operation; 450 int operation;
451
452 /**
453 * Shutdown completion callback
454 */
455 GNUNET_STREAM_ShutdownCompletion completion_cb;
456
457 /**
458 * Closure for completion callback
459 */
460 void *completion_cls;
446}; 461};
447 462
448 463
@@ -1559,6 +1574,10 @@ handle_close (struct GNUNET_STREAM_Socket *socket,
1559{ 1574{
1560 struct GNUNET_STREAM_MessageHeader *close_ack; 1575 struct GNUNET_STREAM_MessageHeader *close_ack;
1561 1576
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 "%x: Received CLOSE from %x\n",
1579 socket->our_id,
1580 socket->other_peer);
1562 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader)); 1581 close_ack = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
1563 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader)); 1582 close_ack->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
1564 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK); 1583 close_ack->header.type = htons (GNUNET_MESSAGE_TYPE_STREAM_CLOSE_ACK);
@@ -1625,10 +1644,30 @@ handle_close_ack (struct GNUNET_STREAM_Socket *socket,
1625 const struct GNUNET_STREAM_MessageHeader *message, 1644 const struct GNUNET_STREAM_MessageHeader *message,
1626 const struct GNUNET_ATS_Information*atsi) 1645 const struct GNUNET_ATS_Information*atsi)
1627{ 1646{
1647 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
1648
1649 shutdown_handle = socket->shutdown_handle;
1628 switch (socket->state) 1650 switch (socket->state)
1629 { 1651 {
1630 case STATE_CLOSE_WAIT: 1652 case STATE_CLOSE_WAIT:
1631 socket->state = STATE_CLOSED; 1653 socket->state = STATE_CLOSED;
1654 if ( (NULL == shutdown_handle) ||
1655 (SHUT_RDWR != shutdown_handle->operation) )
1656 {
1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1658 "%x: Received CLOSE_ACK when shutdown handle is NULL or "
1659 "not for SHUT_RDWR\n",
1660 socket->our_id);
1661 return GNUNET_OK;
1662 }
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1664 "%x: Received CLOSE_ACK from %x\n",
1665 socket->our_id,
1666 socket->other_peer);
1667 if (NULL != shutdown_handle->completion_cb) /* Shutdown completion */
1668 shutdown_handle->completion_cb(shutdown_handle->completion_cls,
1669 SHUT_RDWR);
1670 GNUNET_free (shutdown_handle); /* Free shutdown handle */
1632 break; 1671 break;
1633 default: 1672 default:
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -2515,16 +2554,25 @@ GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
2515{ 2554{
2516 struct GNUNET_STREAM_ShutdownHandle *handle; 2555 struct GNUNET_STREAM_ShutdownHandle *handle;
2517 struct GNUNET_STREAM_MessageHeader *msg; 2556 struct GNUNET_STREAM_MessageHeader *msg;
2557
2558 GNUNET_assert (NULL == socket->shutdown_handle);
2518 2559
2519 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle)); 2560 handle = GNUNET_malloc (sizeof (struct GNUNET_STREAM_ShutdownHandle));
2520 handle->socket = socket; 2561 handle->socket = socket;
2562 handle->completion_cb = completion_cb;
2563 handle->completion_cls = completion_cls;
2564 socket->shutdown_handle = handle;
2565
2521 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader)); 2566 msg = GNUNET_malloc (sizeof (struct GNUNET_STREAM_MessageHeader));
2522 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader)); 2567 msg->header.size = htons (sizeof (struct GNUNET_STREAM_MessageHeader));
2523 switch (operation) 2568 switch (operation)
2524 { 2569 {
2525 case SHUT_RD: 2570 case SHUT_RD:
2526 handle->operation = SHUT_RD; 2571 handle->operation = SHUT_RD;
2527 2572 if (NULL != socket->read_handle)
2573 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2574 "Existing read handle should be cancelled before shutting"
2575 " down reading\n");
2528 break; 2576 break;
2529 case SHUT_WR: 2577 case SHUT_WR:
2530 handle->operation = SHUT_WR; 2578 handle->operation = SHUT_WR;