revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / gallium / auxiliary / rbug / rbug_connection.c
blobae4e27f9f6b99dd6b3122aa4d1e781f610ae2e21
1 /*
2 * Copyright 2009 VMware, Inc.
3 * All Rights Reserved.
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
14 * Software.
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
32 int socket;
33 uint32_t send_serial;
34 uint32_t recv_serial;
35 enum rbug_opcode opcode;
38 /**
39 * Create a rbug connection from a socket created with u_socket.
41 * Result:
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);
48 c->socket = socket;
49 return c;
52 /**
53 * Free a connection, also closes socket.
55 void
56 rbug_disconnect(struct rbug_connection *c)
58 u_socket_close(c->socket);
59 FREE(c);
62 /**
63 * Waits for a message to be fully received.
64 * Also returns the serial for the message, serial is not touched for replys.
66 * Result:
67 * demarshaled message on success, NULL on connection error
69 struct rbug_header *
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;
75 size_t length = 0;
76 size_t read = 0;
77 int ret;
80 ret = u_socket_peek(c->socket, &header, sizeof(header));
81 if (ret <= 0) {
82 return NULL;
85 length = (size_t)header.length * 4;
86 data = MALLOC(length);
87 if (!data) {
88 return NULL;
90 data->opcode = 0;
92 do {
93 uint8_t *ptr = ((uint8_t*)data) + read;
94 ret = u_socket_recv(c->socket, ptr, length - read);
96 if (ret <= 0) {
97 FREE(data);
98 return NULL;
101 read += ret;
102 } while(read < length);
104 out = rbug_demarshal(data);
105 if (!out)
106 FREE(data);
107 else if (serial)
108 *serial = c->recv_serial++;
109 else
110 c->recv_serial++;
112 return out;
116 * Frees a message and associated data.
118 void
119 rbug_free_header(struct rbug_header *header)
121 if (!header)
122 return;
124 FREE(header->__message);
125 FREE(header);
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)
136 c->opcode = opcode;
137 return 0;
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);
149 return ret;
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)
160 if (c->opcode < 0)
161 return 0;
162 else if (serial)
163 *serial = c->send_serial++;
164 else
165 c->send_serial++;
167 return 0;