Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
- Bugfix: flush the socket on close only if there is some *output*.
[simgrid.git] / src / gras / Transport / transport_plugin_buf.c
index e19f2ea..bbfe9dd 100644 (file)
@@ -2,17 +2,17 @@
 
 /* buf trp (transport) - buffered transport using the TCP one            */
 
-/* Authors: Martin Quinson                                                  */
-/* Copyright (C) 2004 Martin Quinson.                                       */
+/* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
 
 /* This program is free software; you can redistribute it and/or modify it
  under the terms of the license (GNU LGPL) which comes with this package. */
* under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include <netinet/in.h>   /* htonl/ntohl */
 #include <stdlib.h>
 #include <string.h>       /* memset */
 
+#include "portable.h"
 #include "xbt/misc.h"
+#include "xbt/sysdep.h"
 #include "transport_private.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_buf,transport,
@@ -53,7 +53,7 @@ typedef struct {
  ***/
 
 typedef struct {
-  uint32_t size;
+  int size;
   char *data;
   int pos; /* for receive; not exchanged over the net */
 } gras_trp_buf_t;
@@ -151,6 +151,7 @@ gras_trp_buf_socket_accept(gras_socket_t  sock,
   TRY(super->socket_accept(sock,dst));
   (*dst)->plugin = sock->plugin;
   gras_trp_buf_init_sock(*dst);
+  XBT_OUT;
   return no_error;
 }
 
@@ -159,13 +160,20 @@ void gras_trp_buf_socket_close(gras_socket_t sock){
   gras_trp_bufdata_t *data=sock->bufdata;
 
   XBT_IN;
-  if (data->in.size || data->out.size)
+  if (data->in.size!=data->in.pos) {
+     WARN1("Socket closed, but %d bytes were unread",data->in.size - data->in.pos);
+  }
+   
+  if (data->out.size!=data->out.pos) {
+    DEBUG2("Flush the socket before closing (in=%d,out=%d)",data->in.size, data->out.size);
     gras_trp_buf_flush(sock);
+  }
+   
   if (data->in.data)
-    xbt_free(data->in.data);
+    free(data->in.data);
   if (data->out.data)
-    xbt_free(data->out.data);
-  xbt_free(data);
+    free(data->out.data);
+  free(data);
 
   super->socket_close(sock);
 }
@@ -237,10 +245,10 @@ gras_trp_buf_chunk_recv(gras_socket_t sock,
     long int thissize;
 
     if (data->in.size == data->in.pos) { /* out of data. Get more */
-      uint32_t nextsize;
+      int nextsize;
       DEBUG0("Recv the size");
       TRY(super->chunk_recv(sock,(char*)&nextsize, 4));
-      data->in.size = ntohl(nextsize);
+      data->in.size = (int)ntohl(nextsize);
 
       VERB1("Recv the chunk (size=%d)",data->in.size);
       TRY(super->chunk_recv(sock, data->in.data, data->in.size));
@@ -271,13 +279,18 @@ gras_trp_buf_chunk_recv(gras_socket_t sock,
 xbt_error_t 
 gras_trp_buf_flush(gras_socket_t sock) {
   xbt_error_t errcode;
-  uint32_t size;
+  int size;
   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
   gras_trp_bufdata_t *data=sock->bufdata;
 
   XBT_IN;
-  size = htonl(data->out.size);
-  DEBUG1("Send the size (=%d)",data->out.size);
+  if (! (data->out.size-data->out.pos) ) {
+     DEBUG0("Nothing to flush");
+     return no_error;
+  }
+   
+  size = (int)htonl(data->out.size-data->out.pos);
+  DEBUG1("Send the size (=%d)",data->out.size-data->out.pos);
   TRY(super->chunk_send(sock,(char*) &size, 4));
 
   DEBUG1("Send the chunk (size=%d)",data->out.size);