Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pre-alloc the buffers
[simgrid.git] / src / gras / Transport / transport_plugin_buf.c
1 /* $Id$ */
2
3 /* buf trp (transport) - buffered transport using the TCP one            */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2004 Martin Quinson.                                       */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <netinet/in.h>   /* htonl/ntohl */
12 #include <stdlib.h>
13 #include <string.h>       /* memset */
14
15 #include "gras_private.h"
16 #include "transport_private.h"
17
18 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_buf,transport);
19
20 /***
21  *** Prototypes 
22  ***/
23 gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
24                                         const char *host,
25                                         unsigned short port,
26                                         /* OUT */ gras_socket_t *sock);
27 gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
28                                         unsigned short port,
29                                         /* OUT */ gras_socket_t *sock);
30 gras_error_t gras_trp_buf_socket_accept(gras_socket_t  *sock,
31                                         gras_socket_t **dst);
32
33 void         gras_trp_buf_socket_close(gras_socket_t *sd);
34   
35 gras_error_t gras_trp_buf_chunk_send(gras_socket_t *sd,
36                                      const char *data,
37                                      long int size);
38
39 gras_error_t gras_trp_buf_chunk_recv(gras_socket_t *sd,
40                                      char *data,
41                                      long int size);
42 gras_error_t gras_trp_buf_flush(gras_socket_t  *sock);
43
44
45 /***
46  *** Specific plugin part
47  ***/
48
49 typedef struct {
50   gras_trp_plugin_t *super;
51 } gras_trp_buf_plug_data_t;
52
53 /***
54  *** Specific socket part
55  ***/
56
57 typedef struct {
58   uint32_t size;
59   char *data;
60   int pos; /* for receive; not exchanged over the net */
61 } gras_trp_buf_t;
62
63 struct gras_trp_bufdata_{
64   gras_trp_buf_t in;
65   gras_trp_buf_t out;
66   int buffsize;
67 };
68
69 gras_error_t gras_trp_buf_init_sock(gras_socket_t *sock) {
70   gras_trp_bufdata_t *data=malloc(sizeof(gras_trp_bufdata_t));
71   
72   GRAS_IN;
73   if (!data)
74     RAISE_MALLOC;
75   data->in.size  = 0;
76   if (!(data->in.data = malloc(data->buffsize)))
77     RAISE_MALLOC;
78   data->in.pos   = 0; /* useless, indeed, since size==pos */
79   data->out.size = 0;
80   if (!(data->out.data = malloc(data->buffsize)))
81     RAISE_MALLOC;
82   data->out.pos  = 0;
83   //  data->buffsize = 32 * 1024 - 4; /* default socket buffsize (32k) - headers */ 
84   data->buffsize = 100 * 1024 ; /* 100k */ 
85   sock->bufdata = data;
86   return no_error;
87 }
88
89
90 /***
91  *** Code
92  ***/
93 gras_error_t
94 gras_trp_buf_setup(gras_trp_plugin_t *plug) {
95   gras_error_t errcode;
96   gras_trp_buf_plug_data_t *data =malloc(sizeof(gras_trp_buf_plug_data_t));
97   if (!data)
98     RAISE_MALLOC;
99
100   GRAS_IN;
101   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
102                                   &(data->super)));
103   DEBUG1("Derivate a buffer plugin from %s",gras_if_RL() ? "tcp" : "sg");
104
105   plug->socket_client = gras_trp_buf_socket_client;
106   plug->socket_server = gras_trp_buf_socket_server;
107   plug->socket_accept = gras_trp_buf_socket_accept;
108   plug->socket_close  = gras_trp_buf_socket_close;
109
110   plug->chunk_send    = gras_trp_buf_chunk_send;
111   plug->chunk_recv    = gras_trp_buf_chunk_recv;
112
113   plug->flush         = gras_trp_buf_flush;
114
115   plug->data = (void*)data;
116   plug->exit = NULL;
117   
118   return no_error;
119 }
120
121 gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
122                                         const char *host,
123                                         unsigned short port,
124                                         /* OUT */ gras_socket_t *sock){
125   gras_error_t errcode;
126   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
127
128   GRAS_IN;
129   TRY(super->socket_client(super,host,port,sock));
130   sock->plugin = self;
131   TRY(gras_trp_buf_init_sock(sock));
132     
133   return no_error;
134 }
135
136 /**
137  * gras_trp_buf_socket_server:
138  *
139  * Open a socket used to receive messages.
140  */
141 gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
142                                         unsigned short port,
143                                         /* OUT */ gras_socket_t *sock){
144   gras_error_t errcode;
145   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
146
147   GRAS_IN;
148   TRY(super->socket_server(super,port,sock));
149   sock->plugin = self;
150   TRY(gras_trp_buf_init_sock(sock));
151   return no_error;
152 }
153
154 gras_error_t
155 gras_trp_buf_socket_accept(gras_socket_t  *sock,
156                            gras_socket_t **dst) {
157   gras_error_t errcode;
158   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
159       
160   GRAS_IN;
161   TRY(super->socket_accept(sock,dst));
162   (*dst)->plugin = sock->plugin;
163   TRY(gras_trp_buf_init_sock(*dst));
164   return no_error;
165 }
166
167 void gras_trp_buf_socket_close(gras_socket_t *sock){
168   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
169   gras_trp_bufdata_t *data=sock->bufdata;
170
171   GRAS_IN;
172   if (data->in.size || data->out.size)
173     gras_trp_buf_flush(sock);
174   if (data->in.data)
175     free(data->in.data);
176   if (data->out.data)
177     free(data->out.data);
178   free(data);
179
180   return super->socket_close(sock);
181 }
182
183 /**
184  * gras_trp_buf_chunk_send:
185  *
186  * Send data on a TCP socket
187  */
188 gras_error_t 
189 gras_trp_buf_chunk_send(gras_socket_t *sock,
190                         const char *chunk,
191                         long int size) {
192
193   gras_error_t errcode;
194   gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
195   int chunk_pos=0;
196
197   GRAS_IN;
198   /* Let underneath plugin check for direction, we work even in duplex */
199   gras_assert0(size >= 0, "Cannot send a negative amount of data");
200
201   while (chunk_pos < size) {
202     /* size of the chunck to receive in that shot */
203     long int thissize = min(size-chunk_pos,data->buffsize - data->out.size);
204     DEBUG5("Set the chars %d..%ld into the buffer (size=%ld, ctn='%.*s')",
205            (int)data->out.size,
206            ((int)data->out.size) + thissize -1,
207            size, chunk_pos, chunk);
208
209     memcpy(data->out.data + data->out.size, chunk + chunk_pos, thissize);
210
211     data->out.size += thissize;
212     chunk_pos      += thissize;
213     DEBUG5("New pos = %d; Still to send = %ld of %ld; ctn sofar='%.*s'",
214            data->out.size,size-chunk_pos,size,(int)chunk_pos,chunk);
215
216     if (data->out.size == data->buffsize) /* out of space. Flush it */
217       TRY(gras_trp_buf_flush(sock));
218   }
219
220   GRAS_OUT;
221   return no_error;
222 }
223
224 /**
225  * gras_trp_buf_chunk_recv:
226  *
227  * Receive data on a TCP socket.
228  */
229 gras_error_t 
230 gras_trp_buf_chunk_recv(gras_socket_t *sock,
231                         char *chunk,
232                         long int size) {
233
234   gras_error_t errcode;
235   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
236   gras_trp_bufdata_t *data=sock->bufdata;
237   long int chunck_pos = 0;
238
239   /* Let underneath plugin check for direction, we work even in duplex */
240   gras_assert0(sock, "Cannot recv on an NULL socket");
241   gras_assert0(size >= 0, "Cannot receive a negative amount of data");
242   
243   GRAS_IN;
244
245   while (chunck_pos < size) {
246     /* size of the chunck to receive in that shot */
247     long int thissize;
248
249     if (data->in.size == data->in.pos) { /* out of data. Get more */
250       uint32_t nextsize;
251       DEBUG0("Recv the size");
252       TRY(super->chunk_recv(sock,(char*)&nextsize, 4));
253       data->in.size = ntohl(nextsize);
254
255       VERB1("Recv the chunk (size=%d)",data->in.size);
256       TRY(super->chunk_recv(sock, data->in.data, data->in.size));
257       data->in.pos=0;
258     }
259     
260     thissize = min(size-chunck_pos ,  data->in.size - data->in.pos);
261     DEBUG2("Get the chars %d..%ld out of the buffer",
262            data->in.pos,
263            data->in.pos + thissize - 1);
264     memcpy(chunk+chunck_pos, data->in.data + data->in.pos, thissize);
265
266     data->in.pos += thissize;
267     chunck_pos   += thissize;
268     DEBUG5("New pos = %d; Still to receive = %ld of %ld. Ctn so far='%.*s'",
269            data->in.pos,size - chunck_pos,size,(int)chunck_pos,chunk);
270   }
271
272   GRAS_OUT;
273   return no_error;
274 }
275
276 /**
277  * gras_trp_buf_flush:
278  *
279  * Make sure the data is sent
280  */
281 gras_error_t 
282 gras_trp_buf_flush(gras_socket_t *sock) {
283   gras_error_t errcode;
284   uint32_t size;
285   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
286   gras_trp_bufdata_t *data=sock->bufdata;
287
288   GRAS_IN;
289   size = htonl(data->out.size);
290   DEBUG1("Send the size (=%d)",data->out.size);
291   TRY(super->chunk_send(sock,(char*) &size, 4));
292
293   DEBUG1("Send the chunk (size=%d)",data->out.size);
294   TRY(super->chunk_send(sock, data->out.data, data->out.size));
295   VERB1("Chunk sent (size=%d)",data->out.size);
296   data->out.size = 0;
297   return no_error;
298 }