VirtualBox

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

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

Audio/ValKit: Initial support for the audio test execution service (ATS) to support playing test tones [Doxygen fixes]. bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.1 KB
Line 
1/* $Id: AudioTestService.cpp 89228 2021-05-21 15:09:16Z 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 if (!pThis->Callbacks.pfnTonePlay)
539 return atsReplyRC(pThis, pClient, pPktHdr, VERR_NOT_SUPPORTED, "Playing tones not supported");
540
541 PATSPKTREQTONEPLAY pReq = (PATSPKTREQTONEPLAY)pPktHdr;
542 rc = pThis->Callbacks.pfnTonePlay(pThis->Callbacks.pvUser, &pReq->StreamCfg, &pReq->ToneParms);
543
544 int rc2 = atsReplyAck(pThis, pClient, pPktHdr);
545 if (RT_SUCCESS(rc))
546 rc = rc2;
547
548 return rc;
549}
550
551/**
552 * Main request processing routine for each client.
553 *
554 * @returns IPRT status code.
555 * @param pThis The ATS instance.
556 * @param pClient The ATS client structure sending the request.
557 */
558static int atsClientReqProcess(PATSSERVER pThis, PATSCLIENTINST pClient)
559{
560 /*
561 * Read client command packet and process it.
562 */
563 PATSPKTHDR pPktHdr = NULL;
564 int rc = atsRecvPkt(pThis, pClient, &pPktHdr, true /*fAutoRetryOnFailure*/);
565 if (RT_FAILURE(rc))
566 return rc;
567
568 /*
569 * Do a string switch on the opcode bit.
570 */
571 /* Connection: */
572 if ( atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_HOWDY))
573 rc = atsDoHowdy(pThis, pClient, pPktHdr);
574 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_BYE))
575 rc = atsDoBye(pThis, pClient, pPktHdr);
576 /* Audio testing: */
577 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TONE_PLAY))
578 rc = atsDoTonePlay(pThis, pClient, pPktHdr);
579 /* Misc: */
580 else
581 rc = atsReplyUnknown(pThis, pClient, pPktHdr);
582
583 RTMemFree(pPktHdr);
584
585 return rc;
586}
587
588/**
589 * Destroys a client instance.
590 *
591 * @returns nothing.
592 * @param pClient The ATS client structure.
593 */
594static void atsClientDestroy(PATSCLIENTINST pClient)
595{
596 if (pClient->pszHostname)
597 RTStrFree(pClient->pszHostname);
598 RTMemFree(pClient);
599}
600
601/**
602 * The main thread worker serving the clients.
603 */
604static DECLCALLBACK(int) atsClientWorker(RTTHREAD hThread, void *pvUser)
605{
606 RT_NOREF(hThread);
607
608 PATSSERVER pThis = (PATSSERVER)pvUser;
609 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
610
611 unsigned cClientsMax = 0;
612 unsigned cClientsCur = 0;
613 PATSCLIENTINST *papClients = NULL;
614
615 /* Add the pipe to the poll set. */
616 int rc = RTPollSetAddPipe(pThis->hPollSet, pThis->hPipeR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 0);
617 if (RT_SUCCESS(rc))
618 {
619 while (!pThis->fTerminate)
620 {
621 uint32_t fEvts;
622 uint32_t uId;
623 rc = RTPoll(pThis->hPollSet, RT_INDEFINITE_WAIT, &fEvts, &uId);
624 if (RT_SUCCESS(rc))
625 {
626 if (uId == 0)
627 {
628 if (fEvts & RTPOLL_EVT_ERROR)
629 break;
630
631 /* We got woken up because of a new client. */
632 Assert(fEvts & RTPOLL_EVT_READ);
633
634 uint8_t bRead;
635 size_t cbRead = 0;
636 rc = RTPipeRead(pThis->hPipeR, &bRead, 1, &cbRead);
637 AssertRC(rc);
638
639 RTCritSectEnter(&pThis->CritSectClients);
640 /* Walk the list and add all new clients. */
641 PATSCLIENTINST pIt, pItNext;
642 RTListForEachSafe(&pThis->LstClientsNew, pIt, pItNext, ATSCLIENTINST, NdLst)
643 {
644 RTListNodeRemove(&pIt->NdLst);
645 Assert(cClientsCur <= cClientsMax);
646 if (cClientsCur == cClientsMax)
647 {
648 /* Realloc to accommodate for the new clients. */
649 PATSCLIENTINST *papClientsNew = (PATSCLIENTINST *)RTMemRealloc(papClients, (cClientsMax + 10) * sizeof(PATSCLIENTINST));
650 if (RT_LIKELY(papClientsNew))
651 {
652 cClientsMax += 10;
653 papClients = papClientsNew;
654 }
655 }
656 if (cClientsCur < cClientsMax)
657 {
658 /* Find a free slot in the client array. */
659 unsigned idxSlt = 0;
660 while ( idxSlt < cClientsMax
661 && papClients[idxSlt] != NULL)
662 idxSlt++;
663
664 rc = pThis->pTransport->pfnPollSetAdd(pThis->hPollSet, pIt->pTransportClient, idxSlt + 1);
665 if (RT_SUCCESS(rc))
666 {
667 cClientsCur++;
668 papClients[idxSlt] = pIt;
669 }
670 else
671 {
672 pThis->pTransport->pfnNotifyBye(pIt->pTransportClient);
673 atsClientDestroy(pIt);
674 }
675 }
676 else
677 {
678 pThis->pTransport->pfnNotifyBye(pIt->pTransportClient);
679 atsClientDestroy(pIt);
680 }
681 }
682 RTCritSectLeave(&pThis->CritSectClients);
683 }
684 else
685 {
686 /* Client sends a request, pick the right client and process it. */
687 PATSCLIENTINST pClient = papClients[uId - 1];
688 AssertPtr(pClient);
689 if (fEvts & RTPOLL_EVT_READ)
690 rc = atsClientReqProcess(pThis, pClient);
691
692 if ( (fEvts & RTPOLL_EVT_ERROR)
693 || RT_FAILURE(rc))
694 {
695 /* Close connection and remove client from array. */
696 rc = pThis->pTransport->pfnPollSetRemove(pThis->hPollSet, pClient->pTransportClient, uId);
697 AssertRC(rc);
698
699 pThis->pTransport->pfnNotifyBye(pClient->pTransportClient);
700 papClients[uId - 1] = NULL;
701 cClientsCur--;
702 atsClientDestroy(pClient);
703 }
704 }
705 }
706 }
707 }
708
709 return rc;
710}
711
712/**
713 * The main thread waiting for new client connections.
714 *
715 * @returns VBox status code.
716 */
717static DECLCALLBACK(int) atsMainThread(RTTHREAD hThread, void *pvUser)
718{
719 RT_NOREF(hThread);
720
721 PATSSERVER pThis = (PATSSERVER)pvUser;
722 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
723
724 int rc = VINF_SUCCESS;
725
726 while (!pThis->fTerminate)
727 {
728 /*
729 * Wait for new connection and spin off a new thread
730 * for every new client.
731 */
732 PATSTRANSPORTCLIENT pTransportClient;
733 rc = pThis->pTransport->pfnWaitForConnect(&pTransportClient);
734 if (RT_FAILURE(rc))
735 continue;
736
737 /*
738 * New connection, create new client structure and spin of
739 * the request handling thread.
740 */
741 PATSCLIENTINST pClient = (PATSCLIENTINST)RTMemAllocZ(sizeof(ATSCLIENTINST));
742 if (RT_LIKELY(pClient))
743 {
744 pClient->enmState = ATSCLIENTSTATE_INITIALISING;
745 pClient->pTransportClient = pTransportClient;
746 pClient->pszHostname = NULL;
747
748 /* Add client to the new list and inform the worker thread. */
749 RTCritSectEnter(&pThis->CritSectClients);
750 RTListAppend(&pThis->LstClientsNew, &pClient->NdLst);
751 RTCritSectLeave(&pThis->CritSectClients);
752
753 size_t cbWritten = 0;
754 rc = RTPipeWrite(pThis->hPipeW, "", 1, &cbWritten);
755 if (RT_FAILURE(rc))
756 RTMsgError("Failed to inform worker thread of a new client");
757 }
758 else
759 {
760 RTMsgError("Creating new client structure failed with out of memory error\n");
761 pThis->pTransport->pfnNotifyBye(pTransportClient);
762 }
763 }
764
765 return rc;
766}
767
768/**
769 * Initializes the global ATS state.
770 *
771 * @returns VBox status code.
772 * @param pThis The ATS instance.
773 * @param pCallbacks The callbacks table to use.
774 * */
775int AudioTestSvcInit(PATSSERVER pThis, PCATSCALLBACKS pCallbacks)
776{
777 memcpy(&pThis->Callbacks, pCallbacks, sizeof(ATSCALLBACKS));
778
779 pThis->fStarted = false;
780 pThis->fTerminate = false;
781 RTListInit(&pThis->LstClientsNew);
782
783 /*
784 * The default transporter is the first one.
785 */
786 pThis->pTransport = g_apTransports[0];
787
788 /*
789 * Initialize the transport layer.
790 */
791 int rc = pThis->pTransport->pfnInit();
792 if (RT_SUCCESS(rc))
793 {
794 rc = RTCritSectInit(&pThis->CritSectClients);
795 if (RT_SUCCESS(rc))
796 {
797 rc = RTPollSetCreate(&pThis->hPollSet);
798 if (RT_SUCCESS(rc))
799 {
800 rc = RTPipeCreate(&pThis->hPipeR, &pThis->hPipeW, 0);
801 if (RT_SUCCESS(rc))
802 {
803 /* Spin off the thread serving connections. */
804 rc = RTThreadCreate(&pThis->hThreadServing, atsClientWorker, pThis, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE,
805 "AUDTSTSRVC");
806 if (RT_SUCCESS(rc))
807 return VINF_SUCCESS;
808 else
809 RTMsgError("Creating the client worker thread failed with %Rrc\n", rc);
810
811 RTPipeClose(pThis->hPipeR);
812 RTPipeClose(pThis->hPipeW);
813 }
814 else
815 RTMsgError("Creating communications pipe failed with %Rrc\n", rc);
816
817 RTPollSetDestroy(pThis->hPollSet);
818 }
819 else
820 RTMsgError("Creating pollset failed with %Rrc\n", rc);
821
822 RTCritSectDelete(&pThis->CritSectClients);
823 }
824 else
825 RTMsgError("Creating global critical section failed with %Rrc\n", rc);
826 }
827 else
828 RTMsgError("Initializing the transport layer failed with %Rrc\n", rc);
829
830 return rc;
831}
832
833/**
834 * Starts a formerly initialized ATS instance.
835 *
836 * @returns VBox status code.
837 * @param pThis The ATS instance.
838 */
839int AudioTestSvcStart(PATSSERVER pThis)
840{
841 /* Spin off the main thread. */
842 int rc = RTThreadCreate(&pThis->hThreadMain, atsMainThread, pThis, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE,
843 "AUDTSTSRVM");
844 if (RT_SUCCESS(rc))
845 pThis->fStarted = true;
846
847 return rc;
848}
849
850/**
851 * Shuts down a formerly started ATS instance.
852 *
853 * @returns VBox status code.
854 * @param pThis The ATS instance.
855 */
856int AudioTestSvcShutdown(PATSSERVER pThis)
857{
858 if (!pThis->fStarted)
859 return VINF_SUCCESS;
860
861 ASMAtomicXchgBool(&pThis->fTerminate, true);
862
863 if (pThis->pTransport)
864 pThis->pTransport->pfnTerm();
865
866 size_t cbWritten;
867 int rc = RTPipeWrite(pThis->hPipeW, "", 1, &cbWritten);
868 AssertRCReturn(rc, rc);
869
870 /* First close serving thread. */
871 int rcThread;
872 rc = RTThreadWait(pThis->hThreadServing, RT_MS_30SEC, &rcThread);
873 if (RT_SUCCESS(rc))
874 {
875 rc = rcThread;
876 if (RT_SUCCESS(rc))
877 {
878 /* Close the main thread last. */
879 rc = RTThreadWait(pThis->hThreadMain, RT_MS_30SEC, &rcThread);
880 if (RT_SUCCESS(rc))
881 rc = rcThread;
882
883 if (rc == VERR_TCP_SERVER_DESTROYED)
884 rc = VINF_SUCCESS;
885 }
886 }
887
888 if (RT_SUCCESS(rc))
889 pThis->fStarted = false;
890
891 return rc;
892}
893
894/**
895 * Destroys an ATS instance, internal version.
896 *
897 * @returns VBox status code.
898 * @param pThis ATS instance to destroy.
899 */
900static int audioTestSvcDestroyInternal(PATSSERVER pThis)
901{
902 int rc = VINF_SUCCESS;
903
904 if (pThis->hPipeR != NIL_RTPIPE)
905 {
906 rc = RTPipeClose(pThis->hPipeR);
907 AssertRCReturn(rc, rc);
908 pThis->hPipeR = NIL_RTPIPE;
909 }
910
911 if (pThis->hPipeW != NIL_RTPIPE)
912 {
913 rc = RTPipeClose(pThis->hPipeW);
914 AssertRCReturn(rc, rc);
915 pThis->hPipeW = NIL_RTPIPE;
916 }
917
918 RTPollSetDestroy(pThis->hPollSet);
919 pThis->hPollSet = NIL_RTPOLLSET;
920
921 pThis->pTransport = NULL;
922
923 PATSCLIENTINST pIt, pItNext;
924 RTListForEachSafe(&pThis->LstClientsNew, pIt, pItNext, ATSCLIENTINST, NdLst)
925 {
926 RTListNodeRemove(&pIt->NdLst);
927
928 RTMemFree(pIt);
929 pIt = NULL;
930 }
931
932 if (RTCritSectIsInitialized(&pThis->CritSectClients))
933 {
934 rc = RTCritSectDelete(&pThis->CritSectClients);
935 AssertRCReturn(rc, rc);
936 }
937
938 return rc;
939}
940
941/**
942 * Destroys an ATS instance.
943 *
944 * @returns VBox status code.
945 * @param pThis ATS instance to destroy.
946 */
947int AudioTestSvcDestroy(PATSSERVER pThis)
948{
949 return audioTestSvcDestroyInternal(pThis);
950}
951
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