VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioTestService.cpp@ 89225

Last change on this file since 89225 was 89225, checked in by vboxsync, 4 years ago

Audio/ValKit: Implemented audio test execution service (ATS) shutdown handling + testing. bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.6 KB
Line 
1/* $Id: AudioTestService.cpp 89225 2021-05-21 13:04:46Z vboxsync $ */
2/** @file
3 * AudioTestService - Audio test execution server.
4 */
5
6/*
7 * Copyright (C) 2021 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP RTLOGGROUP_DEFAULT
23#include <iprt/alloca.h>
24#include <iprt/asm.h>
25#include <iprt/assert.h>
26#include <iprt/critsect.h>
27#include <iprt/crc.h>
28#include <iprt/ctype.h>
29#include <iprt/dir.h>
30#include <iprt/env.h>
31#include <iprt/err.h>
32#include <iprt/getopt.h>
33#include <iprt/handle.h>
34#include <iprt/initterm.h>
35#include <iprt/json.h>
36#include <iprt/list.h>
37#include <iprt/log.h>
38#include <iprt/mem.h>
39#include <iprt/message.h>
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/pipe.h>
43#include <iprt/poll.h>
44#include <iprt/process.h>
45#include <iprt/stream.h>
46#include <iprt/string.h>
47#include <iprt/thread.h>
48
49#include "AudioTestService.h"
50#include "AudioTestServiceInternal.h"
51
52
53/*********************************************************************************************************************************
54* Structures and Typedefs *
55*********************************************************************************************************************************/
56
57
58/*********************************************************************************************************************************
59* Global Variables *
60*********************************************************************************************************************************/
61/**
62 * Transport layers.
63 */
64static const PCATSTRANSPORT g_apTransports[] =
65{
66 &g_TcpTransport
67};
68
69/**
70 * ATS client state.
71 */
72typedef enum ATSCLIENTSTATE
73{
74 /** Invalid client state. */
75 ATSCLIENTSTATE_INVALID = 0,
76 /** Client is initialising, only the HOWDY and BYE packets are allowed. */
77 ATSCLIENTSTATE_INITIALISING,
78 /** Client is in fully cuntional state and ready to process all requests. */
79 ATSCLIENTSTATE_READY,
80 /** Client is destroying. */
81 ATSCLIENTSTATE_DESTROYING,
82 /** 32bit hack. */
83 ATSCLIENTSTATE_32BIT_HACK = 0x7fffffff
84} ATSCLIENTSTATE;
85
86/**
87 * ATS client instance.
88 */
89typedef struct ATSCLIENTINST
90{
91 /** List node for new clients. */
92 RTLISTNODE NdLst;
93 /** The current client state. */
94 ATSCLIENTSTATE enmState;
95 /** Transport backend specific data. */
96 PATSTRANSPORTCLIENT pTransportClient;
97 /** Client hostname. */
98 char *pszHostname;
99} ATSCLIENTINST;
100/** Pointer to a ATS client instance. */
101typedef ATSCLIENTINST *PATSCLIENTINST;
102
103/**
104 * Returns the string represenation of the given state.
105 */
106static const char *atsClientStateStringify(ATSCLIENTSTATE enmState)
107{
108 switch (enmState)
109 {
110 case ATSCLIENTSTATE_INVALID:
111 return "INVALID";
112 case ATSCLIENTSTATE_INITIALISING:
113 return "INITIALISING";
114 case ATSCLIENTSTATE_READY:
115 return "READY";
116 case ATSCLIENTSTATE_DESTROYING:
117 return "DESTROYING";
118 case ATSCLIENTSTATE_32BIT_HACK:
119 default:
120 break;
121 }
122
123 AssertMsgFailed(("Unknown state %#x\n", enmState));
124 return "UNKNOWN";
125}
126
127/**
128 * Calculates the checksum value, zero any padding space and send the packet.
129 *
130 * @returns IPRT status code.
131 * @param pThis The ATS instance.
132 * @param pClient The ATS client structure.
133 * @param pPkt The packet to send. Must point to a correctly
134 * aligned buffer.
135 */
136static int atsSendPkt(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPkt)
137{
138 Assert(pPkt->cb >= sizeof(*pPkt));
139 pPkt->uCrc32 = RTCrc32(pPkt->achOpcode, pPkt->cb - RT_UOFFSETOF(ATSPKTHDR, achOpcode));
140 if (pPkt->cb != RT_ALIGN_32(pPkt->cb, ATSPKT_ALIGNMENT))
141 memset((uint8_t *)pPkt + pPkt->cb, '\0', RT_ALIGN_32(pPkt->cb, ATSPKT_ALIGNMENT) - pPkt->cb);
142
143 Log(("atsSendPkt: cb=%#x opcode=%.8s\n", pPkt->cb, pPkt->achOpcode));
144 Log2(("%.*Rhxd\n", RT_MIN(pPkt->cb, 256), pPkt));
145 int rc = pThis->pTransport->pfnSendPkt(pClient->pTransportClient, pPkt);
146 while (RT_UNLIKELY(rc == VERR_INTERRUPTED) && !pThis->fTerminate)
147 rc = pThis->pTransport->pfnSendPkt(pClient->pTransportClient, pPkt);
148 if (RT_FAILURE(rc))
149 Log(("atsSendPkt: rc=%Rrc\n", rc));
150
151 return rc;
152}
153
154/**
155 * Sends a babble reply and disconnects the client (if applicable).
156 *
157 * @param pThis The ATS instance.
158 * @param pClient The ATS client structure.
159 * @param pszOpcode The BABBLE opcode.
160 */
161static void atsReplyBabble(PATSSERVER pThis, PATSCLIENTINST pClient, const char *pszOpcode)
162{
163 ATSPKTHDR Reply;
164 Reply.cb = sizeof(Reply);
165 Reply.uCrc32 = 0;
166 memcpy(Reply.achOpcode, pszOpcode, sizeof(Reply.achOpcode));
167
168 pThis->pTransport->pfnBabble(pClient->pTransportClient, &Reply, 20*1000);
169}
170
171/**
172 * Receive and validate a packet.
173 *
174 * Will send bable responses to malformed packets that results in a error status
175 * code.
176 *
177 * @returns IPRT status code.
178 * @param pThis The ATS instance.
179 * @param pClient The ATS client structure.
180 * @param ppPktHdr Where to return the packet on success. Free
181 * with RTMemFree.
182 * @param fAutoRetryOnFailure Whether to retry on error.
183 */
184static int atsRecvPkt(PATSSERVER pThis, PATSCLIENTINST pClient, PPATSPKTHDR ppPktHdr, bool fAutoRetryOnFailure)
185{
186 for (;;)
187 {
188 PATSPKTHDR pPktHdr;
189 int rc = pThis->pTransport->pfnRecvPkt(pClient->pTransportClient, &pPktHdr);
190 if (RT_SUCCESS(rc))
191 {
192 /* validate the packet. */
193 if ( pPktHdr->cb >= sizeof(ATSPKTHDR)
194 && pPktHdr->cb < ATSPKT_MAX_SIZE)
195 {
196 Log2(("atsRecvPkt: pPktHdr=%p cb=%#x crc32=%#x opcode=%.8s\n"
197 "%.*Rhxd\n",
198 pPktHdr, pPktHdr->cb, pPktHdr->uCrc32, pPktHdr->achOpcode, RT_MIN(pPktHdr->cb, 256), pPktHdr));
199 uint32_t uCrc32Calc = pPktHdr->uCrc32 != 0
200 ? RTCrc32(&pPktHdr->achOpcode[0], pPktHdr->cb - RT_UOFFSETOF(ATSPKTHDR, achOpcode))
201 : 0;
202 if (pPktHdr->uCrc32 == uCrc32Calc)
203 {
204 AssertCompileMemberSize(ATSPKTHDR, achOpcode, 8);
205 if ( RT_C_IS_UPPER(pPktHdr->achOpcode[0])
206 && RT_C_IS_UPPER(pPktHdr->achOpcode[1])
207 && (RT_C_IS_UPPER(pPktHdr->achOpcode[2]) || pPktHdr->achOpcode[2] == ' ')
208 && (RT_C_IS_PRINT(pPktHdr->achOpcode[3]) || pPktHdr->achOpcode[3] == ' ')
209 && (RT_C_IS_PRINT(pPktHdr->achOpcode[4]) || pPktHdr->achOpcode[4] == ' ')
210 && (RT_C_IS_PRINT(pPktHdr->achOpcode[5]) || pPktHdr->achOpcode[5] == ' ')
211 && (RT_C_IS_PRINT(pPktHdr->achOpcode[6]) || pPktHdr->achOpcode[6] == ' ')
212 && (RT_C_IS_PRINT(pPktHdr->achOpcode[7]) || pPktHdr->achOpcode[7] == ' ')
213 )
214 {
215 Log(("atsRecvPkt: cb=%#x opcode=%.8s\n", pPktHdr->cb, pPktHdr->achOpcode));
216 *ppPktHdr = pPktHdr;
217 return rc;
218 }
219
220 rc = VERR_IO_BAD_COMMAND;
221 }
222 else
223 {
224 Log(("atsRecvPkt: cb=%#x opcode=%.8s crc32=%#x actual=%#x\n",
225 pPktHdr->cb, pPktHdr->achOpcode, pPktHdr->uCrc32, uCrc32Calc));
226 rc = VERR_IO_CRC;
227 }
228 }
229 else
230 rc = VERR_IO_BAD_LENGTH;
231
232 /* Send babble reply and disconnect the client if the transport is
233 connection oriented. */
234 if (rc == VERR_IO_BAD_LENGTH)
235 atsReplyBabble(pThis, pClient, "BABBLE L");
236 else if (rc == VERR_IO_CRC)
237 atsReplyBabble(pThis, pClient, "BABBLE C");
238 else if (rc == VERR_IO_BAD_COMMAND)
239 atsReplyBabble(pThis, pClient, "BABBLE O");
240 else
241 atsReplyBabble(pThis, pClient, "BABBLE ");
242 RTMemFree(pPktHdr);
243 }
244
245 /* Try again or return failure? */
246 if ( pThis->fTerminate
247 || rc != VERR_INTERRUPTED
248 || !fAutoRetryOnFailure
249 )
250 {
251 Log(("atsRecvPkt: rc=%Rrc\n", rc));
252 return rc;
253 }
254 }
255}
256
257/**
258 * Make a simple reply, only status opcode.
259 *
260 * @returns IPRT status code of the send.
261 * @param pThis The ATS instance.
262 * @param pClient The ATS client structure.
263 * @param pReply The reply packet.
264 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
265 * with space.
266 * @param cbExtra Bytes in addition to the header.
267 */
268static int atsReplyInternal(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTSTS pReply, const char *pszOpcode, size_t cbExtra)
269{
270 /* copy the opcode, don't be too strict in case of a padding screw up. */
271 size_t cchOpcode = strlen(pszOpcode);
272 if (RT_LIKELY(cchOpcode == sizeof(pReply->Hdr.achOpcode)))
273 memcpy(pReply->Hdr.achOpcode, pszOpcode, sizeof(pReply->Hdr.achOpcode));
274 else
275 {
276 Assert(cchOpcode == sizeof(pReply->Hdr.achOpcode));
277 while (cchOpcode > 0 && pszOpcode[cchOpcode - 1] == ' ')
278 cchOpcode--;
279 AssertMsgReturn(cchOpcode < sizeof(pReply->Hdr.achOpcode), ("%d/'%.8s'\n", cchOpcode, pszOpcode), VERR_INTERNAL_ERROR_4);
280 memcpy(pReply->Hdr.achOpcode, pszOpcode, cchOpcode);
281 memset(&pReply->Hdr.achOpcode[cchOpcode], ' ', sizeof(pReply->Hdr.achOpcode) - cchOpcode);
282 }
283
284 pReply->Hdr.cb = (uint32_t)sizeof(ATSPKTSTS) + (uint32_t)cbExtra;
285 pReply->Hdr.uCrc32 = 0;
286
287 return atsSendPkt(pThis, pClient, &pReply->Hdr);
288}
289
290/**
291 * Make a simple reply, only status opcode.
292 *
293 * @returns IPRT status code of the send.
294 * @param pThis The ATS instance.
295 * @param pClient The ATS client structure.
296 * @param pPktHdr The original packet (for future use).
297 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
298 * with space.
299 */
300static int atsReplySimple(PATSSERVER pThis, PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr, const char *pszOpcode)
301{
302 ATSPKTSTS Pkt;
303
304 RT_ZERO(Pkt);
305 Pkt.rcReq = VINF_SUCCESS;
306 Pkt.cchStsMsg = 0;
307 RT_NOREF(pPktHdr);
308 return atsReplyInternal(pThis, pClient, &Pkt, pszOpcode, 0);
309}
310
311/**
312 * Acknowledges a packet with success.
313 *
314 * @returns IPRT status code of the send.
315 * @param pThis The ATS instance.
316 * @param pClient The ATS client structure.
317 * @param pPktHdr The original packet (for future use).
318 */
319static int atsReplyAck(PATSSERVER pThis, PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr)
320{
321 return atsReplySimple(pThis, pClient, pPktHdr, "ACK ");
322}
323
324/**
325 * Replies with a failure.
326 *
327 * @returns IPRT status code of the send.
328 * @param pThis The ATS instance.
329 * @param pClient The ATS client structure.
330 * @param pPktHdr The original packet (for future use).
331 * @param rcReq Status code.
332 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
333 * with space.
334 * @param rcReq The status code of the request.
335 * @param pszDetailFmt Longer description of the problem (format string).
336 * @param va Format arguments.
337 */
338static int atsReplyFailureV(PATSSERVER pThis,
339 PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr, const char *pszOpcode, int rcReq, const char *pszDetailFmt, va_list va)
340{
341 RT_NOREF(pPktHdr);
342 union
343 {
344 ATSPKTSTS Hdr;
345 char ach[256];
346 } uPkt;
347
348 RT_ZERO(uPkt);
349 size_t cchDetail = RTStrPrintfV(&uPkt.ach[sizeof(ATSPKTSTS)],
350 sizeof(uPkt) - sizeof(ATSPKTSTS),
351 pszDetailFmt, va);
352 uPkt.Hdr.rcReq = rcReq;
353 uPkt.Hdr.cchStsMsg = (uint32_t)cchDetail;
354 return atsReplyInternal(pThis, pClient, &uPkt.Hdr, pszOpcode, cchDetail + 1);
355}
356
357/**
358 * Replies with a failure.
359 *
360 * @returns IPRT status code of the send.
361 * @param pThis The ATS instance.
362 * @param pClient The ATS client structure.
363 * @param pPktHdr The original packet (for future use).
364 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
365 * with space.
366 * @param rcReq Status code.
367 * @param pszDetailFmt Longer description of the problem (format string).
368 * @param ... Format arguments.
369 */
370static int atsReplyFailure(PATSSERVER pThis,
371 PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr, const char *pszOpcode, int rcReq, const char *pszDetailFmt, ...)
372{
373 va_list va;
374 va_start(va, pszDetailFmt);
375 int rc = atsReplyFailureV(pThis, pClient, pPktHdr, pszOpcode, rcReq, pszDetailFmt, va);
376 va_end(va);
377 return rc;
378}
379
380/**
381 * Replies according to the return code.
382 *
383 * @returns IPRT status code of the send.
384 * @param pThis The ATS instance.
385 * @param pClient The ATS client structure.
386 * @param pPktHdr The packet to reply to.
387 * @param rcOperation The status code to report.
388 * @param pszOperationFmt The operation that failed. Typically giving the
389 * function call with important arguments.
390 * @param ... Arguments to the format string.
391 */
392static int atsReplyRC(PATSSERVER pThis,
393 PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr, int rcOperation, const char *pszOperationFmt, ...)
394{
395 if (RT_SUCCESS(rcOperation))
396 return atsReplyAck(pThis, pClient, pPktHdr);
397
398 char szOperation[128];
399 va_list va;
400 va_start(va, pszOperationFmt);
401 RTStrPrintfV(szOperation, sizeof(szOperation), pszOperationFmt, va);
402 va_end(va);
403
404 return atsReplyFailure(pThis, pClient, pPktHdr, "FAILED ", rcOperation, "%s failed with rc=%Rrc (opcode '%.8s')",
405 szOperation, rcOperation, pPktHdr->achOpcode);
406}
407
408/**
409 * Signal a bad packet exact size.
410 *
411 * @returns IPRT status code of the send.
412 * @param pThis The ATS instance.
413 * @param pClient The ATS client structure.
414 * @param pPktHdr The packet to reply to.
415 * @param cb The wanted size.
416 */
417static int atsReplyBadSize(PATSSERVER pThis, PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr, size_t cb)
418{
419 return atsReplyFailure(pThis, pClient, pPktHdr, "BAD SIZE", VERR_INVALID_PARAMETER, "Expected at %zu bytes, got %u (opcode '%.8s')",
420 cb, pPktHdr->cb, pPktHdr->achOpcode);
421}
422
423/**
424 * Deals with a unknown command.
425 *
426 * @returns IPRT status code of the send.
427 * @param pThis The ATS instance.
428 * @param pClient The ATS client structure.
429 * @param pPktHdr The packet to reply to.
430 */
431static int atsReplyUnknown(PATSSERVER pThis, PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr)
432{
433 return atsReplyFailure(pThis, pClient, pPktHdr, "UNKNOWN ", VERR_NOT_FOUND, "Opcode '%.8s' is not known", pPktHdr->achOpcode);
434}
435
436#if 0
437/**
438 * Deals with a command which contains an unterminated string.
439 *
440 * @returns IPRT status code of the send.
441 * @param pClient The ATS client structure.
442 * @param pPktHdr The packet containing the unterminated string.
443 */
444static int atsReplyBadStrTermination(PATSCLIENT pClient, PCATSPKTHDR pPktHdr)
445{
446 return atsReplyFailure(pClient, pPktHdr, "BAD TERM", VERR_INVALID_PARAMETER, "Opcode '%.8s' contains an unterminated string", pPktHdr->achOpcode);
447}
448#endif
449
450/**
451 * Deals with a command sent in an invalid client state.
452 *
453 * @returns IPRT status code of the send.
454 * @param pThis The ATS instance.
455 * @param pClient The ATS client structure.
456 * @param pPktHdr The packet containing the unterminated string.
457 */
458static int atsReplyInvalidState(PATSSERVER pThis, PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr)
459{
460 return atsReplyFailure(pThis, pClient, pPktHdr, "INVSTATE", VERR_INVALID_STATE, "Opcode '%.8s' is not supported at client state '%s",
461 pPktHdr->achOpcode, atsClientStateStringify(pClient->enmState));
462}
463
464/**
465 * Verifies and acknowledges a "BYE" request.
466 *
467 * @returns IPRT status code.
468 * @param pThis The ATS instance.
469 * @param pClient The ATS client structure.
470 * @param pPktHdr The howdy packet.
471 */
472static int atsDoBye(PATSSERVER pThis, PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr)
473{
474 int rc;
475 if (pPktHdr->cb == sizeof(ATSPKTHDR))
476 rc = atsReplyAck(pThis, pClient, pPktHdr);
477 else
478 rc = atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTHDR));
479 return rc;
480}
481
482/**
483 * Verifies and acknowledges a "HOWDY" request.
484 *
485 * @returns IPRT status code.
486 * @param pThis The ATS instance.
487 * @param pClient The ATS client structure.
488 * @param pPktHdr The howdy packet.
489 */
490static int atsDoHowdy(PATSSERVER pThis, PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr)
491{
492 int rc = VINF_SUCCESS;
493
494 if (pPktHdr->cb != sizeof(ATSPKTREQHOWDY))
495 return atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTREQHOWDY));
496
497 if (pClient->enmState != ATSCLIENTSTATE_INITIALISING)
498 return atsReplyInvalidState(pThis, pClient, pPktHdr);
499
500 PATSPKTREQHOWDY pReq = (PATSPKTREQHOWDY)pPktHdr;
501
502 if (pReq->uVersion != ATS_PROTOCOL_VS)
503 return atsReplyRC(pThis, pClient, pPktHdr, VERR_VERSION_MISMATCH, "The given version %#x is not supported", pReq->uVersion);
504
505 ATSPKTREPHOWDY Rep;
506 RT_ZERO(Rep);
507
508 Rep.uVersion = ATS_PROTOCOL_VS;
509
510 rc = atsReplyInternal(pThis, pClient, &Rep.Sts, "ACK ", sizeof(Rep) - sizeof(ATSPKTSTS));
511 if (RT_SUCCESS(rc))
512 {
513 pThis->pTransport->pfnNotifyHowdy(pClient->pTransportClient);
514 pClient->enmState = ATSCLIENTSTATE_READY;
515 }
516
517 return rc;
518}
519
520/**
521 * Verifies and processes a "TONE PLAY" request.
522 *
523 * @returns IPRT status code.
524 * @param pThis The ATS instance.
525 * @param pClient The ATS client structure.
526 * @param pPktHdr The packet header.
527 */
528static int atsDoTonePlay(PATSSERVER pThis, PATSCLIENTINST pClient, PCATSPKTHDR pPktHdr)
529{
530 int rc = VINF_SUCCESS;
531
532 if (pPktHdr->cb < sizeof(ATSPKTREQTONEPLAY))
533 return atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTREQTONEPLAY));
534
535 if (pClient->enmState != ATSCLIENTSTATE_READY)
536 return atsReplyInvalidState(pThis, pClient, pPktHdr);
537
538 return rc;
539}
540
541/**
542 * Main request processing routine for each client.
543 *
544 * @returns IPRT status code.
545 * @param pThis The ATS instance.
546 * @param pClient The ATS client structure sending the request.
547 */
548static int atsClientReqProcess(PATSSERVER pThis, PATSCLIENTINST pClient)
549{
550 /*
551 * Read client command packet and process it.
552 */
553 PATSPKTHDR pPktHdr = NULL;
554 int rc = atsRecvPkt(pThis, pClient, &pPktHdr, true /*fAutoRetryOnFailure*/);
555 if (RT_FAILURE(rc))
556 return rc;
557
558 /*
559 * Do a string switch on the opcode bit.
560 */
561 /* Connection: */
562 if ( atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_HOWDY))
563 rc = atsDoHowdy(pThis, pClient, pPktHdr);
564 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_BYE))
565 rc = atsDoBye(pThis, pClient, pPktHdr);
566 /* Gadget API. */
567 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TONE_PLAY))
568 rc = atsDoTonePlay(pThis, pClient, pPktHdr);
569 /* Misc: */
570 else
571 rc = atsReplyUnknown(pThis, pClient, pPktHdr);
572
573 RTMemFree(pPktHdr);
574
575 return rc;
576}
577
578/**
579 * Destroys a client instance.
580 *
581 * @returns nothing.
582 * @param pClient The ATS client structure.
583 */
584static void atsClientDestroy(PATSCLIENTINST pClient)
585{
586 if (pClient->pszHostname)
587 RTStrFree(pClient->pszHostname);
588 RTMemFree(pClient);
589}
590
591/**
592 * The main thread worker serving the clients.
593 */
594static DECLCALLBACK(int) atsClientWorker(RTTHREAD hThread, void *pvUser)
595{
596 RT_NOREF(hThread);
597
598 PATSSERVER pThis = (PATSSERVER)pvUser;
599 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
600
601 unsigned cClientsMax = 0;
602 unsigned cClientsCur = 0;
603 PATSCLIENTINST *papClients = NULL;
604
605 /* Add the pipe to the poll set. */
606 int rc = RTPollSetAddPipe(pThis->hPollSet, pThis->hPipeR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 0);
607 if (RT_SUCCESS(rc))
608 {
609 while (!pThis->fTerminate)
610 {
611 uint32_t fEvts;
612 uint32_t uId;
613 rc = RTPoll(pThis->hPollSet, RT_INDEFINITE_WAIT, &fEvts, &uId);
614 if (RT_SUCCESS(rc))
615 {
616 if (uId == 0)
617 {
618 if (fEvts & RTPOLL_EVT_ERROR)
619 break;
620
621 /* We got woken up because of a new client. */
622 Assert(fEvts & RTPOLL_EVT_READ);
623
624 uint8_t bRead;
625 size_t cbRead = 0;
626 rc = RTPipeRead(pThis->hPipeR, &bRead, 1, &cbRead);
627 AssertRC(rc);
628
629 RTCritSectEnter(&pThis->CritSectClients);
630 /* Walk the list and add all new clients. */
631 PATSCLIENTINST pIt, pItNext;
632 RTListForEachSafe(&pThis->LstClientsNew, pIt, pItNext, ATSCLIENTINST, NdLst)
633 {
634 RTListNodeRemove(&pIt->NdLst);
635 Assert(cClientsCur <= cClientsMax);
636 if (cClientsCur == cClientsMax)
637 {
638 /* Realloc to accommodate for the new clients. */
639 PATSCLIENTINST *papClientsNew = (PATSCLIENTINST *)RTMemRealloc(papClients, (cClientsMax + 10) * sizeof(PATSCLIENTINST));
640 if (RT_LIKELY(papClientsNew))
641 {
642 cClientsMax += 10;
643 papClients = papClientsNew;
644 }
645 }
646 if (cClientsCur < cClientsMax)
647 {
648 /* Find a free slot in the client array. */
649 unsigned idxSlt = 0;
650 while ( idxSlt < cClientsMax
651 && papClients[idxSlt] != NULL)
652 idxSlt++;
653
654 rc = pThis->pTransport->pfnPollSetAdd(pThis->hPollSet, pIt->pTransportClient, idxSlt + 1);
655 if (RT_SUCCESS(rc))
656 {
657 cClientsCur++;
658 papClients[idxSlt] = pIt;
659 }
660 else
661 {
662 pThis->pTransport->pfnNotifyBye(pIt->pTransportClient);
663 atsClientDestroy(pIt);
664 }
665 }
666 else
667 {
668 pThis->pTransport->pfnNotifyBye(pIt->pTransportClient);
669 atsClientDestroy(pIt);
670 }
671 }
672 RTCritSectLeave(&pThis->CritSectClients);
673 }
674 else
675 {
676 /* Client sends a request, pick the right client and process it. */
677 PATSCLIENTINST pClient = papClients[uId - 1];
678 AssertPtr(pClient);
679 if (fEvts & RTPOLL_EVT_READ)
680 rc = atsClientReqProcess(pThis, pClient);
681
682 if ( (fEvts & RTPOLL_EVT_ERROR)
683 || RT_FAILURE(rc))
684 {
685 /* Close connection and remove client from array. */
686 rc = pThis->pTransport->pfnPollSetRemove(pThis->hPollSet, pClient->pTransportClient, uId);
687 AssertRC(rc);
688
689 pThis->pTransport->pfnNotifyBye(pClient->pTransportClient);
690 papClients[uId - 1] = NULL;
691 cClientsCur--;
692 atsClientDestroy(pClient);
693 }
694 }
695 }
696 }
697 }
698
699 return rc;
700}
701
702/**
703 * The main thread waiting for new client connections.
704 *
705 * @returns VBox status code.
706 */
707static DECLCALLBACK(int) atsMainThread(RTTHREAD hThread, void *pvUser)
708{
709 RT_NOREF(hThread);
710
711 PATSSERVER pThis = (PATSSERVER)pvUser;
712 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
713
714 int rc = VINF_SUCCESS;
715
716 while (!pThis->fTerminate)
717 {
718 /*
719 * Wait for new connection and spin off a new thread
720 * for every new client.
721 */
722 PATSTRANSPORTCLIENT pTransportClient;
723 rc = pThis->pTransport->pfnWaitForConnect(&pTransportClient);
724 if (RT_FAILURE(rc))
725 continue;
726
727 /*
728 * New connection, create new client structure and spin of
729 * the request handling thread.
730 */
731 PATSCLIENTINST pClient = (PATSCLIENTINST)RTMemAllocZ(sizeof(ATSCLIENTINST));
732 if (RT_LIKELY(pClient))
733 {
734 pClient->enmState = ATSCLIENTSTATE_INITIALISING;
735 pClient->pTransportClient = pTransportClient;
736 pClient->pszHostname = NULL;
737
738 /* Add client to the new list and inform the worker thread. */
739 RTCritSectEnter(&pThis->CritSectClients);
740 RTListAppend(&pThis->LstClientsNew, &pClient->NdLst);
741 RTCritSectLeave(&pThis->CritSectClients);
742
743 size_t cbWritten = 0;
744 rc = RTPipeWrite(pThis->hPipeW, "", 1, &cbWritten);
745 if (RT_FAILURE(rc))
746 RTMsgError("Failed to inform worker thread of a new client");
747 }
748 else
749 {
750 RTMsgError("Creating new client structure failed with out of memory error\n");
751 pThis->pTransport->pfnNotifyBye(pTransportClient);
752 }
753 }
754
755 return rc;
756}
757
758/**
759 * Initializes the global ATS state.
760 *
761 * @returns VBox status code.
762 * @param pThis The ATS instance.
763 * */
764int AudioTestSvcInit(PATSSERVER pThis)
765{
766 pThis->fStarted = false;
767 pThis->fTerminate = false;
768 RTListInit(&pThis->LstClientsNew);
769
770 /*
771 * The default transporter is the first one.
772 */
773 pThis->pTransport = g_apTransports[0];
774
775 /*
776 * Initialize the transport layer.
777 */
778 int rc = pThis->pTransport->pfnInit();
779 if (RT_SUCCESS(rc))
780 {
781 rc = RTCritSectInit(&pThis->CritSectClients);
782 if (RT_SUCCESS(rc))
783 {
784 rc = RTPollSetCreate(&pThis->hPollSet);
785 if (RT_SUCCESS(rc))
786 {
787 rc = RTPipeCreate(&pThis->hPipeR, &pThis->hPipeW, 0);
788 if (RT_SUCCESS(rc))
789 {
790 /* Spin off the thread serving connections. */
791 rc = RTThreadCreate(&pThis->hThreadServing, atsClientWorker, pThis, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE,
792 "AUDTSTSRVC");
793 if (RT_SUCCESS(rc))
794 return VINF_SUCCESS;
795 else
796 RTMsgError("Creating the client worker thread failed with %Rrc\n", rc);
797
798 RTPipeClose(pThis->hPipeR);
799 RTPipeClose(pThis->hPipeW);
800 }
801 else
802 RTMsgError("Creating communications pipe failed with %Rrc\n", rc);
803
804 RTPollSetDestroy(pThis->hPollSet);
805 }
806 else
807 RTMsgError("Creating pollset failed with %Rrc\n", rc);
808
809 RTCritSectDelete(&pThis->CritSectClients);
810 }
811 else
812 RTMsgError("Creating global critical section failed with %Rrc\n", rc);
813 }
814 else
815 RTMsgError("Initializing the transport layer failed with %Rrc\n", rc);
816
817 return rc;
818}
819
820/**
821 * Starts a formerly initialized ATS instance.
822 *
823 * @returns VBox status code.
824 * @param pThis The ATS instance.
825 */
826int AudioTestSvcStart(PATSSERVER pThis)
827{
828 /* Spin off the main thread. */
829 int rc = RTThreadCreate(&pThis->hThreadMain, atsMainThread, pThis, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE,
830 "AUDTSTSRVM");
831 if (RT_SUCCESS(rc))
832 pThis->fStarted = true;
833
834 return rc;
835}
836
837/**
838 * Shuts down a formerly started ATS instance.
839 *
840 * @returns VBox status code.
841 * @param pThis The ATS instance.
842 */
843int AudioTestSvcShutdown(PATSSERVER pThis)
844{
845 if (!pThis->fStarted)
846 return VINF_SUCCESS;
847
848 ASMAtomicXchgBool(&pThis->fTerminate, true);
849
850 if (pThis->pTransport)
851 pThis->pTransport->pfnTerm();
852
853 size_t cbWritten;
854 int rc = RTPipeWrite(pThis->hPipeW, "", 1, &cbWritten);
855 AssertRCReturn(rc, rc);
856
857 /* First close serving thread. */
858 int rcThread;
859 rc = RTThreadWait(pThis->hThreadServing, RT_MS_30SEC, &rcThread);
860 if (RT_SUCCESS(rc))
861 {
862 rc = rcThread;
863 if (RT_SUCCESS(rc))
864 {
865 /* Close the main thread last. */
866 rc = RTThreadWait(pThis->hThreadMain, RT_MS_30SEC, &rcThread);
867 if (RT_SUCCESS(rc))
868 rc = rcThread;
869
870 if (rc == VERR_TCP_SERVER_DESTROYED)
871 rc = VINF_SUCCESS;
872 }
873 }
874
875 if (RT_SUCCESS(rc))
876 pThis->fStarted = false;
877
878 return rc;
879}
880
881/**
882 * Destroys an ATS instance, internal version.
883 *
884 * @returns VBox status code.
885 * @param pThis ATS instance to destroy.
886 */
887static int audioTestSvcDestroyInternal(PATSSERVER pThis)
888{
889 int rc = VINF_SUCCESS;
890
891 if (pThis->hPipeR != NIL_RTPIPE)
892 {
893 rc = RTPipeClose(pThis->hPipeR);
894 AssertRCReturn(rc, rc);
895 pThis->hPipeR = NIL_RTPIPE;
896 }
897
898 if (pThis->hPipeW != NIL_RTPIPE)
899 {
900 rc = RTPipeClose(pThis->hPipeW);
901 AssertRCReturn(rc, rc);
902 pThis->hPipeW = NIL_RTPIPE;
903 }
904
905 RTPollSetDestroy(pThis->hPollSet);
906 pThis->hPollSet = NIL_RTPOLLSET;
907
908 pThis->pTransport = NULL;
909
910 PATSCLIENTINST pIt, pItNext;
911 RTListForEachSafe(&pThis->LstClientsNew, pIt, pItNext, ATSCLIENTINST, NdLst)
912 {
913 RTListNodeRemove(&pIt->NdLst);
914
915 RTMemFree(pIt);
916 pIt = NULL;
917 }
918
919 if (RTCritSectIsInitialized(&pThis->CritSectClients))
920 {
921 rc = RTCritSectDelete(&pThis->CritSectClients);
922 AssertRCReturn(rc, rc);
923 }
924
925 return rc;
926}
927
928/**
929 * Destroys an ATS instance.
930 *
931 * @returns VBox status code.
932 * @param pThis ATS instance to destroy.
933 */
934int AudioTestSvcDestroy(PATSSERVER pThis)
935{
936 return audioTestSvcDestroyInternal(pThis);
937}
938
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette