Merge branch 'mesa_7_5_branch'
[mesa.git] / src / gallium / auxiliary / rbug / rbug_connection.c
1 /*
2 * Copyright 2009 VMware, Inc.
3 * All Rights Reserved.
4 *
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:
11 *
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.
15 *
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.
23 */
24
25 #include "rbug/rbug.h"
26 #include "rbug/rbug_internal.h"
27
28 #include "util/u_network.h"
29
30 struct rbug_connection
31 {
32 int socket;
33 uint32_t send_serial;
34 uint32_t recv_serial;
35 enum rbug_opcode opcode;
36 };
37
38 /**
39 * Create a rbug connection from a socket created with u_socket.
40 *
41 * Result:
42 * A new allocated connection using socket as communication path
43 */
44 struct rbug_connection *
45 rbug_from_socket(int socket)
46 {
47 struct rbug_connection *c = CALLOC_STRUCT(rbug_connection);
48 c->socket = socket;
49 return c;
50 }
51
52 /**
53 * Free a connection, also closes socket.
54 */
55 void
56 rbug_disconnect(struct rbug_connection *c)
57 {
58 u_socket_close(c->socket);
59 FREE(c);
60 }
61
62 /**
63 * Waits for a message to be fully received.
64 * Also returns the serial for the message, serial is not touched for replys.
65 *
66 * Result:
67 * demarshaled message on success, NULL on connection error
68 */
69 struct rbug_header *
70 rbug_get_message(struct rbug_connection *c, uint32_t *serial)
71 {
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;
78
79
80 ret = u_socket_peek(c->socket, &header, sizeof(header));
81 if (ret <= 0) {
82 return NULL;
83 }
84
85 length = (size_t)header.length * 4;
86 data = MALLOC(length);
87 if (!data) {
88 return NULL;
89 }
90
91 do {
92 uint8_t *ptr = ((uint8_t*)data) + read;
93 ret = u_socket_recv(c->socket, ptr, length - read);
94
95 if (ret <= 0) {
96 FREE(data);
97 return NULL;
98 }
99
100 read += ret;
101 } while(read < length);
102
103 out = rbug_demarshal(data);
104 if (!out)
105 FREE(data);
106 else if (serial)
107 *serial = c->recv_serial++;
108 else
109 c->recv_serial++;
110
111 return out;
112 }
113
114 /**
115 * Frees a message and associated data.
116 */
117 void
118 rbug_free_header(struct rbug_header *header)
119 {
120 if (!header)
121 return;
122
123 FREE(header->__message);
124 FREE(header);
125 }
126
127 /**
128 * Internal function used by rbug_send_* functions.
129 *
130 * Start sending a message.
131 */
132 int
133 rbug_connection_send_start(struct rbug_connection *c, enum rbug_opcode opcode, uint32_t length)
134 {
135 c->opcode = opcode;
136 return 0;
137 }
138
139 /**
140 * Internal function used by rbug_send_* functions.
141 *
142 * Write data to the socket.
143 */
144 int
145 rbug_connection_write(struct rbug_connection *c, void *to, uint32_t size)
146 {
147 int ret = u_socket_send(c->socket, to, size);
148 return ret;
149 }
150
151 /**
152 * Internal function used by rbug_send_* functions.
153 *
154 * Finish writeing data to the socket.
155 * Ups the send_serial and sets the serial argument if supplied.
156 */
157 int rbug_connection_send_finish(struct rbug_connection *c, uint32_t *serial)
158 {
159 if (c->opcode < 0)
160 return 0;
161 else if (serial)
162 *serial = c->send_serial++;
163 else
164 c->send_serial++;
165
166 return 0;
167 }