2 This file is part of GNUnet.
3 Copyright (C) 2009 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_util_lib.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
36 * How much buffer space do we want to have at least
37 * before transmitting another increment?
39 #define MIN_BLOCK_SIZE 128
43 struct GNUNET_SERVER_TransmitContext
46 * Which client are we transmitting to?
48 struct GNUNET_SERVER_Client *client;
51 * Transmission buffer. (current offset for writing).
56 * Number of bytes in buf.
61 * Offset for writing in buf.
66 * Timeout for this request.
68 struct GNUNET_TIME_Absolute timeout;
73 * Helper function for incremental transmission of the response.
76 transmit_response (void *cls, size_t size, void *buf)
78 struct GNUNET_SERVER_TransmitContext *tc = cls;
83 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
86 if (tc->total - tc->off > size)
89 msize = tc->total - tc->off;
90 memcpy (buf, &tc->buf[tc->off], msize);
92 if (tc->total == tc->off)
94 GNUNET_SERVER_receive_done (tc->client, GNUNET_OK);
95 GNUNET_SERVER_client_drop (tc->client);
96 GNUNET_free_non_null (tc->buf);
102 GNUNET_SERVER_notify_transmit_ready (tc->client,
103 GNUNET_MIN (MIN_BLOCK_SIZE,
104 tc->total - tc->off),
105 GNUNET_TIME_absolute_get_remaining
106 (tc->timeout), &transmit_response,
110 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
118 * Create a new transmission context for the
121 * @param client client to create the context for.
122 * @return NULL on error
124 struct GNUNET_SERVER_TransmitContext *
125 GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client)
127 struct GNUNET_SERVER_TransmitContext *tc;
129 GNUNET_assert (NULL != client);
130 tc = GNUNET_new (struct GNUNET_SERVER_TransmitContext);
131 GNUNET_SERVER_client_keep (client);
138 * Append a message to the transmission context.
139 * All messages in the context will be sent by
140 * the transmit_context_run method.
142 * @param tc context to use
143 * @param data what to append to the result message
144 * @param length length of data
145 * @param type type of the message
148 GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext
149 *tc, const void *data,
150 size_t length, uint16_t type)
152 struct GNUNET_MessageHeader *msg;
155 GNUNET_assert (length < GNUNET_SERVER_MAX_MESSAGE_SIZE);
156 size = length + sizeof (struct GNUNET_MessageHeader);
157 GNUNET_assert (size > length);
158 tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
159 msg = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
161 msg->size = htons (size);
162 msg->type = htons (type);
163 memcpy (&msg[1], data, length);
168 * Append a message to the transmission context.
169 * All messages in the context will be sent by
170 * the transmit_context_run method.
172 * @param tc context to use
173 * @param msg message to append
176 GNUNET_SERVER_transmit_context_append_message (struct
177 GNUNET_SERVER_TransmitContext
179 const struct GNUNET_MessageHeader
182 struct GNUNET_MessageHeader *m;
185 size = ntohs (msg->size);
186 tc->buf = GNUNET_realloc (tc->buf, tc->total + size);
187 m = (struct GNUNET_MessageHeader *) &tc->buf[tc->total];
189 memcpy (m, msg, size);
194 * Execute a transmission context. If there is
195 * an error in the transmission, the #GNUNET_SERVER_receive_done()
196 * method will be called with an error code (#GNUNET_SYSERR),
197 * otherwise with #GNUNET_OK.
199 * @param tc transmission context to use
200 * @param timeout when to time out and abort the transmission
203 GNUNET_SERVER_transmit_context_run (struct GNUNET_SERVER_TransmitContext *tc,
204 struct GNUNET_TIME_Relative timeout)
206 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
208 GNUNET_SERVER_notify_transmit_ready (tc->client,
209 GNUNET_MIN (MIN_BLOCK_SIZE,
211 &transmit_response, tc))
214 GNUNET_SERVER_transmit_context_destroy (tc, GNUNET_SYSERR);
220 * Destroy a transmission context. This function must not be called
221 * after 'GNUNET_SERVER_transmit_context_run'.
223 * @param tc transmission context to destroy
224 * @param success code to give to 'GNUNET_SERVER_receive_done' for
225 * the client: GNUNET_OK to keep the connection open and
226 * continue to receive
227 * GNUNET_NO to close the connection (normal behavior)
228 * GNUNET_SYSERR to close the connection (signal
232 GNUNET_SERVER_transmit_context_destroy (struct GNUNET_SERVER_TransmitContext
235 GNUNET_SERVER_receive_done (tc->client, success);
236 GNUNET_SERVER_client_drop (tc->client);
237 GNUNET_free_non_null (tc->buf);
242 /* end of server_tc.c */