1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "server.h"
|
---|
8 | #include "cr_unpack.h"
|
---|
9 | #include "cr_error.h"
|
---|
10 | #include "cr_mem.h"
|
---|
11 | #include "server_dispatch.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Accept a new client connection, create a new CRClient and add to run queue.
|
---|
16 | */
|
---|
17 | void
|
---|
18 | crServerAddNewClient(void)
|
---|
19 | {
|
---|
20 | CRClient *newClient = (CRClient *) crCalloc(sizeof(CRClient));
|
---|
21 |
|
---|
22 | if (newClient) {
|
---|
23 | newClient->spu_id = cr_server.client_spu_id;
|
---|
24 | newClient->conn = crNetAcceptClient( cr_server.protocol, NULL,
|
---|
25 | cr_server.tcpip_port,
|
---|
26 | cr_server.mtu, 1 );
|
---|
27 |
|
---|
28 | newClient->currentCtxInfo = &cr_server.MainContextInfo;
|
---|
29 |
|
---|
30 | /* add to array */
|
---|
31 | cr_server.clients[cr_server.numClients++] = newClient;
|
---|
32 |
|
---|
33 | crServerAddToRunQueue( newClient );
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Check if client is in the run queue.
|
---|
40 | */
|
---|
41 | static GLboolean
|
---|
42 | FindClientInQueue(CRClient *client)
|
---|
43 | {
|
---|
44 | RunQueue *q = cr_server.run_queue;
|
---|
45 | while (q) {
|
---|
46 | if (q->client == client) {
|
---|
47 | return 1;
|
---|
48 | }
|
---|
49 | q = q->next;
|
---|
50 | if (q == cr_server.run_queue)
|
---|
51 | return 0; /* back head */
|
---|
52 | }
|
---|
53 | return 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | #if 0
|
---|
58 | static int
|
---|
59 | PrintQueue(void)
|
---|
60 | {
|
---|
61 | RunQueue *q = cr_server.run_queue;
|
---|
62 | int count = 0;
|
---|
63 | crDebug("Queue entries:");
|
---|
64 | while (q) {
|
---|
65 | count++;
|
---|
66 | crDebug("Entry: %p client: %p", q, q->client);
|
---|
67 | q = q->next;
|
---|
68 | if (q == cr_server.run_queue)
|
---|
69 | return count;
|
---|
70 | }
|
---|
71 | return count;
|
---|
72 | }
|
---|
73 | #endif
|
---|
74 |
|
---|
75 |
|
---|
76 | void crServerAddToRunQueue( CRClient *client )
|
---|
77 | {
|
---|
78 | RunQueue *q = (RunQueue *) crAlloc( sizeof( *q ) );
|
---|
79 |
|
---|
80 | #ifdef VBOX_WITH_CRHGSMI
|
---|
81 | client->conn->pClient = client;
|
---|
82 | CRVBOXHGSMI_CMDDATA_CLEANUP(&client->conn->CmdData);
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | /* give this client a unique number if needed */
|
---|
86 | if (!client->number) {
|
---|
87 | client->number = client->conn->u32ClientID;
|
---|
88 | }
|
---|
89 |
|
---|
90 | crDebug("Adding client %p to the run queue", client);
|
---|
91 |
|
---|
92 | if (FindClientInQueue(client)) {
|
---|
93 | crError("CRServer: client %p already in the queue!", client);
|
---|
94 | }
|
---|
95 |
|
---|
96 | q->client = client;
|
---|
97 | q->blocked = 0;
|
---|
98 |
|
---|
99 | if (!cr_server.run_queue)
|
---|
100 | {
|
---|
101 | /* adding to empty queue */
|
---|
102 | cr_server.run_queue = q;
|
---|
103 | q->next = q;
|
---|
104 | q->prev = q;
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | /* insert in doubly-linked list */
|
---|
109 | q->next = cr_server.run_queue->next;
|
---|
110 | cr_server.run_queue->next->prev = q;
|
---|
111 |
|
---|
112 | q->prev = cr_server.run_queue;
|
---|
113 | cr_server.run_queue->next = q;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | static void crServerCleanupClient(CRClient *client)
|
---|
118 | {
|
---|
119 | int32_t pos;
|
---|
120 | CRClient *oldclient = cr_server.curClient;
|
---|
121 |
|
---|
122 | cr_server.curClient = client;
|
---|
123 |
|
---|
124 | /* Destroy any windows created by the client */
|
---|
125 | for (pos = 0; pos<CR_MAX_WINDOWS; pos++)
|
---|
126 | {
|
---|
127 | if (client->windowList[pos])
|
---|
128 | {
|
---|
129 | cr_server.dispatch.WindowDestroy(client->windowList[pos]);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /* Check if we have context(s) made by this client left, could happen if client side code is lazy */
|
---|
134 | for (pos = 0; pos<CR_MAX_CONTEXTS; pos++)
|
---|
135 | {
|
---|
136 | if (client->contextList[pos])
|
---|
137 | {
|
---|
138 | cr_server.dispatch.DestroyContext(client->contextList[pos]);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | cr_server.curClient = oldclient;
|
---|
143 | }
|
---|
144 |
|
---|
145 | static void crServerCleanupByPID(uint64_t pid)
|
---|
146 | {
|
---|
147 | CRClientNode *pNode=cr_server.pCleanupClient, *pNext;
|
---|
148 |
|
---|
149 | while (pNode)
|
---|
150 | {
|
---|
151 | if (pNode->pClient->pid==pid)
|
---|
152 | {
|
---|
153 | crServerCleanupClient(pNode->pClient);
|
---|
154 | crFree(pNode->pClient);
|
---|
155 | if (pNode->prev)
|
---|
156 | {
|
---|
157 | pNode->prev->next=pNode->next;
|
---|
158 | }
|
---|
159 | else
|
---|
160 | {
|
---|
161 | cr_server.pCleanupClient=pNode->next;
|
---|
162 | }
|
---|
163 | if (pNode->next)
|
---|
164 | {
|
---|
165 | pNode->next->prev = pNode->prev;
|
---|
166 | }
|
---|
167 |
|
---|
168 | pNext=pNode->next;
|
---|
169 | crFree(pNode);
|
---|
170 | pNode=pNext;
|
---|
171 | }
|
---|
172 | else
|
---|
173 | {
|
---|
174 | pNode=pNode->next;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | void
|
---|
180 | crServerDeleteClient( CRClient *client )
|
---|
181 | {
|
---|
182 | int i, j;
|
---|
183 | int cleanup=1;
|
---|
184 |
|
---|
185 | crDebug("Deleting client %p (%d msgs left)", client, crNetNumMessages(client->conn));
|
---|
186 |
|
---|
187 | #if 0
|
---|
188 | if (crNetNumMessages(client->conn) > 0) {
|
---|
189 | crDebug("Delay destroying client: message still pending");
|
---|
190 | return;
|
---|
191 | }
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | if (!FindClientInQueue(client)) {
|
---|
195 | /* this should never happen */
|
---|
196 | crError("CRServer: client %p not found in the queue!", client);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* remove from clients[] array */
|
---|
200 | for (i = 0; i < cr_server.numClients; i++) {
|
---|
201 | if (cr_server.clients[i] == client) {
|
---|
202 | /* found it */
|
---|
203 | for (j = i; j < cr_server.numClients - 1; j++)
|
---|
204 | cr_server.clients[j] = cr_server.clients[j + 1];
|
---|
205 | cr_server.numClients--;
|
---|
206 | break;
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* check if there're any other guest threads in same process */
|
---|
211 | for (i=0; i < cr_server.numClients; i++)
|
---|
212 | {
|
---|
213 | if (cr_server.clients[i]->pid==client->pid)
|
---|
214 | {
|
---|
215 | cleanup=0;
|
---|
216 | break;
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (cleanup)
|
---|
221 | {
|
---|
222 | crServerCleanupClient(client);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /* remove from the run queue */
|
---|
226 | if (cr_server.run_queue)
|
---|
227 | {
|
---|
228 | RunQueue *q = cr_server.run_queue;
|
---|
229 | RunQueue *qStart = cr_server.run_queue;
|
---|
230 | do {
|
---|
231 | if (q->client == client)
|
---|
232 | {
|
---|
233 | /* this test seems a bit excessive */
|
---|
234 | if ((q->next == q->prev) && (q->next == q) && (cr_server.run_queue == q))
|
---|
235 | {
|
---|
236 | /* We're removing/deleting the only client */
|
---|
237 | CRASSERT(cr_server.numClients == 0);
|
---|
238 | crFree(q);
|
---|
239 | cr_server.run_queue = NULL;
|
---|
240 | cr_server.curClient = NULL;
|
---|
241 | crDebug("Last client deleted - empty run queue.");
|
---|
242 | }
|
---|
243 | else
|
---|
244 | {
|
---|
245 | /* remove from doubly linked list and free the node */
|
---|
246 | if (cr_server.curClient == q->client)
|
---|
247 | cr_server.curClient = NULL;
|
---|
248 | if (cr_server.run_queue == q)
|
---|
249 | cr_server.run_queue = q->next;
|
---|
250 | q->prev->next = q->next;
|
---|
251 | q->next->prev = q->prev;
|
---|
252 | crFree(q);
|
---|
253 | }
|
---|
254 | break;
|
---|
255 | }
|
---|
256 | q = q->next;
|
---|
257 | } while (q != qStart);
|
---|
258 | }
|
---|
259 |
|
---|
260 | crNetFreeConnection(client->conn);
|
---|
261 | client->conn = NULL;
|
---|
262 |
|
---|
263 | if (cleanup)
|
---|
264 | {
|
---|
265 | crServerCleanupByPID(client->pid);
|
---|
266 | crFree(client);
|
---|
267 | }
|
---|
268 | else
|
---|
269 | {
|
---|
270 | CRClientNode *pNode = (CRClientNode *)crAlloc(sizeof(CRClientNode));
|
---|
271 | if (!pNode)
|
---|
272 | {
|
---|
273 | crWarning("Not enough memory, forcing client cleanup");
|
---|
274 | crServerCleanupClient(client);
|
---|
275 | crServerCleanupByPID(client->pid);
|
---|
276 | crFree(client);
|
---|
277 | return;
|
---|
278 | }
|
---|
279 | pNode->pClient = client;
|
---|
280 | pNode->prev = NULL;
|
---|
281 | pNode->next = cr_server.pCleanupClient;
|
---|
282 | cr_server.pCleanupClient = pNode;
|
---|
283 | }
|
---|
284 |
|
---|
285 | if (!cr_server.numClients)
|
---|
286 | {
|
---|
287 | /* if no clients, the guest driver may be unloaded,
|
---|
288 | * and thus the visible regions situation might not be under control anymore,
|
---|
289 | * so cleanup the 3D framebuffer data here
|
---|
290 | * @todo: what really should happen is that guest driver on unload
|
---|
291 | * posts some request to host that would copy the current framebuffer 3D data to the 2D buffer
|
---|
292 | * (i.e. to the memory used by the standard IFramebuffer API) */
|
---|
293 | HCR_FRAMEBUFFER hFb;
|
---|
294 | for (hFb = CrPMgrFbGetFirstEnabled(); hFb; hFb = CrPMgrFbGetNextEnabled(hFb))
|
---|
295 | {
|
---|
296 | int rc = CrFbUpdateBegin(hFb);
|
---|
297 | if (RT_SUCCESS(rc))
|
---|
298 | {
|
---|
299 | CrFbRegionsClear(hFb);
|
---|
300 | CrFbUpdateEnd(hFb);
|
---|
301 | }
|
---|
302 | else
|
---|
303 | WARN(("CrFbUpdateBegin failed %d", rc));
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Test if the given client is in the middle of a glBegin/End or
|
---|
310 | * glNewList/EndList pair.
|
---|
311 | * This is used to test if we can advance to the next client.
|
---|
312 | * \return GL_TRUE if so, GL_FALSE otherwise.
|
---|
313 | */
|
---|
314 | GLboolean
|
---|
315 | crServerClientInBeginEnd(const CRClient *client)
|
---|
316 | {
|
---|
317 | if (client->currentCtxInfo
|
---|
318 | && client->currentCtxInfo->pContext
|
---|
319 | && (client->currentCtxInfo->pContext->lists.currentIndex != 0 ||
|
---|
320 | client->currentCtxInfo->pContext->current.inBeginEnd ||
|
---|
321 | client->currentCtxInfo->pContext->occlusion.currentQueryObject)) {
|
---|
322 | return GL_TRUE;
|
---|
323 | }
|
---|
324 | else {
|
---|
325 | return GL_FALSE;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Find the next client in the run queue that's not blocked and has a
|
---|
332 | * waiting message.
|
---|
333 | * Check if all clients are blocked (on barriers, semaphores), if so we've
|
---|
334 | * deadlocked!
|
---|
335 | * If no clients have a waiting message, call crNetRecv to get something
|
---|
336 | * if 'block' is true, else return NULL if 'block' if false.
|
---|
337 | */
|
---|
338 | static RunQueue *
|
---|
339 | getNextClient(GLboolean block)
|
---|
340 | {
|
---|
341 | while (1)
|
---|
342 | {
|
---|
343 | if (cr_server.run_queue)
|
---|
344 | {
|
---|
345 | GLboolean all_blocked = GL_TRUE;
|
---|
346 | GLboolean done_something = GL_FALSE;
|
---|
347 | RunQueue *start = cr_server.run_queue;
|
---|
348 |
|
---|
349 | /* check if this client's connection has gone away */
|
---|
350 | if (!cr_server.run_queue->client->conn
|
---|
351 | || (cr_server.run_queue->client->conn->type == CR_NO_CONNECTION
|
---|
352 | && crNetNumMessages(cr_server.run_queue->client->conn) == 0))
|
---|
353 | {
|
---|
354 | crServerDeleteClient( cr_server.run_queue->client );
|
---|
355 | start = cr_server.run_queue;
|
---|
356 | }
|
---|
357 |
|
---|
358 | if (cr_server.run_queue == NULL) {
|
---|
359 | /* empty queue */
|
---|
360 | return NULL;
|
---|
361 | }
|
---|
362 |
|
---|
363 | if (crServerClientInBeginEnd(cr_server.run_queue->client)) {
|
---|
364 | /* We _must_ service this client and no other.
|
---|
365 | * If we've got a message waiting on this client's connection we'll
|
---|
366 | * service it. Else, return NULL.
|
---|
367 | */
|
---|
368 | if (crNetNumMessages(cr_server.run_queue->client->conn) > 0)
|
---|
369 | return cr_server.run_queue;
|
---|
370 | else
|
---|
371 | return NULL;
|
---|
372 | }
|
---|
373 |
|
---|
374 | /* loop over entries in run queue, looking for next one that's ready */
|
---|
375 | while (!done_something || cr_server.run_queue != start)
|
---|
376 | {
|
---|
377 | done_something = GL_TRUE;
|
---|
378 | if (!cr_server.run_queue->blocked)
|
---|
379 | {
|
---|
380 | all_blocked = GL_FALSE;
|
---|
381 | }
|
---|
382 | if (!cr_server.run_queue->blocked
|
---|
383 | && cr_server.run_queue->client->conn
|
---|
384 | && crNetNumMessages(cr_server.run_queue->client->conn) > 0)
|
---|
385 | {
|
---|
386 | /* OK, this client isn't blocked and has a queued message */
|
---|
387 | return cr_server.run_queue;
|
---|
388 | }
|
---|
389 | cr_server.run_queue = cr_server.run_queue->next;
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (all_blocked)
|
---|
393 | {
|
---|
394 | /* XXX crError is fatal? Should this be an info/warning msg? */
|
---|
395 | crError( "crserver: DEADLOCK! (numClients=%d, all blocked)",
|
---|
396 | cr_server.numClients );
|
---|
397 | if (cr_server.numClients < (int) cr_server.maxBarrierCount) {
|
---|
398 | crError("Waiting for more clients!!!");
|
---|
399 | while (cr_server.numClients < (int) cr_server.maxBarrierCount) {
|
---|
400 | crNetRecv();
|
---|
401 | }
|
---|
402 | }
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | if (!block)
|
---|
407 | return NULL;
|
---|
408 |
|
---|
409 | /* no one had any work, get some! */
|
---|
410 | crNetRecv();
|
---|
411 |
|
---|
412 | } /* while */
|
---|
413 |
|
---|
414 | /* UNREACHED */
|
---|
415 | /* return NULL; */
|
---|
416 | }
|
---|
417 |
|
---|
418 | typedef struct CR_SERVER_PENDING_MSG
|
---|
419 | {
|
---|
420 | RTLISTNODE Node;
|
---|
421 | uint32_t cbMsg;
|
---|
422 | CRMessage Msg;
|
---|
423 | } CR_SERVER_PENDING_MSG;
|
---|
424 |
|
---|
425 | static int crServerPendMsg(CRConnection *conn, const CRMessage *msg, int cbMsg)
|
---|
426 | {
|
---|
427 | CR_SERVER_PENDING_MSG *pMsg;
|
---|
428 |
|
---|
429 | if (!cbMsg)
|
---|
430 | {
|
---|
431 | WARN(("cbMsg is null!"));
|
---|
432 | return VERR_INVALID_PARAMETER;
|
---|
433 | }
|
---|
434 |
|
---|
435 | pMsg = (CR_SERVER_PENDING_MSG*)RTMemAlloc(cbMsg + RT_OFFSETOF(CR_SERVER_PENDING_MSG, Msg));
|
---|
436 | if (!pMsg)
|
---|
437 | {
|
---|
438 | WARN(("RTMemAlloc failed"));
|
---|
439 | return VERR_NO_MEMORY;
|
---|
440 | }
|
---|
441 |
|
---|
442 | pMsg->cbMsg = cbMsg;
|
---|
443 |
|
---|
444 | memcpy(&pMsg->Msg, msg, cbMsg);
|
---|
445 |
|
---|
446 | RTListAppend(&conn->PendingMsgList, &pMsg->Node);
|
---|
447 |
|
---|
448 | return VINF_SUCCESS;
|
---|
449 | }
|
---|
450 |
|
---|
451 | int crServerPendSaveState(PSSMHANDLE pSSM)
|
---|
452 | {
|
---|
453 | int i, rc;
|
---|
454 |
|
---|
455 | for (i = 0; i < cr_server.numClients; i++)
|
---|
456 | {
|
---|
457 | CR_SERVER_PENDING_MSG *pIter;
|
---|
458 | CRClient *pClient = cr_server.clients[i];
|
---|
459 | CRConnection *pConn;
|
---|
460 | if (!pClient || !pClient->conn)
|
---|
461 | {
|
---|
462 | WARN(("invalid client state"));
|
---|
463 | continue;
|
---|
464 | }
|
---|
465 |
|
---|
466 | pConn = pClient->conn;
|
---|
467 |
|
---|
468 | if (RTListIsEmpty(&pConn->PendingMsgList))
|
---|
469 | continue;
|
---|
470 |
|
---|
471 | CRASSERT(pConn->u32ClientID);
|
---|
472 |
|
---|
473 | rc = SSMR3PutU32(pSSM, pConn->u32ClientID);
|
---|
474 | AssertRCReturn(rc, rc);
|
---|
475 |
|
---|
476 | RTListForEach(&pConn->PendingMsgList, pIter, CR_SERVER_PENDING_MSG, Node)
|
---|
477 | {
|
---|
478 | CRASSERT(pIter->cbMsg);
|
---|
479 |
|
---|
480 | rc = SSMR3PutU32(pSSM, pIter->cbMsg);
|
---|
481 | AssertRCReturn(rc, rc);
|
---|
482 |
|
---|
483 | rc = SSMR3PutMem(pSSM, &pIter->Msg, pIter->cbMsg);
|
---|
484 | AssertRCReturn(rc, rc);
|
---|
485 | }
|
---|
486 |
|
---|
487 | rc = SSMR3PutU32(pSSM, 0);
|
---|
488 | AssertRCReturn(rc, rc);
|
---|
489 | }
|
---|
490 |
|
---|
491 | rc = SSMR3PutU32(pSSM, 0);
|
---|
492 | AssertRCReturn(rc, rc);
|
---|
493 |
|
---|
494 | return VINF_SUCCESS;
|
---|
495 | }
|
---|
496 |
|
---|
497 | int crServerPendLoadState(PSSMHANDLE pSSM, uint32_t u32Version)
|
---|
498 | {
|
---|
499 | int rc;
|
---|
500 | uint32_t u32;
|
---|
501 | CRClient *pClient;
|
---|
502 |
|
---|
503 | if (u32Version < SHCROGL_SSM_VERSION_WITH_PEND_CMD_INFO)
|
---|
504 | return VINF_SUCCESS;
|
---|
505 |
|
---|
506 | rc = SSMR3GetU32(pSSM, &u32);
|
---|
507 | AssertRCReturn(rc, rc);
|
---|
508 |
|
---|
509 | if (!u32)
|
---|
510 | return VINF_SUCCESS;
|
---|
511 |
|
---|
512 | do {
|
---|
513 | rc = crVBoxServerClientGet(u32, &pClient);
|
---|
514 | AssertRCReturn(rc, rc);
|
---|
515 |
|
---|
516 | for(;;)
|
---|
517 | {
|
---|
518 | CR_SERVER_PENDING_MSG *pMsg;
|
---|
519 |
|
---|
520 | rc = SSMR3GetU32(pSSM, &u32);
|
---|
521 | AssertRCReturn(rc, rc);
|
---|
522 |
|
---|
523 | if (!u32)
|
---|
524 | break;
|
---|
525 |
|
---|
526 | pMsg = (CR_SERVER_PENDING_MSG*)RTMemAlloc(u32 + RT_OFFSETOF(CR_SERVER_PENDING_MSG, Msg));
|
---|
527 | if (!pMsg)
|
---|
528 | {
|
---|
529 | WARN(("RTMemAlloc failed"));
|
---|
530 | return VERR_NO_MEMORY;
|
---|
531 | }
|
---|
532 |
|
---|
533 | pMsg->cbMsg = u32;
|
---|
534 | rc = SSMR3GetMem(pSSM, &pMsg->Msg, u32);
|
---|
535 | AssertRCReturn(rc, rc);
|
---|
536 |
|
---|
537 | RTListAppend(&pClient->conn->PendingMsgList, &pMsg->Node);
|
---|
538 | }
|
---|
539 |
|
---|
540 | rc = SSMR3GetU32(pSSM, &u32);
|
---|
541 | AssertRCReturn(rc, rc);
|
---|
542 | } while (u32);
|
---|
543 |
|
---|
544 | rc = SSMR3GetU32(pSSM, &u32);
|
---|
545 | AssertRCReturn(rc, rc);
|
---|
546 |
|
---|
547 | return VINF_SUCCESS;
|
---|
548 | }
|
---|
549 |
|
---|
550 | static void crServerPendProcess(CRConnection *conn)
|
---|
551 | {
|
---|
552 | CR_SERVER_PENDING_MSG *pIter, *pNext;
|
---|
553 | RTListForEachSafe(&conn->PendingMsgList, pIter, pNext, CR_SERVER_PENDING_MSG, Node)
|
---|
554 | {
|
---|
555 | CRMessage *msg = &pIter->Msg;
|
---|
556 | const CRMessageOpcodes *msg_opcodes;
|
---|
557 | int opcodeBytes;
|
---|
558 | const char *data_ptr;
|
---|
559 |
|
---|
560 | RTListNodeRemove(&pIter->Node);
|
---|
561 |
|
---|
562 | CRASSERT(msg->header.type == CR_MESSAGE_OPCODES);
|
---|
563 |
|
---|
564 | msg_opcodes = (const CRMessageOpcodes *) msg;
|
---|
565 | opcodeBytes = (msg_opcodes->numOpcodes + 3) & ~0x03;
|
---|
566 |
|
---|
567 | data_ptr = (const char *) msg_opcodes + sizeof (CRMessageOpcodes) + opcodeBytes;
|
---|
568 |
|
---|
569 | crUnpack(data_ptr, /* first command's operands */
|
---|
570 | data_ptr - 1, /* first command's opcode */
|
---|
571 | msg_opcodes->numOpcodes, /* how many opcodes */
|
---|
572 | &(cr_server.dispatch)); /* the CR dispatch table */
|
---|
573 |
|
---|
574 | RTMemFree(pIter);
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | /**
|
---|
579 | * This function takes the given message (which should be a buffer of
|
---|
580 | * rendering commands) and executes it.
|
---|
581 | */
|
---|
582 | static void
|
---|
583 | crServerDispatchMessage(CRConnection *conn, CRMessage *msg, int cbMsg)
|
---|
584 | {
|
---|
585 | const CRMessageOpcodes *msg_opcodes;
|
---|
586 | int opcodeBytes;
|
---|
587 | const char *data_ptr;
|
---|
588 | #ifdef VBOX_WITH_CRHGSMI
|
---|
589 | PCRVBOXHGSMI_CMDDATA pCmdData = NULL;
|
---|
590 | #endif
|
---|
591 | CR_UNPACK_BUFFER_TYPE enmType;
|
---|
592 | bool fUnpack = true;
|
---|
593 |
|
---|
594 | if (msg->header.type == CR_MESSAGE_REDIR_PTR)
|
---|
595 | {
|
---|
596 | #ifdef VBOX_WITH_CRHGSMI
|
---|
597 | pCmdData = &msg->redirptr.CmdData;
|
---|
598 | #endif
|
---|
599 | msg = (CRMessage *) msg->redirptr.pMessage;
|
---|
600 | }
|
---|
601 |
|
---|
602 | CRASSERT(msg->header.type == CR_MESSAGE_OPCODES);
|
---|
603 |
|
---|
604 | msg_opcodes = (const CRMessageOpcodes *) msg;
|
---|
605 | opcodeBytes = (msg_opcodes->numOpcodes + 3) & ~0x03;
|
---|
606 |
|
---|
607 | #ifdef VBOXCR_LOGFPS
|
---|
608 | CRASSERT(cr_server.curClient && cr_server.curClient->conn && cr_server.curClient->conn->id == msg->header.conn_id);
|
---|
609 | cr_server.curClient->conn->opcodes_count += msg_opcodes->numOpcodes;
|
---|
610 | #endif
|
---|
611 |
|
---|
612 | data_ptr = (const char *) msg_opcodes + sizeof(CRMessageOpcodes) + opcodeBytes;
|
---|
613 |
|
---|
614 | enmType = crUnpackGetBufferType(data_ptr - 1, /* first command's opcode */
|
---|
615 | msg_opcodes->numOpcodes /* how many opcodes */);
|
---|
616 | switch (enmType)
|
---|
617 | {
|
---|
618 | case CR_UNPACK_BUFFER_TYPE_GENERIC:
|
---|
619 | {
|
---|
620 | if (RTListIsEmpty(&conn->PendingMsgList))
|
---|
621 | break;
|
---|
622 |
|
---|
623 | if (RT_SUCCESS(crServerPendMsg(conn, msg, cbMsg)))
|
---|
624 | {
|
---|
625 | fUnpack = false;
|
---|
626 | break;
|
---|
627 | }
|
---|
628 |
|
---|
629 | WARN(("crServerPendMsg failed"));
|
---|
630 | crServerPendProcess(conn);
|
---|
631 | break;
|
---|
632 | }
|
---|
633 | case CR_UNPACK_BUFFER_TYPE_CMDBLOCK_BEGIN:
|
---|
634 | {
|
---|
635 | if (RTListIsEmpty(&conn->PendingMsgList))
|
---|
636 | {
|
---|
637 | if (RT_SUCCESS(crServerPendMsg(conn, msg, cbMsg)))
|
---|
638 | {
|
---|
639 | Assert(!RTListIsEmpty(&conn->PendingMsgList));
|
---|
640 | fUnpack = false;
|
---|
641 | break;
|
---|
642 | }
|
---|
643 | else
|
---|
644 | WARN(("crServerPendMsg failed"));
|
---|
645 | }
|
---|
646 | else
|
---|
647 | WARN(("Pend List is NOT empty, drain the current list, and ignore this command"));
|
---|
648 |
|
---|
649 | crServerPendProcess(conn);
|
---|
650 | break;
|
---|
651 | }
|
---|
652 | case CR_UNPACK_BUFFER_TYPE_CMDBLOCK_END:
|
---|
653 | {
|
---|
654 | CRASSERT(!RTListIsEmpty(&conn->PendingMsgList));
|
---|
655 | crServerPendProcess(conn);
|
---|
656 | Assert(RTListIsEmpty(&conn->PendingMsgList));
|
---|
657 | break;
|
---|
658 | }
|
---|
659 | default:
|
---|
660 | WARN(("unsupported buffer type"));
|
---|
661 | break;
|
---|
662 | }
|
---|
663 |
|
---|
664 | if (fUnpack)
|
---|
665 | {
|
---|
666 | crUnpack(data_ptr, /* first command's operands */
|
---|
667 | data_ptr - 1, /* first command's opcode */
|
---|
668 | msg_opcodes->numOpcodes, /* how many opcodes */
|
---|
669 | &(cr_server.dispatch)); /* the CR dispatch table */
|
---|
670 | }
|
---|
671 |
|
---|
672 | #ifdef VBOX_WITH_CRHGSMI
|
---|
673 | if (pCmdData)
|
---|
674 | {
|
---|
675 | int rc = VINF_SUCCESS;
|
---|
676 | CRVBOXHGSMI_CMDDATA_ASSERT_CONSISTENT(pCmdData);
|
---|
677 | if (CRVBOXHGSMI_CMDDATA_IS_SETWB(pCmdData))
|
---|
678 | {
|
---|
679 | uint32_t cbWriteback = pCmdData->cbWriteback;
|
---|
680 | rc = crVBoxServerInternalClientRead(conn->pClient, (uint8_t*)pCmdData->pWriteback, &cbWriteback);
|
---|
681 | Assert(rc == VINF_SUCCESS || rc == VERR_BUFFER_OVERFLOW);
|
---|
682 | *pCmdData->pcbWriteback = cbWriteback;
|
---|
683 | }
|
---|
684 | VBOXCRHGSMI_CMD_CHECK_COMPLETE(pCmdData, rc);
|
---|
685 | }
|
---|
686 | #endif
|
---|
687 | }
|
---|
688 |
|
---|
689 |
|
---|
690 | typedef enum
|
---|
691 | {
|
---|
692 | CLIENT_GONE = 1, /* the client has disconnected */
|
---|
693 | CLIENT_NEXT = 2, /* we can advance to next client */
|
---|
694 | CLIENT_MORE = 3 /* we need to keep servicing current client */
|
---|
695 | } ClientStatus;
|
---|
696 |
|
---|
697 |
|
---|
698 | /**
|
---|
699 | * Process incoming/pending message for the given client (queue entry).
|
---|
700 | * \return CLIENT_GONE if this client has gone away/exited,
|
---|
701 | * CLIENT_NEXT if we can advance to the next client
|
---|
702 | * CLIENT_MORE if we have to process more messages for this client.
|
---|
703 | */
|
---|
704 | static ClientStatus
|
---|
705 | crServerServiceClient(const RunQueue *qEntry)
|
---|
706 | {
|
---|
707 | CRMessage *msg;
|
---|
708 | CRConnection *conn;
|
---|
709 |
|
---|
710 | /* set current client pointer */
|
---|
711 | cr_server.curClient = qEntry->client;
|
---|
712 |
|
---|
713 | conn = cr_server.run_queue->client->conn;
|
---|
714 |
|
---|
715 | /* service current client as long as we can */
|
---|
716 | while (conn && conn->type != CR_NO_CONNECTION &&
|
---|
717 | crNetNumMessages(conn) > 0) {
|
---|
718 | unsigned int len;
|
---|
719 |
|
---|
720 | /*
|
---|
721 | crDebug("%d messages on %p",
|
---|
722 | crNetNumMessages(conn), (void *) conn);
|
---|
723 | */
|
---|
724 |
|
---|
725 | /* Don't use GetMessage, because we want to do our own crNetRecv() calls
|
---|
726 | * here ourself.
|
---|
727 | * Note that crNetPeekMessage() DOES remove the message from the queue
|
---|
728 | * if there is one.
|
---|
729 | */
|
---|
730 | len = crNetPeekMessage( conn, &msg );
|
---|
731 | CRASSERT(len > 0);
|
---|
732 | if (msg->header.type != CR_MESSAGE_OPCODES
|
---|
733 | && msg->header.type != CR_MESSAGE_REDIR_PTR) {
|
---|
734 | crError( "SPU %d sent me CRAP (type=0x%x)",
|
---|
735 | cr_server.curClient->spu_id, msg->header.type );
|
---|
736 | }
|
---|
737 |
|
---|
738 | /* Do the context switch here. No sense in switching before we
|
---|
739 | * really have any work to process. This is a no-op if we're
|
---|
740 | * not really switching contexts.
|
---|
741 | *
|
---|
742 | * XXX This isn't entirely sound. The crStateMakeCurrent() call
|
---|
743 | * will compute the state difference and dispatch it using
|
---|
744 | * the head SPU's dispatch table.
|
---|
745 | *
|
---|
746 | * This is a problem if this is the first buffer coming in,
|
---|
747 | * and the head SPU hasn't had a chance to do a MakeCurrent()
|
---|
748 | * yet (likely because the MakeCurrent() command is in the
|
---|
749 | * buffer itself).
|
---|
750 | *
|
---|
751 | * At best, in this case, the functions are no-ops, and
|
---|
752 | * are essentially ignored by the SPU. In the typical
|
---|
753 | * case, things aren't too bad; if the SPU just calls
|
---|
754 | * crState*() functions to update local state, everything
|
---|
755 | * will work just fine.
|
---|
756 | *
|
---|
757 | * In the worst (but unusual) case where a nontrivial
|
---|
758 | * SPU is at the head of a crserver's SPU chain (say,
|
---|
759 | * in a multiple-tiered "tilesort" arrangement, as
|
---|
760 | * seen in the "multitilesort.conf" configuration), the
|
---|
761 | * SPU may rely on state set during the MakeCurrent() that
|
---|
762 | * may not be present yet, because no MakeCurrent() has
|
---|
763 | * yet been dispatched.
|
---|
764 | *
|
---|
765 | * This headache will have to be revisited in the future;
|
---|
766 | * for now, SPUs that could head a crserver's SPU chain
|
---|
767 | * will have to detect the case that their functions are
|
---|
768 | * being called outside of a MakeCurrent(), and will have
|
---|
769 | * to handle the situation gracefully. (This is currently
|
---|
770 | * the case with the "tilesort" SPU.)
|
---|
771 | */
|
---|
772 |
|
---|
773 | #if 0
|
---|
774 | crStateMakeCurrent( cr_server.curClient->currentCtx );
|
---|
775 | #else
|
---|
776 | /* Check if the current window is the one that the client wants to
|
---|
777 | * draw into. If not, dispatch a MakeCurrent to activate the proper
|
---|
778 | * window.
|
---|
779 | */
|
---|
780 | if (cr_server.curClient) {
|
---|
781 | int clientWindow = cr_server.curClient->currentWindow;
|
---|
782 | int clientContext = cr_server.curClient->currentContextNumber;
|
---|
783 | CRContextInfo *clientCtxInfo = cr_server.curClient->currentCtxInfo;
|
---|
784 | if (clientCtxInfo != cr_server.currentCtxInfo
|
---|
785 | || clientWindow != cr_server.currentWindow
|
---|
786 | || cr_server.bForceMakeCurrentOnClientSwitch) {
|
---|
787 | crServerDispatchMakeCurrent(clientWindow, 0, clientContext);
|
---|
788 | /*
|
---|
789 | CRASSERT(cr_server.currentWindow == clientWindow);
|
---|
790 | */
|
---|
791 | }
|
---|
792 | }
|
---|
793 | #endif
|
---|
794 |
|
---|
795 | /* Force scissor, viewport and projection matrix update in
|
---|
796 | * crServerSetOutputBounds().
|
---|
797 | */
|
---|
798 | cr_server.currentSerialNo = 0;
|
---|
799 |
|
---|
800 | /* Commands get dispatched here */
|
---|
801 | crServerDispatchMessage( conn, msg, len );
|
---|
802 |
|
---|
803 | crNetFree( conn, msg );
|
---|
804 |
|
---|
805 | if (qEntry->blocked) {
|
---|
806 | /* Note/assert: we should not be inside a glBegin/End or glNewList/
|
---|
807 | * glEndList pair at this time!
|
---|
808 | */
|
---|
809 | CRASSERT(0);
|
---|
810 | return CLIENT_NEXT;
|
---|
811 | }
|
---|
812 |
|
---|
813 | } /* while */
|
---|
814 |
|
---|
815 | /*
|
---|
816 | * Check if client/connection is gone
|
---|
817 | */
|
---|
818 | if (!conn || conn->type == CR_NO_CONNECTION) {
|
---|
819 | crDebug("Delete client %p at %d", cr_server.run_queue->client, __LINE__);
|
---|
820 | crServerDeleteClient( cr_server.run_queue->client );
|
---|
821 | return CLIENT_GONE;
|
---|
822 | }
|
---|
823 |
|
---|
824 | /*
|
---|
825 | * Determine if we can advance to next client.
|
---|
826 | * If we're currently inside a glBegin/End primitive or building a display
|
---|
827 | * list we can't service another client until we're done with the
|
---|
828 | * primitive/list.
|
---|
829 | */
|
---|
830 | if (crServerClientInBeginEnd(cr_server.curClient)) {
|
---|
831 | /* The next message has to come from the current client's connection. */
|
---|
832 | CRASSERT(!qEntry->blocked);
|
---|
833 | return CLIENT_MORE;
|
---|
834 | }
|
---|
835 | else {
|
---|
836 | /* get next client */
|
---|
837 | return CLIENT_NEXT;
|
---|
838 | }
|
---|
839 | }
|
---|
840 |
|
---|
841 |
|
---|
842 |
|
---|
843 | /**
|
---|
844 | * Check if any of the clients need servicing.
|
---|
845 | * If so, service one client and return.
|
---|
846 | * Else, just return.
|
---|
847 | */
|
---|
848 | void
|
---|
849 | crServerServiceClients(void)
|
---|
850 | {
|
---|
851 | RunQueue *q;
|
---|
852 |
|
---|
853 | q = getNextClient(GL_FALSE); /* don't block */
|
---|
854 | while (q)
|
---|
855 | {
|
---|
856 | ClientStatus stat = crServerServiceClient(q);
|
---|
857 | if (stat == CLIENT_NEXT && cr_server.run_queue->next) {
|
---|
858 | /* advance to next client */
|
---|
859 | cr_server.run_queue = cr_server.run_queue->next;
|
---|
860 | }
|
---|
861 | q = getNextClient(GL_FALSE);
|
---|
862 | }
|
---|
863 | }
|
---|
864 |
|
---|
865 |
|
---|
866 |
|
---|
867 |
|
---|
868 | /**
|
---|
869 | * Main crserver loop. Service connections from all connected clients.
|
---|
870 | * XXX add a config option to specify whether the crserver
|
---|
871 | * should exit when there's no more clients.
|
---|
872 | */
|
---|
873 | void
|
---|
874 | crServerSerializeRemoteStreams(void)
|
---|
875 | {
|
---|
876 | /*MSG msg;*/
|
---|
877 |
|
---|
878 | while (cr_server.run_queue)
|
---|
879 | {
|
---|
880 | crServerServiceClients();
|
---|
881 | /*if (PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
|
---|
882 | {
|
---|
883 | if (msg.message == WM_QUIT)
|
---|
884 | {
|
---|
885 | PostQuitMessage((int)msg.wParam);
|
---|
886 | break;
|
---|
887 | }
|
---|
888 | TranslateMessage( &msg );
|
---|
889 | DispatchMessage( &msg );
|
---|
890 | }*/
|
---|
891 | }
|
---|
892 | }
|
---|
893 |
|
---|
894 |
|
---|
895 | /**
|
---|
896 | * This will be called by the network layer when it's received a new message.
|
---|
897 | */
|
---|
898 | int
|
---|
899 | crServerRecv( CRConnection *conn, CRMessage *msg, unsigned int len )
|
---|
900 | {
|
---|
901 | CRMessage *pRealMsg;
|
---|
902 | (void) len;
|
---|
903 |
|
---|
904 | pRealMsg = (msg->header.type!=CR_MESSAGE_REDIR_PTR) ? msg : (CRMessage*) msg->redirptr.pMessage;
|
---|
905 |
|
---|
906 | switch( pRealMsg->header.type )
|
---|
907 | {
|
---|
908 | /* Called when using multiple threads */
|
---|
909 | case CR_MESSAGE_NEWCLIENT:
|
---|
910 | crServerAddNewClient();
|
---|
911 | return 1; /* msg handled */
|
---|
912 | default:
|
---|
913 | /*crWarning( "Why is the crserver getting a message of type 0x%x?",
|
---|
914 | msg->header.type ); */
|
---|
915 | ;
|
---|
916 | }
|
---|
917 | return 0; /* not handled */
|
---|
918 | }
|
---|