VirtualBox

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

Last change on this file since 104587 was 99739, checked in by vboxsync, 18 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.0 KB
Line 
1/* $Id: AudioTestService.cpp 99739 2023-05-11 01:01:08Z vboxsync $ */
2/** @file
3 * AudioTestService - Audio test execution server.
4 */
5
6/*
7 * Copyright (C) 2021-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_AUDIO_TEST
33#include <iprt/log.h>
34
35#include <iprt/alloca.h>
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/critsect.h>
39#include <iprt/crc.h>
40#include <iprt/ctype.h>
41#include <iprt/dir.h>
42#include <iprt/env.h>
43#include <iprt/err.h>
44#include <iprt/file.h>
45#include <iprt/getopt.h>
46#include <iprt/handle.h>
47#include <iprt/initterm.h>
48#include <iprt/json.h>
49#include <iprt/list.h>
50#include <iprt/mem.h>
51#include <iprt/message.h>
52#include <iprt/param.h>
53#include <iprt/path.h>
54#include <iprt/pipe.h>
55#include <iprt/poll.h>
56#include <iprt/process.h>
57#include <iprt/stream.h>
58#include <iprt/string.h>
59#include <iprt/thread.h>
60
61#include <VBox/log.h>
62
63#include "AudioTestService.h"
64#include "AudioTestServiceInternal.h"
65
66
67/*********************************************************************************************************************************
68* Structures and Typedefs *
69*********************************************************************************************************************************/
70/**
71 * A generic ATS reply, used by the client
72 * to process the incoming packets.
73 */
74typedef struct ATSSRVREPLY
75{
76 char szOp[ATSPKT_OPCODE_MAX_LEN];
77 void *pvPayload;
78 size_t cbPayload;
79} ATSSRVREPLY;
80/** Pointer to a generic ATS reply. */
81typedef struct ATSSRVREPLY *PATSSRVREPLY;
82
83
84/*********************************************************************************************************************************
85* Global Variables *
86*********************************************************************************************************************************/
87/**
88 * Transport layers.
89 */
90const PCATSTRANSPORT g_apTransports[] =
91{
92 &g_TcpTransport
93};
94/** Number of transport layers in \a g_apTransports. */
95const size_t g_cTransports = RT_ELEMENTS(g_apTransports);
96
97/**
98 * ATS client state.
99 */
100typedef enum ATSCLIENTSTATE
101{
102 /** Invalid client state. */
103 ATSCLIENTSTATE_INVALID = 0,
104 /** Client is initialising, only the HOWDY and BYE packets are allowed. */
105 ATSCLIENTSTATE_INITIALISING,
106 /** Client is in fully cuntional state and ready to process all requests. */
107 ATSCLIENTSTATE_READY,
108 /** Client is destroying. */
109 ATSCLIENTSTATE_DESTROYING,
110 /** 32bit hack. */
111 ATSCLIENTSTATE_32BIT_HACK = 0x7fffffff
112} ATSCLIENTSTATE;
113
114/**
115 * ATS client instance.
116 */
117typedef struct ATSCLIENTINST
118{
119 /** List node for new clients. */
120 RTLISTNODE NdLst;
121 /** The current client state. */
122 ATSCLIENTSTATE enmState;
123 /** Transport backend specific data. */
124 PATSTRANSPORTCLIENT pTransportClient;
125 /** Client hostname. */
126 char *pszHostname;
127} ATSCLIENTINST;
128/** Pointer to a ATS client instance. */
129typedef ATSCLIENTINST *PATSCLIENTINST;
130
131
132/*********************************************************************************************************************************
133* Prototypes *
134*********************************************************************************************************************************/
135static int atsClientDisconnect(PATSSERVER pThis, PATSCLIENTINST pInst);
136
137
138
139/**
140 * Returns the string represenation of the given state.
141 */
142static const char *atsClientStateStringify(ATSCLIENTSTATE enmState)
143{
144 switch (enmState)
145 {
146 case ATSCLIENTSTATE_INVALID:
147 return "INVALID";
148 case ATSCLIENTSTATE_INITIALISING:
149 return "INITIALISING";
150 case ATSCLIENTSTATE_READY:
151 return "READY";
152 case ATSCLIENTSTATE_DESTROYING:
153 return "DESTROYING";
154 case ATSCLIENTSTATE_32BIT_HACK:
155 default:
156 break;
157 }
158
159 AssertMsgFailed(("Unknown state %#x\n", enmState));
160 return "UNKNOWN";
161}
162
163/**
164 * Calculates the checksum value, zero any padding space and send the packet.
165 *
166 * @returns IPRT status code.
167 * @param pThis The ATS instance.
168 * @param pInst The ATS client structure.
169 * @param pPkt The packet to send. Must point to a correctly
170 * aligned buffer.
171 */
172static int atsSendPkt(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPkt)
173{
174 Assert(pPkt->cb >= sizeof(*pPkt));
175 pPkt->uCrc32 = RTCrc32(pPkt->achOpcode, pPkt->cb - RT_UOFFSETOF(ATSPKTHDR, achOpcode));
176 if (pPkt->cb != RT_ALIGN_32(pPkt->cb, ATSPKT_ALIGNMENT))
177 memset((uint8_t *)pPkt + pPkt->cb, '\0', RT_ALIGN_32(pPkt->cb, ATSPKT_ALIGNMENT) - pPkt->cb);
178
179 LogFlowFunc(("cb=%RU32 (%#x), payload=%RU32 (%#x), opcode=%.8s\n",
180 pPkt->cb, pPkt->cb, pPkt->cb - sizeof(ATSPKTHDR), pPkt->cb - sizeof(ATSPKTHDR), pPkt->achOpcode));
181 int rc = pThis->pTransport->pfnSendPkt(pThis->pTransportInst, pInst->pTransportClient, pPkt);
182 while (RT_UNLIKELY(rc == VERR_INTERRUPTED) && !pThis->fTerminate)
183 rc = pThis->pTransport->pfnSendPkt(pThis->pTransportInst, pInst->pTransportClient, pPkt);
184
185 return rc;
186}
187
188/**
189 * Sends a babble reply and disconnects the client (if applicable).
190 *
191 * @param pThis The ATS instance.
192 * @param pInst The ATS server instance.
193 * @param pszOpcode The BABBLE opcode.
194 */
195static void atsReplyBabble(PATSSERVER pThis, PATSCLIENTINST pInst, const char *pszOpcode)
196{
197 ATSPKTHDR Reply;
198 Reply.cb = sizeof(Reply);
199 Reply.uCrc32 = 0;
200 memcpy(Reply.achOpcode, pszOpcode, sizeof(Reply.achOpcode));
201
202 pThis->pTransport->pfnBabble(pThis->pTransportInst, pInst->pTransportClient, &Reply, 20*1000);
203}
204
205/**
206 * Receive and validate a packet.
207 *
208 * Will send bable responses to malformed packets that results in a error status
209 * code.
210 *
211 * @returns IPRT status code.
212 * @param pThis The ATS instance.
213 * @param pInst The opaque ATS instance structure.
214 * @param ppPktHdr Where to return the packet on success. Free
215 * with RTMemFree.
216 * @param fAutoRetryOnFailure Whether to retry on error.
217 */
218static int atsRecvPkt(PATSSERVER pThis, PATSCLIENTINST pInst, PPATSPKTHDR ppPktHdr, bool fAutoRetryOnFailure)
219{
220 for (;;)
221 {
222 PATSPKTHDR pPktHdr;
223 int rc = pThis->pTransport->pfnRecvPkt(pThis->pTransportInst, pInst->pTransportClient, &pPktHdr);
224 if (RT_SUCCESS(rc))
225 {
226 /* validate the packet. */
227 if ( pPktHdr->cb >= sizeof(ATSPKTHDR)
228 && pPktHdr->cb < ATSPKT_MAX_SIZE)
229 {
230 Log2Func(("pPktHdr=%p cb=%#x crc32=%#x opcode=%.8s\n",
231 pPktHdr, pPktHdr->cb, pPktHdr->uCrc32, pPktHdr->achOpcode));
232 uint32_t uCrc32Calc = pPktHdr->uCrc32 != 0
233 ? RTCrc32(&pPktHdr->achOpcode[0], pPktHdr->cb - RT_UOFFSETOF(ATSPKTHDR, achOpcode))
234 : 0;
235 if (pPktHdr->uCrc32 == uCrc32Calc)
236 {
237 AssertCompileMemberSize(ATSPKTHDR, achOpcode, 8);
238 if ( RT_C_IS_UPPER(pPktHdr->achOpcode[0])
239 && RT_C_IS_UPPER(pPktHdr->achOpcode[1])
240 && (RT_C_IS_UPPER(pPktHdr->achOpcode[2]) || pPktHdr->achOpcode[2] == ' ')
241 && (RT_C_IS_PRINT(pPktHdr->achOpcode[3]) || pPktHdr->achOpcode[3] == ' ')
242 && (RT_C_IS_PRINT(pPktHdr->achOpcode[4]) || pPktHdr->achOpcode[4] == ' ')
243 && (RT_C_IS_PRINT(pPktHdr->achOpcode[5]) || pPktHdr->achOpcode[5] == ' ')
244 && (RT_C_IS_PRINT(pPktHdr->achOpcode[6]) || pPktHdr->achOpcode[6] == ' ')
245 && (RT_C_IS_PRINT(pPktHdr->achOpcode[7]) || pPktHdr->achOpcode[7] == ' ')
246 )
247 {
248 Log(("cb=%#x opcode=%.8s\n", pPktHdr->cb, pPktHdr->achOpcode));
249 *ppPktHdr = pPktHdr;
250 return rc;
251 }
252
253 rc = VERR_IO_BAD_COMMAND;
254 }
255 else
256 {
257 Log(("cb=%#x opcode=%.8s crc32=%#x actual=%#x\n",
258 pPktHdr->cb, pPktHdr->achOpcode, pPktHdr->uCrc32, uCrc32Calc));
259 rc = VERR_IO_CRC;
260 }
261 }
262 else
263 rc = VERR_IO_BAD_LENGTH;
264
265 /* Send babble reply and disconnect the client if the transport is
266 connection oriented. */
267 if (rc == VERR_IO_BAD_LENGTH)
268 atsReplyBabble(pThis, pInst, "BABBLE L");
269 else if (rc == VERR_IO_CRC)
270 atsReplyBabble(pThis, pInst, "BABBLE C");
271 else if (rc == VERR_IO_BAD_COMMAND)
272 atsReplyBabble(pThis, pInst, "BABBLE O");
273 else
274 atsReplyBabble(pThis, pInst, "BABBLE ");
275 RTMemFree(pPktHdr);
276 }
277
278 /* Try again or return failure? */
279 if ( pThis->fTerminate
280 || rc != VERR_INTERRUPTED
281 || !fAutoRetryOnFailure
282 )
283 {
284 Log(("rc=%Rrc\n", rc));
285 return rc;
286 }
287 }
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 pInst The opaque ATS instance structure.
296 * @param pReply The reply packet.
297 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
298 * with space.
299 * @param cbExtra Bytes in addition to the header.
300 */
301static int atsReplyInternal(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pReply, const char *pszOpcode, size_t cbExtra)
302{
303 /* copy the opcode, don't be too strict in case of a padding screw up. */
304 size_t cchOpcode = strlen(pszOpcode);
305 if (RT_LIKELY(cchOpcode == sizeof(pReply->achOpcode)))
306 memcpy(pReply->achOpcode, pszOpcode, sizeof(pReply->achOpcode));
307 else
308 {
309 Assert(cchOpcode == sizeof(pReply->achOpcode));
310 while (cchOpcode > 0 && pszOpcode[cchOpcode - 1] == ' ')
311 cchOpcode--;
312 AssertMsgReturn(cchOpcode < sizeof(pReply->achOpcode), ("%d/'%.8s'\n", cchOpcode, pszOpcode), VERR_INTERNAL_ERROR_4);
313 memcpy(pReply->achOpcode, pszOpcode, cchOpcode);
314 memset(&pReply->achOpcode[cchOpcode], ' ', sizeof(pReply->achOpcode) - cchOpcode);
315 }
316
317 pReply->cb = (uint32_t)sizeof(ATSPKTHDR) + (uint32_t)cbExtra;
318 pReply->uCrc32 = 0;
319
320 return atsSendPkt(pThis, pInst, pReply);
321}
322
323/**
324 * Make a simple reply, only status opcode.
325 *
326 * @returns IPRT status code of the send.
327 * @param pThis The ATS instance.
328 * @param pInst The opaque ATS instance structure.
329 * @param pPktHdr The original packet (for future use).
330 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
331 * with space.
332 */
333static int atsReplySimple(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr, const char *pszOpcode)
334{
335 return atsReplyInternal(pThis, pInst, pPktHdr, pszOpcode, 0);
336}
337
338/**
339 * Acknowledges a packet with success.
340 *
341 * @returns IPRT status code of the send.
342 * @param pThis The ATS instance.
343 * @param pInst The opaque ATS instance structure.
344 * @param pPktHdr The original packet (for future use).
345 */
346static int atsReplyAck(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
347{
348 return atsReplySimple(pThis, pInst, pPktHdr, "ACK ");
349}
350
351/**
352 * Replies with a failure.
353 *
354 * @returns IPRT status code of the send.
355 * @param pThis The ATS instance.
356 * @param pInst The opaque ATS instance structure.
357 * @param pPktHdr The original packet (for future use).
358 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
359 * with space.
360 * @param rcReq The status code of the request.
361 * @param pszDetailFmt Longer description of the problem (format string).
362 * @param va Format arguments.
363 */
364static int atsReplyFailureV(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr,
365 const char *pszOpcode, int rcReq, const char *pszDetailFmt, va_list va)
366{
367 RT_NOREF(pPktHdr);
368
369 ATSPKTREPFAIL Rep;
370 RT_ZERO(Rep);
371
372 size_t cchDetail = RTStrPrintfV(Rep.ach, sizeof(Rep.ach), pszDetailFmt, va);
373
374 Rep.rc = rcReq;
375
376 return atsReplyInternal(pThis, pInst, &Rep.Hdr, pszOpcode, sizeof(Rep.rc) + cchDetail + 1);
377}
378
379/**
380 * Replies with a failure.
381 *
382 * @returns IPRT status code of the send.
383 * @param pThis The ATS instance.
384 * @param pInst The opaque ATS instance structure.
385 * @param pPktHdr The original packet (for future use).
386 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
387 * with space.
388 * @param rcReq Status code.
389 * @param pszDetailFmt Longer description of the problem (format string).
390 * @param ... Format arguments.
391 */
392static int atsReplyFailure(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr,
393 const char *pszOpcode, int rcReq, const char *pszDetailFmt, ...)
394{
395 va_list va;
396 va_start(va, pszDetailFmt);
397 int rc = atsReplyFailureV(pThis, pInst, pPktHdr, pszOpcode, rcReq, pszDetailFmt, va);
398 va_end(va);
399 return rc;
400}
401
402/**
403 * Replies according to the return code.
404 *
405 * @returns IPRT status code of the send.
406 * @param pThis The ATS instance.
407 * @param pInst The opaque ATS instance structure.
408 * @param pPktHdr The packet to reply to.
409 * @param rcOperation The status code to report.
410 * @param pszOperationFmt The operation that failed. Typically giving the
411 * function call with important arguments.
412 * @param ... Arguments to the format string.
413 */
414static int atsReplyRC(PATSSERVER pThis,
415 PATSCLIENTINST pInst, PATSPKTHDR pPktHdr, int rcOperation, const char *pszOperationFmt, ...)
416{
417 if (RT_SUCCESS(rcOperation))
418 return atsReplyAck(pThis, pInst, pPktHdr);
419
420 char szOperation[128];
421 va_list va;
422 va_start(va, pszOperationFmt);
423 RTStrPrintfV(szOperation, sizeof(szOperation), pszOperationFmt, va);
424 va_end(va);
425
426 return atsReplyFailure(pThis, pInst, pPktHdr, "FAILED ", rcOperation, "%s failed with rc=%Rrc (opcode '%.8s')",
427 szOperation, rcOperation, pPktHdr->achOpcode);
428}
429
430/**
431 * Signal a bad packet exact size.
432 *
433 * @returns IPRT status code of the send.
434 * @param pThis The ATS instance.
435 * @param pInst The opaque ATS instance structure.
436 * @param pPktHdr The packet to reply to.
437 * @param cb The wanted size.
438 */
439static int atsReplyBadSize(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr, size_t cb)
440{
441 return atsReplyFailure(pThis, pInst, pPktHdr, "BAD SIZE", VERR_INVALID_PARAMETER, "Expected at %zu bytes, got %u (opcode '%.8s')",
442 cb, pPktHdr->cb, pPktHdr->achOpcode);
443}
444
445/**
446 * Deals with a unknown command.
447 *
448 * @returns IPRT status code of the send.
449 * @param pThis The ATS instance.
450 * @param pInst The opaque ATS instance structure.
451 * @param pPktHdr The packet to reply to.
452 */
453static int atsReplyUnknown(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
454{
455 return atsReplyFailure(pThis, pInst, pPktHdr, "UNKNOWN ", VERR_NOT_FOUND, "Opcode '%.8s' is not known", pPktHdr->achOpcode);
456}
457
458/**
459 * Deals with a command sent in an invalid client state.
460 *
461 * @returns IPRT status code of the send.
462 * @param pThis The ATS instance.
463 * @param pInst The opaque ATS instance structure.
464 * @param pPktHdr The packet containing the unterminated string.
465 */
466static int atsReplyInvalidState(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
467{
468 return atsReplyFailure(pThis, pInst, pPktHdr, "INVSTATE", VERR_INVALID_STATE, "Opcode '%.8s' is not supported at client state '%s",
469 pPktHdr->achOpcode, atsClientStateStringify(pInst->enmState));
470}
471
472/**
473 * Verifies and acknowledges a "BYE" request.
474 *
475 * @returns IPRT status code.
476 * @param pThis The ATS instance.
477 * @param pInst The opaque ATS instance structure.
478 * @param pPktHdr The bye packet.
479 */
480static int atsDoBye(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
481{
482 int rc;
483 if (pPktHdr->cb == sizeof(ATSPKTHDR))
484 {
485 if (pThis->Callbacks.pfnBye)
486 {
487 rc = pThis->Callbacks.pfnBye(pThis->Callbacks.pvUser);
488 }
489 else
490 rc = VINF_SUCCESS;
491
492 if (RT_SUCCESS(rc))
493 {
494 rc = atsReplyAck(pThis, pInst, pPktHdr);
495 }
496 else
497 rc = atsReplyRC(pThis, pInst, pPktHdr, rc, "Disconnecting client failed");
498 }
499 else
500 rc = atsReplyBadSize(pThis, pInst, pPktHdr, sizeof(ATSPKTHDR));
501 return rc;
502}
503
504/**
505 * Verifies and acknowledges a "HOWDY" request.
506 *
507 * @returns IPRT status code.
508 * @param pThis The ATS instance.
509 * @param pInst The opaque ATS instance structure.
510 * @param pPktHdr The howdy packet.
511 */
512static int atsDoHowdy(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
513{
514 int rc = VINF_SUCCESS;
515
516 if (pPktHdr->cb != sizeof(ATSPKTREQHOWDY))
517 return atsReplyBadSize(pThis, pInst, pPktHdr, sizeof(ATSPKTREQHOWDY));
518
519 if (pInst->enmState != ATSCLIENTSTATE_INITIALISING)
520 return atsReplyInvalidState(pThis, pInst, pPktHdr);
521
522 PATSPKTREQHOWDY pReq = (PATSPKTREQHOWDY)pPktHdr;
523
524 if (pReq->uVersion != ATS_PROTOCOL_VS)
525 return atsReplyRC(pThis, pInst, pPktHdr, VERR_VERSION_MISMATCH, "The given version %#x is not supported", pReq->uVersion);
526
527 ATSPKTREPHOWDY Rep;
528 RT_ZERO(Rep);
529
530 Rep.uVersion = ATS_PROTOCOL_VS;
531
532 rc = atsReplyInternal(pThis, pInst, &Rep.Hdr, "ACK ", sizeof(Rep) - sizeof(ATSPKTHDR));
533 if (RT_SUCCESS(rc))
534 {
535 pThis->pTransport->pfnNotifyHowdy(pThis->pTransportInst, pInst->pTransportClient);
536
537 if (pThis->Callbacks.pfnHowdy)
538 rc = pThis->Callbacks.pfnHowdy(pThis->Callbacks.pvUser);
539
540 if (RT_SUCCESS(rc))
541 pInst->enmState = ATSCLIENTSTATE_READY;
542 }
543
544 return rc;
545}
546
547/**
548 * Verifies and acknowledges a "TSET BEG" request.
549 *
550 * @returns IPRT status code.
551 * @param pThis The ATS instance.
552 * @param pInst The opaque ATS instance structure.
553 * @param pPktHdr The test set begin packet.
554 */
555static int atsDoTestSetBegin(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
556{
557 if (pPktHdr->cb != sizeof(ATSPKTREQTSETBEG))
558 return atsReplyBadSize(pThis, pInst, pPktHdr, sizeof(ATSPKTREQTSETBEG));
559
560 PATSPKTREQTSETBEG pReq = (PATSPKTREQTSETBEG)pPktHdr;
561
562 int rc = VINF_SUCCESS;
563
564 if (pThis->Callbacks.pfnTestSetBegin)
565 rc = pThis->Callbacks.pfnTestSetBegin(pThis->Callbacks.pvUser, pReq->szTag);
566
567 if (RT_SUCCESS(rc))
568 rc = atsReplyAck(pThis, pInst, pPktHdr);
569 else
570 rc = atsReplyRC(pThis, pInst, pPktHdr, rc, "Beginning test set failed");
571 return rc;
572}
573
574/**
575 * Verifies and acknowledges a "TSET END" request.
576 *
577 * @returns IPRT status code.
578 * @param pThis The ATS instance.
579 * @param pInst The opaque ATS instance structure.
580 * @param pPktHdr The test set end packet.
581 */
582static int atsDoTestSetEnd(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
583{
584 if (pPktHdr->cb != sizeof(ATSPKTREQTSETEND))
585 return atsReplyBadSize(pThis, pInst, pPktHdr, sizeof(ATSPKTREQTSETEND));
586
587 PATSPKTREQTSETEND pReq = (PATSPKTREQTSETEND)pPktHdr;
588
589 int rc = VINF_SUCCESS;
590
591 if (pThis->Callbacks.pfnTestSetEnd)
592 rc = pThis->Callbacks.pfnTestSetEnd(pThis->Callbacks.pvUser, pReq->szTag);
593
594 if (RT_SUCCESS(rc))
595 rc = atsReplyAck(pThis, pInst, pPktHdr);
596 else
597 rc = atsReplyRC(pThis, pInst, pPktHdr, rc, "Ending test set failed");
598 return rc;
599}
600
601/**
602 * Used by atsDoTestSetSend to wait for a reply ACK from the client.
603 *
604 * @returns VINF_SUCCESS on ACK, VERR_GENERAL_FAILURE on NACK,
605 * VERR_NET_NOT_CONNECTED on unknown response (sending a bable reply),
606 * or whatever atsRecvPkt returns.
607 * @param pThis The ATS instance.
608 * @param pInst The opaque ATS instance structure.
609 * @param pPktHdr The original packet (for future use).
610 */
611static int atsWaitForAck(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
612{
613 RT_NOREF(pPktHdr);
614 /** @todo timeout? */
615 PATSPKTHDR pReply;
616 int rc = atsRecvPkt(pThis, pInst, &pReply, false /*fAutoRetryOnFailure*/);
617 if (RT_SUCCESS(rc))
618 {
619 if (atsIsSameOpcode(pReply, "ACK"))
620 rc = VINF_SUCCESS;
621 else if (atsIsSameOpcode(pReply, "NACK"))
622 rc = VERR_GENERAL_FAILURE;
623 else
624 {
625 atsReplyBabble(pThis, pInst, "BABBLE ");
626 rc = VERR_NET_NOT_CONNECTED;
627 }
628 RTMemFree(pReply);
629 }
630 return rc;
631}
632
633/**
634 * Verifies and acknowledges a "TSET SND" request.
635 *
636 * @returns IPRT status code.
637 * @param pThis The ATS instance.
638 * @param pInst The opaque ATS instance structure.
639 * @param pPktHdr The test set end packet.
640 */
641static int atsDoTestSetSend(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
642{
643 if (pPktHdr->cb != sizeof(ATSPKTREQTSETSND))
644 return atsReplyBadSize(pThis, pInst, pPktHdr, sizeof(ATSPKTREQTSETSND));
645
646 PATSPKTREQTSETSND pReq = (PATSPKTREQTSETSND)pPktHdr;
647
648 int rc = VINF_SUCCESS;
649
650 if (!pThis->Callbacks.pfnTestSetSendRead)
651 return atsReplyRC(pThis, pInst, pPktHdr, VERR_NOT_SUPPORTED, "Sending test set not implemented");
652
653 if (pThis->Callbacks.pfnTestSetSendBegin)
654 {
655 rc = pThis->Callbacks.pfnTestSetSendBegin(pThis->Callbacks.pvUser, pReq->szTag);
656 if (RT_FAILURE(rc))
657 return atsReplyRC(pThis, pInst, pPktHdr, rc, "Beginning sending test set '%s' failed", pReq->szTag);
658 }
659
660 for (;;)
661 {
662 uint32_t uMyCrc32 = RTCrc32Start();
663 struct
664 {
665 ATSPKTHDR Hdr;
666 uint32_t uCrc32;
667 char ab[_64K];
668 char abPadding[ATSPKT_ALIGNMENT];
669 } Pkt;
670#ifdef DEBUG
671 RT_ZERO(Pkt);
672#endif
673 size_t cbRead = 0;
674 rc = pThis->Callbacks.pfnTestSetSendRead(pThis->Callbacks.pvUser, pReq->szTag, &Pkt.ab, sizeof(Pkt.ab), &cbRead);
675 if ( RT_FAILURE(rc)
676 || cbRead == 0)
677 {
678 if ( rc == VERR_EOF
679 || (RT_SUCCESS(rc) && cbRead == 0))
680 {
681 Pkt.uCrc32 = RTCrc32Finish(uMyCrc32);
682 rc = atsReplyInternal(pThis, pInst, &Pkt.Hdr, "DATA EOF", sizeof(uint32_t) /* uCrc32 */);
683 if (RT_SUCCESS(rc))
684 rc = atsWaitForAck(pThis, pInst, &Pkt.Hdr);
685 }
686 else
687 rc = atsReplyRC(pThis, pInst, pPktHdr, rc, "Sending data for test set '%s' failed", pReq->szTag);
688 break;
689 }
690
691 uMyCrc32 = RTCrc32Process(uMyCrc32, &Pkt.ab[0], cbRead);
692 Pkt.uCrc32 = RTCrc32Finish(uMyCrc32);
693
694 Log2Func(("cbRead=%zu -> uCrc32=%#x\n", cbRead, Pkt.uCrc32));
695
696 Assert(cbRead <= sizeof(Pkt.ab));
697
698 rc = atsReplyInternal(pThis, pInst, &Pkt.Hdr, "DATA ", sizeof(uint32_t) /* uCrc32 */ + cbRead);
699 if (RT_FAILURE(rc))
700 break;
701
702 rc = atsWaitForAck(pThis, pInst, &Pkt.Hdr);
703 if (RT_FAILURE(rc))
704 break;
705 }
706
707 if (pThis->Callbacks.pfnTestSetSendEnd)
708 {
709 int rc2 = pThis->Callbacks.pfnTestSetSendEnd(pThis->Callbacks.pvUser, pReq->szTag);
710 if (RT_FAILURE(rc2))
711 return atsReplyRC(pThis, pInst, pPktHdr, rc2, "Ending sending test set '%s' failed", pReq->szTag);
712 }
713
714 return rc;
715}
716
717/**
718 * Verifies and processes a "TN PLY" request.
719 *
720 * @returns IPRT status code.
721 * @param pThis The ATS instance.
722 * @param pInst The opaque ATS instance structure.
723 * @param pPktHdr The packet header.
724 */
725static int atsDoTonePlay(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
726{
727 if (pPktHdr->cb < sizeof(ATSPKTREQTONEPLAY))
728 return atsReplyBadSize(pThis, pInst, pPktHdr, sizeof(ATSPKTREQTONEPLAY));
729
730 if (pInst->enmState != ATSCLIENTSTATE_READY)
731 return atsReplyInvalidState(pThis, pInst, pPktHdr);
732
733 int rc = VINF_SUCCESS;
734
735 PATSPKTREQTONEPLAY pReq = (PATSPKTREQTONEPLAY)pPktHdr;
736
737 if (pThis->Callbacks.pfnTonePlay)
738 rc = pThis->Callbacks.pfnTonePlay(pThis->Callbacks.pvUser, &pReq->ToneParms);
739
740 if (RT_SUCCESS(rc))
741 rc = atsReplyAck(pThis, pInst, pPktHdr);
742 else
743 rc = atsReplyRC(pThis, pInst, pPktHdr, rc, "Playing test tone failed");
744 return rc;
745}
746
747/**
748 * Verifies and processes a "TN REC" request.
749 *
750 * @returns IPRT status code.
751 * @param pThis The ATS instance.
752 * @param pInst The opaque ATS instance structure.
753 * @param pPktHdr The packet header.
754 */
755static int atsDoToneRecord(PATSSERVER pThis, PATSCLIENTINST pInst, PATSPKTHDR pPktHdr)
756{
757 if (pPktHdr->cb < sizeof(ATSPKTREQTONEREC))
758 return atsReplyBadSize(pThis, pInst, pPktHdr, sizeof(ATSPKTREQTONEREC));
759
760 if (pInst->enmState != ATSCLIENTSTATE_READY)
761 return atsReplyInvalidState(pThis, pInst, pPktHdr);
762
763 int rc = VINF_SUCCESS;
764
765 PATSPKTREQTONEREC pReq = (PATSPKTREQTONEREC)pPktHdr;
766
767 if (pThis->Callbacks.pfnToneRecord)
768 rc = pThis->Callbacks.pfnToneRecord(pThis->Callbacks.pvUser, &pReq->ToneParms);
769
770 if (RT_SUCCESS(rc))
771 rc = atsReplyAck(pThis, pInst, pPktHdr);
772 else
773 rc = atsReplyRC(pThis, pInst, pPktHdr, rc, "Recording test tone failed");
774 return rc;
775}
776
777/**
778 * Main request processing routine for each client.
779 *
780 * @returns IPRT status code.
781 * @param pThis The ATS instance.
782 * @param pInst The ATS client structure sending the request.
783 * @param pfDisconnect Where to return whether to disconnect the client on success or not.
784 */
785static int atsClientReqProcess(PATSSERVER pThis, PATSCLIENTINST pInst, bool *pfDisconnect)
786{
787 LogRelFlowFuncEnter();
788
789 /*
790 * Read client command packet and process it.
791 */
792 PATSPKTHDR pPktHdr = NULL;
793 int rc = atsRecvPkt(pThis, pInst, &pPktHdr, true /*fAutoRetryOnFailure*/);
794 if (RT_FAILURE(rc))
795 return rc;
796
797 /*
798 * Do a string switch on the opcode bit.
799 */
800 /* Connection: */
801 if ( atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_HOWDY))
802 rc = atsDoHowdy(pThis, pInst, pPktHdr);
803 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_BYE))
804 {
805 rc = atsDoBye(pThis, pInst, pPktHdr);
806 if (RT_SUCCESS(rc))
807 *pfDisconnect = true;
808 }
809 /* Test set handling: */
810 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TESTSET_BEGIN))
811 rc = atsDoTestSetBegin(pThis, pInst, pPktHdr);
812 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TESTSET_END))
813 rc = atsDoTestSetEnd(pThis, pInst, pPktHdr);
814 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TESTSET_SEND))
815 rc = atsDoTestSetSend(pThis, pInst, pPktHdr);
816 /* Audio testing: */
817 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TONE_PLAY))
818 rc = atsDoTonePlay(pThis, pInst, pPktHdr);
819 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TONE_RECORD))
820 rc = atsDoToneRecord(pThis, pInst, pPktHdr);
821 /* Misc: */
822 else
823 rc = atsReplyUnknown(pThis, pInst, pPktHdr);
824
825 RTMemFree(pPktHdr);
826
827 LogRelFlowFuncLeaveRC(rc);
828 return rc;
829}
830
831/**
832 * Disconnects a client.
833 *
834 * @returns VBox status code.
835 * @param pThis The ATS instance.
836 * @param pInst The ATS client to disconnect.
837 */
838static int atsClientDisconnect(PATSSERVER pThis, PATSCLIENTINST pInst)
839{
840 AssertReturn(pInst->enmState != ATSCLIENTSTATE_DESTROYING, VERR_WRONG_ORDER);
841
842 pInst->enmState = ATSCLIENTSTATE_DESTROYING;
843
844 if ( pThis->pTransportInst
845 && pInst->pTransportClient)
846 {
847 if (pThis->pTransport->pfnNotifyBye)
848 pThis->pTransport->pfnNotifyBye(pThis->pTransportInst, pInst->pTransportClient);
849
850 pThis->pTransport->pfnDisconnect(pThis->pTransportInst, pInst->pTransportClient);
851 /* Pointer is now invalid due to the call above. */
852 pInst->pTransportClient = NULL;
853 }
854
855 return VINF_SUCCESS;
856}
857
858/**
859 * Free's (destroys) a client instance.
860 *
861 * @param pInst The opaque ATS instance structure.
862 */
863static void atsClientFree(PATSCLIENTINST pInst)
864{
865 if (!pInst)
866 return;
867
868 /* Make sure that there is no transport client associated with it anymore. */
869 AssertReturnVoid(pInst->enmState == ATSCLIENTSTATE_DESTROYING);
870 AssertReturnVoid(pInst->pTransportClient == NULL);
871
872 if (pInst->pszHostname)
873 {
874 RTStrFree(pInst->pszHostname);
875 pInst->pszHostname = NULL;
876 }
877
878 RTMemFree(pInst);
879 pInst = NULL;
880}
881
882/**
883 * The main thread worker serving the clients.
884 */
885static DECLCALLBACK(int) atsClientWorker(RTTHREAD hThread, void *pvUser)
886{
887 RT_NOREF(hThread);
888
889 PATSSERVER pThis = (PATSSERVER)pvUser;
890 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
891
892 unsigned cClientsMax = 0;
893 unsigned cClientsCur = 0;
894 PATSCLIENTINST *papInsts = NULL;
895
896 /* Add the pipe to the poll set. */
897 int rc = RTPollSetAddPipe(pThis->hPollSet, pThis->hPipeR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 0);
898 if (RT_SUCCESS(rc))
899 {
900 while (!pThis->fTerminate)
901 {
902 uint32_t fEvts;
903 uint32_t uId;
904 rc = RTPoll(pThis->hPollSet, RT_INDEFINITE_WAIT, &fEvts, &uId);
905 LogRelFlowFunc(("RTPoll(...) returned fEvts=#%x, uId=%RU32 -> %Rrc\n", fEvts, uId, rc));
906 if (RT_SUCCESS(rc))
907 {
908 if (uId == 0)
909 {
910 if (fEvts & RTPOLL_EVT_ERROR)
911 break;
912
913 /* We got woken up because of a new client. */
914 Assert(fEvts & RTPOLL_EVT_READ);
915
916 uint8_t bRead;
917 size_t cbRead = 0;
918 rc = RTPipeRead(pThis->hPipeR, &bRead, 1, &cbRead);
919 AssertRC(rc);
920
921 RTCritSectEnter(&pThis->CritSectClients);
922 /* Walk the list and add all new clients. */
923 PATSCLIENTINST pIt, pItNext;
924 RTListForEachSafe(&pThis->LstClientsNew, pIt, pItNext, ATSCLIENTINST, NdLst)
925 {
926 RTListNodeRemove(&pIt->NdLst);
927 Assert(cClientsCur <= cClientsMax);
928 if (cClientsCur == cClientsMax)
929 {
930 /* Realloc to accommodate for the new clients. */
931 PATSCLIENTINST *papInstsNew = (PATSCLIENTINST *)RTMemReallocZ(papInsts, cClientsMax * sizeof(PATSCLIENTINST), (cClientsMax + 10) * sizeof(PATSCLIENTINST));
932 if (RT_LIKELY(papInstsNew))
933 {
934 cClientsMax += 10;
935 papInsts = papInstsNew;
936 }
937 }
938 if (cClientsCur < cClientsMax)
939 {
940 /* Find a free slot in the client array. */
941 unsigned idxSlt = 0;
942 while ( idxSlt < cClientsMax
943 && papInsts[idxSlt] != NULL)
944 idxSlt++;
945
946 rc = pThis->pTransport->pfnPollSetAdd(pThis->pTransportInst, pThis->hPollSet, pIt->pTransportClient, idxSlt + 1);
947 if (RT_SUCCESS(rc))
948 {
949 cClientsCur++;
950 papInsts[idxSlt] = pIt;
951 }
952 else
953 {
954 atsClientDisconnect(pThis, pIt);
955 atsClientFree(pIt);
956 pIt = NULL;
957 }
958 }
959 else
960 {
961 atsClientDisconnect(pThis, pIt);
962 atsClientFree(pIt);
963 pIt = NULL;
964 }
965 }
966 RTCritSectLeave(&pThis->CritSectClients);
967 }
968 else
969 {
970 bool fDisconnect = false;
971
972 /* Client sends a request, pick the right client and process it. */
973 PATSCLIENTINST pInst = papInsts[uId - 1];
974 AssertPtr(pInst);
975 if (fEvts & RTPOLL_EVT_READ)
976 rc = atsClientReqProcess(pThis, pInst, &fDisconnect);
977
978 if ( (fEvts & RTPOLL_EVT_ERROR)
979 || RT_FAILURE(rc)
980 || fDisconnect)
981 {
982 /* Close connection and remove client from array. */
983 int rc2 = pThis->pTransport->pfnPollSetRemove(pThis->pTransportInst, pThis->hPollSet, pInst->pTransportClient, uId);
984 AssertRC(rc2);
985
986 atsClientDisconnect(pThis, pInst);
987 atsClientFree(pInst);
988 pInst = NULL;
989
990 papInsts[uId - 1] = NULL;
991 Assert(cClientsCur);
992 cClientsCur--;
993 }
994 }
995 }
996 }
997 }
998
999 if (papInsts)
1000 {
1001 for (size_t i = 0; i < cClientsMax; i++)
1002 RTMemFree(papInsts[i]);
1003 RTMemFree(papInsts);
1004 }
1005
1006 return rc;
1007}
1008
1009/**
1010 * The main thread waiting for new client connections.
1011 *
1012 * @returns VBox status code.
1013 */
1014static DECLCALLBACK(int) atsMainThread(RTTHREAD hThread, void *pvUser)
1015{
1016 RT_NOREF(hThread);
1017
1018 LogRelFlowFuncEnter();
1019
1020 PATSSERVER pThis = (PATSSERVER)pvUser;
1021 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1022
1023 int rc = RTThreadUserSignal(hThread);
1024 AssertRCReturn(rc, rc);
1025
1026 while (!pThis->fTerminate)
1027 {
1028 /*
1029 * Wait for new connection and spin off a new thread
1030 * for every new client.
1031 */
1032 bool fFromServer;
1033 PATSTRANSPORTCLIENT pTransportClient;
1034 rc = pThis->pTransport->pfnWaitForConnect(pThis->pTransportInst, 1000 /* msTimeout */, &fFromServer, &pTransportClient);
1035 if (RT_FAILURE(rc))
1036 continue;
1037
1038 /*
1039 * New connection, create new client structure and spin off
1040 * the request handling thread.
1041 */
1042 PATSCLIENTINST pInst = (PATSCLIENTINST)RTMemAllocZ(sizeof(ATSCLIENTINST));
1043 if (RT_LIKELY(pInst))
1044 {
1045 pInst->enmState = ATSCLIENTSTATE_INITIALISING;
1046 pInst->pTransportClient = pTransportClient;
1047 pInst->pszHostname = NULL;
1048
1049 /* Add client to the new list and inform the worker thread. */
1050 RTCritSectEnter(&pThis->CritSectClients);
1051 RTListAppend(&pThis->LstClientsNew, &pInst->NdLst);
1052 RTCritSectLeave(&pThis->CritSectClients);
1053
1054 size_t cbWritten = 0;
1055 rc = RTPipeWrite(pThis->hPipeW, "", 1, &cbWritten);
1056 if (RT_FAILURE(rc))
1057 LogRelFunc(("Failed to inform worker thread of a new client, rc=%Rrc\n", rc));
1058 }
1059 else
1060 {
1061 LogRelFunc(("Creating new client structure failed with out of memory error\n"));
1062 pThis->pTransport->pfnNotifyBye(pThis->pTransportInst, pTransportClient);
1063 rc = VERR_NO_MEMORY;
1064 break; /* This is fatal, break out of the loop. */
1065 }
1066
1067 if (RT_SUCCESS(rc))
1068 {
1069 LogRelFunc(("New connection established (%s)\n", fFromServer ? "from server" : "as client"));
1070
1071 /**
1072 * If the new client is not from our server but from a remote server (also called a reverse connection),
1073 * exit this loop and stop trying to connect to the remote server.
1074 *
1075 * Otherwise we would connect lots and lots of clients without any real use.
1076 *
1077 ** @todo Improve this handling -- there might be a better / more elegant solution.
1078 */
1079 if (!fFromServer)
1080 break;
1081 }
1082 }
1083
1084 LogRelFlowFuncLeaveRC(rc);
1085 return rc;
1086}
1087
1088/**
1089 * Initializes an ATS instance.
1090 *
1091 * @note This does *not* start the server!
1092 *
1093 * @returns VBox status code.
1094 * @param pThis The ATS instance.
1095 * @param pCallbacks The callbacks table to use.
1096 */
1097int AudioTestSvcInit(PATSSERVER pThis, PCATSCALLBACKS pCallbacks)
1098{
1099 LogRelFlowFuncEnter();
1100
1101 RT_BZERO(pThis, sizeof(ATSSERVER));
1102
1103 pThis->hPipeR = NIL_RTPIPE;
1104 pThis->hPipeW = NIL_RTPIPE;
1105
1106 RTListInit(&pThis->LstClientsNew);
1107
1108 /* Copy callback table. */
1109 memcpy(&pThis->Callbacks, pCallbacks, sizeof(ATSCALLBACKS));
1110
1111 int rc = RTCritSectInit(&pThis->CritSectClients);
1112 if (RT_SUCCESS(rc))
1113 {
1114 rc = RTPollSetCreate(&pThis->hPollSet);
1115 if (RT_SUCCESS(rc))
1116 {
1117 rc = RTPipeCreate(&pThis->hPipeR, &pThis->hPipeW, 0);
1118 if (RT_SUCCESS(rc))
1119 {
1120 /*
1121 * The default transporter is the first one.
1122 */
1123 pThis->pTransport = g_apTransports[0]; /** @todo Make this dynamic. */
1124
1125 rc = pThis->pTransport->pfnCreate(&pThis->pTransportInst);
1126 if (RT_SUCCESS(rc))
1127 return VINF_SUCCESS;
1128
1129 RTPipeClose(pThis->hPipeR);
1130 RTPipeClose(pThis->hPipeW);
1131 }
1132 else
1133 LogRel(("Creating communications pipe failed with %Rrc\n", rc));
1134
1135 RTPollSetDestroy(pThis->hPollSet);
1136 }
1137 else
1138 LogRel(("Creating pollset failed with %Rrc\n", rc));
1139
1140 RTCritSectDelete(&pThis->CritSectClients);
1141 }
1142 else
1143 LogRel(("Creating critical section failed with %Rrc\n", rc));
1144
1145 if (RT_FAILURE(rc))
1146 LogRel(("Creating server failed with %Rrc\n", rc));
1147
1148 LogRelFlowFuncLeaveRC(rc);
1149 return rc;
1150}
1151
1152/**
1153 * Handles a command line option.
1154 *
1155 * @returns VBox status code.
1156 * @param pThis The ATS instance to handle option for.
1157 * @param ch Option (short) to handle.
1158 * @param pVal Option union to store the result in on success.
1159 */
1160int AudioTestSvcHandleOption(PATSSERVER pThis, int ch, PCRTGETOPTUNION pVal)
1161{
1162 AssertPtrReturn(pThis->pTransport, VERR_WRONG_ORDER); /* Must be creatd first. */
1163 if (!pThis->pTransport->pfnOption)
1164 return VERR_GETOPT_UNKNOWN_OPTION;
1165 return pThis->pTransport->pfnOption(pThis->pTransportInst, ch, pVal);
1166}
1167
1168/**
1169 * Starts a formerly initialized ATS instance.
1170 *
1171 * @returns VBox status code.
1172 * @param pThis The ATS instance to start.
1173 */
1174int AudioTestSvcStart(PATSSERVER pThis)
1175{
1176 LogRelFlowFuncEnter();
1177
1178 /* Spin off the thread serving connections. */
1179 int rc = RTThreadCreate(&pThis->hThreadServing, atsClientWorker, pThis, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE,
1180 "ATSCLWORK");
1181 if (RT_FAILURE(rc))
1182 {
1183 LogRel(("Creating the client worker thread failed with %Rrc\n", rc));
1184 return rc;
1185 }
1186
1187 rc = pThis->pTransport->pfnStart(pThis->pTransportInst);
1188 if (RT_SUCCESS(rc))
1189 {
1190 /* Spin off the connection thread. */
1191 rc = RTThreadCreate(&pThis->hThreadMain, atsMainThread, pThis, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE,
1192 "ATSMAIN");
1193 if (RT_SUCCESS(rc))
1194 {
1195 rc = RTThreadUserWait(pThis->hThreadMain, RT_MS_30SEC);
1196 if (RT_SUCCESS(rc))
1197 pThis->fStarted = true;
1198 }
1199 }
1200
1201 LogRelFlowFuncLeaveRC(rc);
1202 return rc;
1203}
1204
1205/**
1206 * Stops (shuts down) a formerly started ATS instance.
1207 *
1208 * @returns VBox status code.
1209 * @param pThis The ATS instance.
1210 */
1211int AudioTestSvcStop(PATSSERVER pThis)
1212{
1213 if (!pThis->fStarted)
1214 return VINF_SUCCESS;
1215
1216 LogRelFlowFuncEnter();
1217
1218 ASMAtomicXchgBool(&pThis->fTerminate, true);
1219
1220 if (pThis->pTransport)
1221 pThis->pTransport->pfnStop(pThis->pTransportInst);
1222
1223 size_t cbWritten;
1224 int rc = RTPipeWrite(pThis->hPipeW, "", 1, &cbWritten);
1225 AssertRCReturn(rc, rc);
1226
1227 /* First close serving thread. */
1228 int rcThread;
1229 rc = RTThreadWait(pThis->hThreadServing, RT_MS_30SEC, &rcThread);
1230 if (RT_SUCCESS(rc))
1231 {
1232 rc = rcThread;
1233 if (RT_SUCCESS(rc))
1234 {
1235 /* Close the main thread last. */
1236 rc = RTThreadWait(pThis->hThreadMain, RT_MS_30SEC, &rcThread);
1237 if (RT_SUCCESS(rc))
1238 rc = rcThread;
1239
1240 if (rc == VERR_TCP_SERVER_DESTROYED)
1241 rc = VINF_SUCCESS;
1242 }
1243 }
1244
1245 if (RT_SUCCESS(rc))
1246 pThis->fStarted = false;
1247
1248 LogRelFlowFuncLeaveRC(rc);
1249 return rc;
1250}
1251
1252/**
1253 * Destroys an ATS instance, internal version.
1254 *
1255 * @returns VBox status code.
1256 * @param pThis ATS instance to destroy.
1257 */
1258static int audioTestSvcDestroyInternal(PATSSERVER pThis)
1259{
1260 int rc = VINF_SUCCESS;
1261
1262 if (pThis->hPipeR != NIL_RTPIPE)
1263 {
1264 rc = RTPipeClose(pThis->hPipeR);
1265 AssertRCReturn(rc, rc);
1266 pThis->hPipeR = NIL_RTPIPE;
1267 }
1268
1269 if (pThis->hPipeW != NIL_RTPIPE)
1270 {
1271 rc = RTPipeClose(pThis->hPipeW);
1272 AssertRCReturn(rc, rc);
1273 pThis->hPipeW = NIL_RTPIPE;
1274 }
1275
1276 RTPollSetDestroy(pThis->hPollSet);
1277 pThis->hPollSet = NIL_RTPOLLSET;
1278
1279 PATSCLIENTINST pIt, pItNext;
1280 RTListForEachSafe(&pThis->LstClientsNew, pIt, pItNext, ATSCLIENTINST, NdLst)
1281 {
1282 RTListNodeRemove(&pIt->NdLst);
1283 atsClientDisconnect(pThis, pIt);
1284 atsClientFree(pIt);
1285 }
1286
1287 if (RTCritSectIsInitialized(&pThis->CritSectClients))
1288 {
1289 rc = RTCritSectDelete(&pThis->CritSectClients);
1290 AssertRCReturn(rc, rc);
1291 }
1292
1293 return rc;
1294}
1295
1296/**
1297 * Destroys an ATS instance.
1298 *
1299 * @returns VBox status code.
1300 * @param pThis ATS instance to destroy.
1301 */
1302int AudioTestSvcDestroy(PATSSERVER pThis)
1303{
1304 LogRelFlowFuncEnter();
1305
1306 int rc = audioTestSvcDestroyInternal(pThis);
1307 if (RT_SUCCESS(rc))
1308 {
1309 if (pThis->pTransport)
1310 {
1311 if ( pThis->pTransport->pfnDestroy
1312 && pThis->pTransportInst)
1313 {
1314 pThis->pTransport->pfnDestroy(pThis->pTransportInst);
1315 pThis->pTransportInst = NULL;
1316 }
1317 }
1318 }
1319
1320 LogRelFlowFuncLeaveRC(rc);
1321 return rc;
1322}
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