Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initiate win32 cross-port
[simgrid.git] / src / gras / Transport / transport_plugin_tcp.c
index 25ee77f..753296f 100644 (file)
@@ -2,47 +2,50 @@
 
 /* tcp trp (transport) - send/receive a bunch of bytes from a tcp socket    */
 
-/* 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. */
-
-#include <unistd.h>       /* close() pipe() read() write() */
-#include <signal.h>       /* close() pipe() read() write() */
-#include <netinet/in.h>   /* sometimes required for #include <arpa/inet.h> */
-#include <netinet/tcp.h>  /* TCP_NODELAY */
-#include <arpa/inet.h>    /* inet_ntoa() */
-#include <netdb.h>        /* getprotobyname() */
-#include <sys/time.h>     /* struct timeval */
-#include <errno.h>        /* errno */
-#include <sys/wait.h>     /* waitpid() */
-#include <sys/socket.h>   /* getpeername() socket() */
-#include <stdlib.h>
-#include <string.h>       /* memset */
-
-#include "gras_private.h"
+ * under the terms of the license (GNU LGPL) which comes with this package. */
+
+#include "portable.h"
+
+#if 0
+#  include <unistd.h>       /* close() pipe() read() write() */
+#  include <signal.h>       /* close() pipe() read() write() */
+#  include <netinet/in.h>   /* sometimes required for #include <arpa/inet.h> */
+#  include <netinet/tcp.h>  /* TCP_NODELAY */
+#  include <arpa/inet.h>    /* inet_ntoa() */
+#  include <netdb.h>        /* getprotobyname() */
+#  include <sys/time.h>     /* struct timeval */
+#  include <errno.h>        /* errno */
+#  include <sys/wait.h>     /* waitpid() */
+#  include <sys/socket.h>   /* getpeername() socket() */
+#  include <stdlib.h>
+#  include <string.h>       /* memset */
+#endif
+
+
 #include "transport_private.h"
 
-GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_tcp,transport,"TCP transport");
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_tcp,transport,"TCP transport");
 
 /***
  *** Prototypes 
  ***/
-gras_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
-                                       /* OUT */ gras_socket_t *sock);
-gras_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
-                                       /* OUT */ gras_socket_t *sock);
-gras_error_t gras_trp_tcp_socket_accept(gras_socket_t  *sock,
-                                       gras_socket_t **dst);
-
-void         gras_trp_tcp_socket_close(gras_socket_t *sd);
+xbt_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
+                                       gras_socket_t sock);
+xbt_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
+                                       gras_socket_t sock);
+xbt_error_t gras_trp_tcp_socket_accept(gras_socket_t  sock,
+                                       gras_socket_t *dst);
+
+void         gras_trp_tcp_socket_close(gras_socket_t sd);
   
