2 * Copyright 2009 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "rbug/rbug.h"
26 #include "rbug/rbug_internal.h"
28 #include "util/u_network.h"
30 struct rbug_connection
35 enum rbug_opcode opcode;
39 * Create a rbug connection from a socket created with u_socket.
42 * A new allocated connection using socket as communication path
44 struct rbug_connection *
45 rbug_from_socket(int socket)
47 struct rbug_connection *c = CALLOC_STRUCT(rbug_connection);
53 * Free a connection, also closes socket.
56 rbug_disconnect(struct rbug_connection *c)
58 u_socket_close(c->socket);
63 * Waits for a message to be fully received.
64 * Also returns the serial for the message, serial is not touched for replys.
67 * demarshaled message on success, NULL on connection error
70 rbug_get_message(struct rbug_connection *c, uint32_t *serial)
72 struct rbug_proto_header header;
73 struct rbug_header *out;
74 struct rbug_proto_header *data;
80 ret = u_socket_peek(c->socket, &header, sizeof(header));
85 length = (size_t)header.length * 4;
86 data = MALLOC(length);
93 uint8_t *ptr = ((uint8_t*)data) + read;
94 ret = u_socket_recv(c->socket, ptr, length - read);
102 } while(read < length);
104 out = rbug_demarshal(data);
108 *serial = c->recv_serial++;
116 * Frees a message and associated data.
119 rbug_free_header(struct rbug_header *header)
124 FREE(header->__message);
129 * Internal function used by rbug_send_* functions.
131 * Start sending a message.
134 rbug_connection_send_start(struct rbug_connection *c, enum rbug_opcode opcode, uint32_t length)
141 * Internal function used by rbug_send_* functions.
143 * Write data to the socket.
146 rbug_connection_write(struct rbug_connection *c, void *to, uint32_t size)
148 int ret = u_socket_send(c->socket, to, size);
153 * Internal function used by rbug_send_* functions.
155 * Finish writeing data to the socket.
156 * Ups the send_serial and sets the serial argument if supplied.
158 int rbug_connection_send_finish(struct rbug_connection *c, uint32_t *serial)
163 *serial = c->send_serial++;