+ DEBUG0("Flush");
+ if (data->out == buffering_buf) {
+ if (XBT_LOG_ISENABLED(gras_trp_tcp,xbt_log_priority_debug))
+ hexa_print("chunk to send ",
+ (unsigned char *) data->out_buf.data,data->out_buf.size);
+ if ((data->out_buf.size - data->out_buf.pos) != 0) {
+ DEBUG3("Send the chunk (size=%d) to %s:%d",data->out_buf.size,
+ gras_socket_peer_name(sock),gras_socket_peer_port(sock));
+ gras_trp_tcp_send(sock, data->out_buf.data, data->out_buf.size);
+ VERB1("Chunk sent (size=%d)",data->out_buf.size);
+ data->out_buf.size = 0;
+ }
+ }
+
+#ifdef HAVE_READV
+ if (data->out == buffering_iov) {
+ DEBUG0("Flush out iov");
+ vect = sock->bufdata->out_buf_v;
+ if ((size = xbt_dynar_length(vect))) {
+ DEBUG1("Flush %d chunks out of this socket",size);
+ writev(sock->sd,xbt_dynar_get_ptr(vect,0),size);
+ xbt_dynar_reset(vect);
+ }
+ data->out_buf.size = 0; /* reset the buffer containing non-stable data */
+ }
+
+ if (data->in == buffering_iov) {
+ DEBUG0("Flush in iov");
+ vect = sock->bufdata->in_buf_v;
+ if ((size = xbt_dynar_length(vect))) {
+ DEBUG1("Get %d chunks from of this socket",size);
+ readv(sock->sd,xbt_dynar_get_ptr(vect,0),size);
+ xbt_dynar_reset(vect);
+ }
+ }
+#endif
+}
+static void
+gras_trp_buf_send(gras_socket_t sock,
+ const char *chunk,
+ unsigned long int size,
+ int stable_ignored) {
+
+ gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
+ int chunk_pos=0;
+
+ XBT_IN;
+
+ while (chunk_pos < size) {
+ /* size of the chunk to receive in that shot */
+ long int thissize = min(size-chunk_pos,data->buffsize-data->out_buf.size);
+ DEBUG4("Set the chars %d..%ld into the buffer; size=%ld, ctn=(%s)",
+ (int)data->out_buf.size,
+ ((int)data->out_buf.size) + thissize -1,
+ size,
+ hexa_str((unsigned char*)chunk,thissize,0));
+
+ memcpy(data->out_buf.data + data->out_buf.size, chunk + chunk_pos, thissize);
+
+ data->out_buf.size += thissize;
+ chunk_pos += thissize;
+ DEBUG4("New pos = %d; Still to send = %ld of %ld; ctn sofar=(%s)",
+ data->out_buf.size,size-chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
+
+ if (data->out_buf.size == data->buffsize) /* out of space. Flush it */
+ gras_trp_bufiov_flush(sock);
+ }
+
+ XBT_OUT;
+}
+
+static int
+gras_trp_buf_recv(gras_socket_t sock,
+ char *chunk,
+ unsigned long int size) {
+
+ gras_trp_bufdata_t *data=sock->bufdata;
+ long int chunk_pos = 0;
+
+ XBT_IN;
+
+ while (chunk_pos < size) {
+ /* size of the chunk to receive in that shot */
+ long int thissize;
+
+ if (data->in_buf.size == data->in_buf.pos) { /* out of data. Get more */
+
+ DEBUG2("Get more data (size=%d,bufsize=%d)",
+ (int)MIN(size-chunk_pos,data->buffsize),
+ (int)data->buffsize);
+
+
+ data->in_buf.size =
+ gras_trp_tcp_recv_withbuffer(sock, data->in_buf.data,
+ MIN(size-chunk_pos,data->buffsize),
+ data->buffsize);
+
+ data->in_buf.pos=0;
+ }
+
+ thissize = min(size-chunk_pos , data->in_buf.size - data->in_buf.pos);
+ memcpy(chunk+chunk_pos, data->in_buf.data + data->in_buf.pos, thissize);
+
+ data->in_buf.pos += thissize;
+ chunk_pos += thissize;
+ DEBUG4("New pos = %d; Still to receive = %ld of %ld. Ctn so far=(%s)",
+ data->in_buf.pos,size - chunk_pos,size,hexa_str((unsigned char*)chunk,chunk_pos,0));
+ }
+ /* indicate on need to the gras_select function that there is more to read on this socket so that it does not actually select */
+ sock->moredata = (data->in_buf.size > data->in_buf.pos);
+ DEBUG1("There is %smore data",(sock->moredata?"":"no "));
+
+ XBT_OUT;
+ return chunk_pos;
+}
+
+/*****************************************/
+/****[ end of BUFFERED DATA EXCHANGE ]****/
+/*****************************************/
+
+/********************************/
+/****[ VECTOR DATA EXCHANGE ]****/
+/********************************/
+#ifdef HAVE_READV
+static void
+gras_trp_iov_send(gras_socket_t sock,
+ const char *chunk,
+ unsigned long int size,
+ int stable) {
+ struct iovec elm;
+ gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
+
+
+ DEBUG1("Buffer one chunk to be sent later (%s)",
+ hexa_str((char*)chunk,size,0));
+
+ elm.iov_len = (size_t)size;
+
+ if (!stable) {
+ /* data storage won't last until flush. Save it in a buffer if we can */
+
+ if (size > data->buffsize-data->out_buf.size) {
+ /* buffer too small:
+ flush the socket, using data in its actual storage */
+ elm.iov_base = (void*)chunk;
+ xbt_dynar_push(data->out_buf_v,&elm);
+
+ gras_trp_bufiov_flush(sock);
+ return;
+ } else {
+ /* buffer big enough:
+ copy data into it, and chain it for upcoming writev */
+ memcpy(data->out_buf.data + data->out_buf.size, chunk, size);
+ elm.iov_base = (void*)(data->out_buf.data + data->out_buf.size);
+ data->out_buf.size += size;
+
+ xbt_dynar_push(data->out_buf_v,&elm);
+ }
+
+ } else {
+ /* data storage stable. Chain it */
+
+ elm.iov_base = (void*)chunk;
+ xbt_dynar_push(data->out_buf_v,&elm);
+ }
+}
+static int
+gras_trp_iov_recv(gras_socket_t sock,
+ char *chunk,
+ unsigned long int size) {
+ struct iovec elm;
+
+ DEBUG0("Buffer one chunk to be received later");
+ elm.iov_base = (void*)chunk;
+ elm.iov_len = (size_t)size;
+ xbt_dynar_push(sock->bufdata->in_buf_v,&elm);
+
+ return size;
+}
+
+#endif
+/***************************************/
+/****[ end of VECTOR DATA EXCHANGE ]****/
+/***************************************/
+
+
+/***
+ *** Prototypes of BUFFERED
+ ***/
+
+void gras_trp_buf_socket_client(gras_trp_plugin_t self,
+ gras_socket_t sock);
+void gras_trp_buf_socket_server(gras_trp_plugin_t self,
+ gras_socket_t sock);
+gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
+
+void gras_trp_buf_socket_close(gras_socket_t sd);
+
+
+gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) {
+ gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
+
+ data->buffsize = 100 * 1024 ; /* 100k */
+
+ data->in_buf.size = 0;
+ data->in_buf.data = xbt_malloc(data->buffsize);
+ data->in_buf.pos = 0; /* useless, indeed, since size==pos */
+
+ data->out_buf.size = 0;
+ data->out_buf.data = xbt_malloc(data->buffsize);
+ data->out_buf.pos = data->out_buf.size;
+
+#ifdef HAVE_READV
+ data->in_buf_v = data->out_buf_v = NULL;
+ data->in_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
+ data->out_buf_v=xbt_dynar_new(sizeof(struct iovec),NULL);
+ data->out = buffering_iov;
+#else
+ data->out = buffering_buf;
+#endif
+
+ data->in = buffering_buf;
+
+ sock->bufdata = data;
+ return sock;