-gras_error_t gras_trp_tcp_chunk_send(gras_socket_t *sd,
+xbt_error_t gras_trp_tcp_chunk_send(gras_socket_t sd,
                                     const char *data,
                                     long int size);
 
-gras_error_t gras_trp_tcp_chunk_recv(gras_socket_t *sd,
+xbt_error_t gras_trp_tcp_chunk_recv(gras_socket_t sd,
                                     char *data,
                                     long int size);
 
@@ -71,9 +74,9 @@ typedef struct {
 /***
  *** Code
  ***/
-gras_error_t gras_trp_tcp_setup(gras_trp_plugin_t *plug) {
+xbt_error_t gras_trp_tcp_setup(gras_trp_plugin_t *plug) {
 
-  gras_trp_tcp_plug_data_t *data = gras_new(gras_trp_tcp_plug_data_t,1);
+  gras_trp_tcp_plug_data_t *data = xbt_new(gras_trp_tcp_plug_data_t,1);
 
   FD_ZERO(&(data->msg_socks));
   FD_ZERO(&(data->raw_socks));
@@ -96,11 +99,11 @@ gras_error_t gras_trp_tcp_setup(gras_trp_plugin_t *plug) {
 
 void gras_trp_tcp_exit(gras_trp_plugin_t *plug) {
   DEBUG1("Exit plugin TCP (free %p)", plug->data);
-  free(plug->data);
+  xbt_free(plug->data);
 }
 
-gras_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
-                                       /* OUT */ gras_socket_t *sock){
+xbt_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
+                                       gras_socket_t sock){
   
   struct sockaddr_in addr;
   struct hostent *he;
@@ -152,8 +155,8 @@ gras_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
  *
  * Open a socket used to receive messages.
  */
-gras_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
-                                       /* OUT */ gras_socket_t *sock){
+xbt_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
+                                       /* OUT */ gras_socket_t sock){
   int size = sock->bufSize * 1024; 
   int on = 1;
   struct sockaddr_in server;
@@ -200,11 +203,10 @@ gras_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
   return no_error;
 }
 
-gras_error_t
-gras_trp_tcp_socket_accept(gras_socket_t  *sock,
-                          gras_socket_t **dst) {
-  gras_socket_t *res;
-  gras_error_t errcode;
+xbt_error_t
+gras_trp_tcp_socket_accept(gras_socket_t  sock,
+                          gras_socket_t *dst) {
+  gras_socket_t res;
   
   struct sockaddr_in peer_in;
   socklen_t peer_in_len = sizeof(peer_in);
@@ -273,7 +275,7 @@ gras_trp_tcp_socket_accept(gras_socket_t  *sock,
   }
 }
 
-void gras_trp_tcp_socket_close(gras_socket_t *sock){
+void gras_trp_tcp_socket_close(gras_socket_t sock){
   gras_trp_tcp_plug_data_t *tcp;
   
   if (!sock) return; /* close only once */
@@ -313,13 +315,13 @@ void gras_trp_tcp_socket_close(gras_socket_t *sock){
  *
  * Send data on a TCP socket
  */
-gras_error_t 
-gras_trp_tcp_chunk_send(gras_socket_t *sock,
+xbt_error_t 
+gras_trp_tcp_chunk_send(gras_socket_t sock,
                        const char *data,
                        long int size) {
   
   /* TCP sockets are in duplex mode, don't check direction */
-  gras_assert0(size >= 0, "Cannot send a negative amount of data");
+  xbt_assert0(size >= 0, "Cannot send a negative amount of data");
 
   while (size) {
     int status = 0;
@@ -348,14 +350,14 @@ gras_trp_tcp_chunk_send(gras_socket_t *sock,
  *
  * Receive data on a TCP socket.
  */
-gras_error_t 
-gras_trp_tcp_chunk_recv(gras_socket_t *sock,
+xbt_error_t 
+gras_trp_tcp_chunk_recv(gras_socket_t sock,
                        char *data,
                        long int size) {
 
   /* TCP sockets are in duplex mode, don't check direction */
-  gras_assert0(sock, "Cannot recv on an NULL socket");
-  gras_assert0(size >= 0, "Cannot receive a negative amount of data");
+  xbt_assert0(sock, "Cannot recv on an NULL socket");
+  xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
   
   while (size) {
     int status = 0;
@@ -392,7 +394,7 @@ static int TcpProtoNumber(void) {
   
   if(returnValue == 0) {
     fetchedEntry = getprotobyname("tcp");
-    gras_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
+    xbt_assert0(fetchedEntry, "getprotobyname(tcp) gave NULL");
     returnValue = fetchedEntry->p_proto;
   }
   
@@ -404,7 +406,7 @@ static int TcpProtoNumber(void) {
    But I fail to find a good internal organization for now. We may want to split 
    raw and regular sockets more efficiently.
 */
-gras_error_t gras_socket_raw_exchange(gras_socket_t *peer,
+xbt_error_t gras_socket_raw_exchange(gras_socket_t peer,
                                      int sender,
                                      unsigned int timeout,
                                      unsigned long int exp_size,
@@ -413,14 +415,14 @@ gras_error_t gras_socket_raw_exchange(gras_socket_t *peer,
    int res_last, msg_sofar, exp_sofar;
    
    fd_set rd_set;
-   int rv;
+/*    int rv; */
    
    struct timeval timeOut;
    
-   chunk = gras_malloc(msg_size);
-   
-   for   (exp_sofar=0; exp_sofar < exp_size; exp_size += msg_sofar) {
-      for(msg_sofar=0; msg_sofar < msg_size; msg_size += res_last) {
+   chunk = xbt_malloc(msg_size);
+
+   for   (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
+      for(msg_sofar=0; msg_sofar < msg_size; msg_sofar += res_last) {
         
         if(sender) {
            res_last = send(peer->sd, chunk, msg_size - msg_sofar, 0);
@@ -437,12 +439,12 @@ gras_error_t gras_socket_raw_exchange(gras_socket_t *peer,
         }
         if (res_last == 0) {
           /* No progress done, bail out */
-          gras_free(chunk);
+          xbt_free(chunk);
           RAISE0(unknown_error,"Not exchanged a single byte, bailing out");
         }
       }
    }
    
-   gras_free(chunk);
+   xbt_free(chunk);
    return no_error;
 }