2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/server_tc.c
23 * @brief convenience functions for transmission of
24 * complex responses as a server
25 * @author Christian Grothoff
29 #include "gnunet_common.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_scheduler_lib.h"
32 #include "gnunet_server_lib.h"
33 #include "gnunet_time_lib.h"
38 * How much buffer space do we want to have at least
39 * before transmitting another increment?
41 #define MIN_BLOCK_SIZE 128
45 struct GNUNET_SERVER_TransmitContext
48 * Which client are we transmitting to?
50 struct GNUNET_SERVER_Client *client;
53 * Transmission buffer. (current offset for writing).
58 * Number of bytes in buf.
63 * Offset for writing in buf.
68 * Timeout for this request.
70 struct GNUNET_TIME_Absolute timeout;
75 * Helper function for incremental transmission of the response.
78 transmit_response (void *cls, size_t size, void *buf)
80 struct GNUNET_SERVER_TransmitContext *tc = cls;
84 GNUNET_SERVER_receive_done (tc->client, GNUNET_SYSERR);
85 GNUNET_free_non_null (tc->buf);
89 if (tc->total - tc->off > size)
92 msize = tc->total - tc->off;
93 memcpy (buf, &tc->buf[tc->off], msize);
95 if (tc->total == tc->off)
97 GNUNET_SERVER_receive_done (tc->client, GNUNET_OK);
98 GNUNET_free_non_null (tc->buf);
103 if (NULL == GNUNET_SERVER_notify_transmit_ready (tc->client,
106 tc->total - tc->off),
107 GNUNET_TIME_absolute_get_remaining
113 GNUNET_SERVER_receive_done (tc->client, GNUNET_SYSERR);
114 GNUNET_free_non_null (tc->buf);
123 * Create a new transmission context for the
126 * @param client client to create the context for.
127 * @return NULL on error
129 struct GNUNET_SERVER_TransmitContext *
130 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client)
132 struct GNUNET_SERVER_TransmitContext *tc;
134 GNUNET_assert (client != NULL);
135 tc = GNUNET_malloc (sizeof (struct GNUNET_SERVER_TransmitContext));
142 * Append a message to the transmission context.
143 * All messages in the context will be sent by
144 * the transmit_context_run method.
146 * @param tc context to use
147 * @param data what to append to the result message
148 * @param length length of data
149 * @param type type of the message
152 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
153 *tc, const void *data, size_t length,
156 struct GNUNET_MessageHeader *msg;
159 GNUNET_assert (length < GNUNET_SERVER_MAX_MESSAGE_SIZE);
160 size = length + sizeof (struct GNUNET_MessageHeader);
161 GNUNET_assert (size > length);
162 tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
163 msg = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
165 msg->size = htons (size);
166 msg->type = htons (type);
167 memcpy (&msg[1], data, length);
172 * Append a message to the transmission context.
173 * All messages in the context will be sent by
174 * the transmit_context_run method.
176 * @param tc context to use
177 * @param msg message to append
180 GNUNET_SERVER_transmit_context_append_message(struct GNUNET_SERVER_TransmitContext
181 *tc, const struct GNUNET_MessageHeader *msg)
183 struct GNUNET_MessageHeader *m;
186 size = ntohs (msg->size);
187 tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
188 m = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
190 memcpy (m, msg, size);
195 * Execute a transmission context. If there is
196 * an error in the transmission, the receive_done
197 * method will be called with an error code (GNUNET_SYSERR),
198 * otherwise with GNUNET_OK.
200 * @param tc transmission context to use
201 * @param timeout when to time out and abort the transmission
204 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
205 struct GNUNET_TIME_Relative timeout)
207 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
209 GNUNET_SERVER_notify_transmit_ready (tc->client,
210 GNUNET_MIN (MIN_BLOCK_SIZE,
212 &transmit_response, tc))
215 GNUNET_SERVER_receive_done (tc->client, GNUNET_SYSERR);
216 GNUNET_free_non_null (tc->buf);
221 /* end of server_tc.c */