-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ds-ipc.c
897 lines (753 loc) · 24.4 KB
/
ds-ipc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
#include "ds-rt-config.h"
#ifdef ENABLE_PERFTRACING
#if !defined(DS_INCLUDE_SOURCE_FILES) || defined(DS_FORCE_INCLUDE_SOURCE_FILES)
#define DS_IMPL_IPC_GETTER_SETTER
#define DS_IMPL_IPC_PAL_GETTER_SETTER
#include "ds-ipc.h"
#include "ds-protocol.h"
#include "ep.h"
#include "ds-rt.h"
/*
* Globals and volatile access functions.
*/
static volatile uint32_t _ds_shutting_down_state = 0;
static dn_vector_ptr_t *_ds_port_array = NULL;
// set this in get_next_available_stream, and then expose a callback that
// allows us to track which connections have sent their ResumeRuntime commands
static DiagnosticsPort *_ds_current_port = NULL;
static const uint32_t _ds_default_poll_handle_array_size = 16;
#define NUM_NANOSECONDS_IN_1_MS 1000000
static
inline
bool
load_shutting_down_state (void)
{
return (ep_rt_volatile_load_uint32_t (&_ds_shutting_down_state) != 0) ? true : false;
}
static
inline
void
store_shutting_down_state (bool state)
{
ep_rt_volatile_store_uint32_t (&_ds_shutting_down_state, state ? 1 : 0);
}
/*
* Forward declares of all static functions.
*/
static
uint32_t
ipc_stream_factory_get_next_timeout (uint32_t current_timeout_ms);
static
void
ipc_stream_factory_split_port_config (
ep_char8_t *config,
const ep_char8_t *delimiters,
dn_vector_ptr_t *config_array);
static
bool
ipc_stream_factory_build_and_add_port (
DiagnosticsPortBuilder *builder,
ds_ipc_error_callback_func callback,
bool default_port);
static
void
ipc_log_poll_handles (dn_vector_t *ipc_poll_handles);
static
void
connect_port_free_func (void *object);
static
bool
connect_port_get_ipc_poll_handle_func (
void *object,
DiagnosticsIpcPollHandle *handle,
ds_ipc_error_callback_func callback);
static
DiagnosticsIpcStream *
connect_port_get_connected_stream_func (
void *object,
ds_ipc_error_callback_func callback);
static
void
connect_port_reset (
void *object,
ds_ipc_error_callback_func callback);
static
void
listen_port_free_func (void *object);
static
bool
listen_port_get_ipc_poll_handle_func (
void *object,
DiagnosticsIpcPollHandle *handle,
ds_ipc_error_callback_func callback);
static
DiagnosticsIpcStream *
listen_port_get_connected_stream_func (
void *object,
ds_ipc_error_callback_func callback);
static
void
listen_port_reset (
void *object,
ds_ipc_error_callback_func callback);
/*
* IpcStreamFactory.
*/
static
inline
uint32_t
ipc_stream_factory_get_next_timeout (uint32_t current_timeout_ms)
{
if (current_timeout_ms == DS_IPC_TIMEOUT_INFINITE)
return DS_IPC_POLL_TIMEOUT_MIN_MS;
else
return (current_timeout_ms >= DS_IPC_POLL_TIMEOUT_MAX_MS) ?
DS_IPC_POLL_TIMEOUT_MAX_MS :
(uint32_t)((float)current_timeout_ms * DS_IPC_POLL_TIMEOUT_FALLOFF_FACTOR);
}
static
void
ipc_stream_factory_split_port_config (
ep_char8_t *config,
const ep_char8_t *delimiters,
dn_vector_ptr_t *config_array)
{
ep_char8_t *part = NULL;
ep_char8_t *context = NULL;
ep_char8_t *cursor = config;
EP_ASSERT (config != NULL);
EP_ASSERT (delimiters != NULL);
EP_ASSERT (config_array != NULL);
part = ep_rt_utf8_string_strtok (cursor, delimiters, &context);
while (part) {
dn_vector_ptr_push_back (config_array, part);
part = ep_rt_utf8_string_strtok (NULL, delimiters, &context);
}
}
static
bool
ipc_stream_factory_build_and_add_port (
DiagnosticsPortBuilder *builder,
ds_ipc_error_callback_func callback,
bool default_port)
{
EP_ASSERT (builder != NULL);
EP_ASSERT (callback != NULL);
bool result = false;
DiagnosticsIpc *ipc = NULL;
#ifndef DS_IPC_DISABLE_DEFAULT_LISTEN_PORT
if (!default_port && builder->type == DS_PORT_TYPE_LISTEN) {
// Ignore listen type (see conversation in https://github.com/dotnet/runtime/pull/40499 for details)
DS_LOG_INFO_0 ("ipc_stream_factory_build_and_add_port - Ignoring LISTEN port configuration");
return true;
}
#endif
if (builder->type == DS_PORT_TYPE_LISTEN) {
#ifndef DS_IPC_DISABLE_LISTEN_PORTS
ipc = ds_ipc_alloc (builder->path, DS_IPC_CONNECTION_MODE_LISTEN, callback);
ep_raise_error_if_nok (ipc != NULL);
ep_raise_error_if_nok (ds_ipc_listen (ipc, callback));
ep_raise_error_if_nok (dn_vector_ptr_push_back (_ds_port_array, (DiagnosticsPort *)ds_listen_port_alloc (ipc, builder)));
#else
DS_LOG_INFO_0 ("ipc_stream_factory_build_and_add_port - LISTEN ports disabled");
ep_raise_error ();
#endif
} else if (builder->type == DS_PORT_TYPE_CONNECT) {
#ifndef DS_IPC_DISABLE_CONNECT_PORTS
ipc = ds_ipc_alloc (builder->path, DS_IPC_CONNECTION_MODE_CONNECT, callback);
ep_raise_error_if_nok (ipc != NULL);
ep_raise_error_if_nok (dn_vector_ptr_push_back (_ds_port_array, (DiagnosticsPort *)ds_connect_port_alloc (ipc, builder)));
#else
DS_LOG_INFO_0 ("ipc_stream_factory_build_and_add_port - CONNECT ports disabled");
ep_raise_error ();
#endif
}
result = true;
ep_on_exit:
return result;
ep_on_error:
EP_ASSERT (!result);
ds_ipc_free (ipc);
ep_exit_error_handler ();
}
static
void
ipc_log_poll_handles (dn_vector_t *ipc_poll_handles)
{
// TODO: Should this be debug only?
ep_char8_t buffer [DS_IPC_MAX_TO_STRING_LEN];
uint32_t connection_id = 0;
EP_ASSERT (ipc_poll_handles != NULL);
DN_VECTOR_FOREACH_BEGIN (DiagnosticsIpcPollHandle, ipc_poll_handle, ipc_poll_handles) {
if (ipc_poll_handle.ipc) {
if (!(ds_ipc_to_string (ipc_poll_handle.ipc, buffer, (uint32_t)ARRAY_SIZE (buffer)) > 0))
buffer [0] = '\0';
DS_LOG_DEBUG_2 ("\tSERVER IpcPollHandle[%d] = %s", connection_id, buffer);
} else {
if (!(ds_ipc_stream_to_string (ipc_poll_handle.stream, buffer, (uint32_t)ARRAY_SIZE (buffer))))
buffer [0] = '\0';
DS_LOG_DEBUG_2 ("\tCLIENT IpcPollHandle[%d] = %s", connection_id, buffer);
}
connection_id++;
} DN_VECTOR_FOREACH_END;
}
static
bool
EP_CALLBACK_CALLTYPE
ipc_stream_factory_callback (void)
{
return ds_ipc_stream_factory_any_suspended_ports ();
}
bool
ds_ipc_stream_factory_init (void)
{
ep_ipc_stream_factory_callback_set (ipc_stream_factory_callback);
_ds_port_array = dn_vector_ptr_alloc ();
return _ds_port_array != NULL;
}
void
ds_ipc_stream_factory_fini (void)
{
// TODO: Race between server thread and shutdown, _ds_port_array and ports can not be freed without resolving
// that race first. Diagnostic server thread is currently designed to not break waits on
// shutdown unless clients activity wakes server thread.
/*DN_VECTOR_FOREACH_BEGIN (DiagnosticsPort *, port, _ds_port_array) {
ds_port_free_vcall (port);
} DN_PTR_ARRAY_EX_FOREACH_END;
dn_vector_ptr_free (_ds_port_array);
_ds_port_array = NULL;*/
ep_ipc_stream_factory_callback_set (NULL);
}
bool
ds_ipc_stream_factory_configure (ds_ipc_error_callback_func callback)
{
bool result = true;
ep_char8_t *ports = ds_rt_config_value_get_ports ();
if (ports) {
DN_DEFAULT_LOCAL_ALLOCATOR (allocator, dn_vector_ptr_default_local_allocator_byte_size * 2);
dn_vector_ptr_custom_alloc_params_t params = {0, };
params.allocator = (dn_allocator_t *)&allocator;
params.capacity = dn_vector_ptr_default_local_allocator_capacity_size;
dn_vector_ptr_t *port_configs = dn_vector_ptr_custom_alloc (¶ms);
dn_vector_ptr_t *port_config_parts = dn_vector_ptr_custom_alloc (¶ms);
if (port_configs && port_config_parts) {
ipc_stream_factory_split_port_config (ports, ";", port_configs);
DN_VECTOR_PTR_FOREACH_RBEGIN (ep_char8_t *, port_config, port_configs) {
DS_LOG_INFO_1 ("ds_ipc_stream_factory_configure - Attempted to create Diagnostic Port from \"%s\".", port_config ? port_config : "");
if (port_config) {
dn_vector_ptr_clear (port_config_parts);
ipc_stream_factory_split_port_config (port_config, ",", port_config_parts);
uint32_t port_config_parts_index = dn_vector_ptr_size (port_config_parts);
if (port_config_parts_index != 0) {
DiagnosticsPortBuilder port_builder;
if (ds_port_builder_init (&port_builder)) {
DN_VECTOR_PTR_FOREACH_RBEGIN (ep_char8_t *, port_config_part, port_config_parts) {
if (port_config_parts_index == 1)
port_builder.path = port_config_part;
else
ds_port_builder_set_tag (&port_builder, port_config_part);
port_config_parts_index--;
} DN_VECTOR_PTR_FOREACH_END;
if (!ep_rt_utf8_string_is_null_or_empty (port_builder.path)) {
const bool build_success = ipc_stream_factory_build_and_add_port (&port_builder, callback, false);
DS_LOG_INFO_1 ("ds_ipc_stream_factory_configure - Diagnostic Port creation %s", build_success ? "succeeded" : "failed");
result &= build_success;
} else {
DS_LOG_INFO_0("ds_ipc_stream_factory_configure - Ignoring port configuration with empty address");
}
ds_port_builder_fini (&port_builder);
} else {
result &= false;
}
} else {
result &= false;
}
}
} DN_VECTOR_PTR_FOREACH_END;
} else {
result &= false;
}
dn_vector_ptr_free (port_config_parts);
dn_vector_ptr_free (port_configs);
ep_rt_utf8_string_free (ports);
}
#ifndef DS_IPC_DISABLE_DEFAULT_LISTEN_PORT
// create the default listen port
uint32_t port_suspend = ds_rt_config_value_get_default_port_suspend ();
DiagnosticsPortBuilder default_port_builder;
if (ds_port_builder_init (&default_port_builder)) {
default_port_builder.path = NULL;
default_port_builder.suspend_mode = port_suspend > 0 ? DS_PORT_SUSPEND_MODE_SUSPEND : DS_PORT_SUSPEND_MODE_NOSUSPEND;
default_port_builder.type = DS_PORT_TYPE_LISTEN;
result &= ipc_stream_factory_build_and_add_port (&default_port_builder, callback, true);
ds_port_builder_fini (&default_port_builder);
} else {
result &= false;
}
#else
DS_LOG_DEBUG_0 ("ds_ipc_stream_factory_configure - Ignoring default LISTEN port");
#endif
return result;
}
// Polling timeout semantics
// If client connection is opted in
// and connection succeeds => set timeout to infinite
// and connection fails => set timeout to minimum and scale by falloff factor
// else => set timeout to (uint32_t)-1 (infinite)
//
// If an agent closes its socket while we're still connected,
// Poll will return and let us know which connection hung up
DiagnosticsIpcStream *
ds_ipc_stream_factory_get_next_available_stream (ds_ipc_error_callback_func callback)
{
DS_LOG_DEBUG_0 ("ds_ipc_stream_factory_get_next_available_stream - ENTER");
DiagnosticsIpcStream *stream = NULL;
uint32_t poll_timeout_ms = DS_IPC_TIMEOUT_INFINITE;
bool connect_success = true;
uint32_t poll_attempts = 0;
dn_vector_custom_alloc_params_t params = {0, };
params.capacity = _ds_default_poll_handle_array_size;
dn_vector_t ipc_poll_handles;
ep_raise_error_if_nok (dn_vector_custom_init_t (&ipc_poll_handles, ¶ms, DiagnosticsIpcPollHandle));
while (!stream) {
connect_success = true;
DN_VECTOR_PTR_FOREACH_BEGIN (DiagnosticsPort *, port, _ds_port_array) {
DiagnosticsIpcPollHandle ipc_poll_handle;
if (ds_port_get_ipc_poll_handle_vcall (port, &ipc_poll_handle, callback))
ep_raise_error_if_nok (dn_vector_push_back (&ipc_poll_handles, ipc_poll_handle));
else
connect_success = false;
} DN_VECTOR_PTR_FOREACH_END;
poll_timeout_ms = connect_success ?
DS_IPC_TIMEOUT_INFINITE :
ipc_stream_factory_get_next_timeout (poll_timeout_ms);
int32_t ret_val;
if (dn_vector_size (&ipc_poll_handles) > 0) {
poll_attempts++;
DS_LOG_DEBUG_2 ("ds_ipc_stream_factory_get_next_available_stream - Poll attempt: %d, timeout: %dms.", poll_attempts, poll_timeout_ms);
ipc_log_poll_handles (&ipc_poll_handles);
ret_val = ds_ipc_poll (dn_vector_data_t (&ipc_poll_handles, DiagnosticsIpcPollHandle), dn_vector_size (&ipc_poll_handles), poll_timeout_ms, callback);
} else {
if (poll_timeout_ms == DS_IPC_TIMEOUT_INFINITE)
poll_timeout_ms = DS_IPC_POLL_TIMEOUT_MAX_MS;
DS_LOG_DEBUG_1 ("ds_ipc_stream_factory_get_next_available_stream - Nothing to poll, sleeping using timeout: %dms.", poll_timeout_ms);
ep_rt_thread_sleep ((uint64_t)poll_timeout_ms * NUM_NANOSECONDS_IN_1_MS);
ret_val = 0; // timeout
}
bool saw_error = false;
if (ret_val != 0) {
uint32_t connection_id = 0;
DN_VECTOR_FOREACH_BEGIN (DiagnosticsIpcPollHandle, ipc_poll_handle, &ipc_poll_handles) {
DiagnosticsPort *port = (DiagnosticsPort *)ipc_poll_handle.user_data;
switch (ipc_poll_handle.events) {
case DS_IPC_POLL_EVENTS_HANGUP:
EP_ASSERT (port != NULL);
ds_port_reset_vcall (port, callback);
DS_LOG_INFO_2 ("ds_ipc_stream_factory_get_next_available_stream - HUP :: Poll attempt: %d, connection %d hung up. Connect is reset.", poll_attempts, connection_id);
poll_timeout_ms = DS_IPC_POLL_TIMEOUT_MIN_MS;
break;
case DS_IPC_POLL_EVENTS_SIGNALED:
EP_ASSERT (port != NULL);
if (!stream) { // only use first signaled stream; will get others on subsequent calls
stream = ds_port_get_connected_stream_vcall (port, callback);
if (!stream)
saw_error = true;
_ds_current_port = port;
}
DS_LOG_DEBUG_2 ("ds_ipc_stream_factory_get_next_available_stream - SIG :: Poll attempt: %d, connection %d signalled.", poll_attempts, connection_id);
break;
case DS_IPC_POLL_EVENTS_ERR:
ds_port_reset_vcall ((DiagnosticsPort *)ipc_poll_handle.user_data, callback);
DS_LOG_INFO_2 ("ds_ipc_stream_factory_get_next_available_stream - ERR :: Poll attempt: %d, connection %d errored. Connection is reset.", poll_attempts, connection_id);
saw_error = true;
break;
case DS_IPC_POLL_EVENTS_NONE:
DS_LOG_INFO_2 ("ds_ipc_stream_factory_get_next_available_stream - NON :: Poll attempt: %d, connection %d had no events.", poll_attempts, connection_id);
break;
default:
ds_port_reset_vcall ((DiagnosticsPort *)ipc_poll_handle.user_data, callback);
DS_LOG_INFO_2 ("ds_ipc_stream_factory_get_next_available_stream - UNK :: Poll attempt: %d, connection %d had invalid PollEvent.", poll_attempts, connection_id);
saw_error = true;
break;
}
connection_id++;
} DN_VECTOR_FOREACH_END;
}
if (!stream && saw_error) {
// Some errors can cause the poll to return instantly, we want to delay if we see an error to avoid
// runaway CPU usage.
if (poll_timeout_ms == DS_IPC_TIMEOUT_INFINITE)
poll_timeout_ms = DS_IPC_POLL_TIMEOUT_MAX_MS;
DS_LOG_DEBUG_1 ("ds_ipc_stream_factory_get_next_available_stream - Saw error, sleeping using timeout: %dms.", poll_timeout_ms);
ep_rt_thread_sleep ((uint64_t)poll_timeout_ms * NUM_NANOSECONDS_IN_1_MS);
_ds_current_port = NULL;
ep_raise_error ();
}
// clear the view.
dn_vector_clear (&ipc_poll_handles);
}
ep_on_exit:
DS_LOG_DEBUG_2 ("ds_ipc_stream_factory_get_next_available_stream - EXIT :: Poll attempt: %d, stream using handle %d.", poll_attempts, stream ? ds_ipc_stream_get_handle_int32_t (stream) : -1);
dn_vector_dispose (&ipc_poll_handles);
return stream;
ep_on_error:
stream = NULL;
ep_exit_error_handler ();
}
void
ds_ipc_stream_factory_resume_current_port (void)
{
if (_ds_current_port != NULL)
_ds_current_port->has_resumed_runtime = true;
}
bool
ds_ipc_stream_factory_any_suspended_ports (void)
{
bool any_suspended_ports = false;
DN_VECTOR_PTR_FOREACH_BEGIN (DiagnosticsPort *, port, _ds_port_array) {
any_suspended_ports |= !(port->suspend_mode == DS_PORT_SUSPEND_MODE_NOSUSPEND || port->has_resumed_runtime);
} DN_VECTOR_PTR_FOREACH_END;
return any_suspended_ports;
}
bool
ds_ipc_stream_factory_has_active_ports (void)
{
return !load_shutting_down_state () &&
dn_vector_ptr_size (_ds_port_array) > 0;
}
void
ds_ipc_stream_factory_close_ports (ds_ipc_error_callback_func callback)
{
DN_VECTOR_PTR_FOREACH_BEGIN (DiagnosticsPort *, port, _ds_port_array) {
ds_port_close (port, false, callback);
} DN_VECTOR_PTR_FOREACH_END;
}
bool
ds_ipc_stream_factory_shutdown (ds_ipc_error_callback_func callback)
{
if (load_shutting_down_state ())
return true;
store_shutting_down_state (true);
DN_VECTOR_PTR_FOREACH_BEGIN (DiagnosticsPort *, port, _ds_port_array) {
ds_port_close (port, true, callback);
} DN_VECTOR_PTR_FOREACH_END;
_ds_current_port = NULL;
return true;
}
/*
* DiagnosticsPort.
*/
DiagnosticsPort *
ds_port_init (
DiagnosticsPort *port,
DiagnosticsPortVtable *vtable,
DiagnosticsIpc *ipc,
DiagnosticsPortBuilder *builder)
{
EP_ASSERT (port != NULL);
EP_ASSERT (vtable != NULL);
EP_ASSERT (ipc != NULL);
EP_ASSERT (builder != NULL);
port->vtable = vtable;
port->suspend_mode = builder->suspend_mode;
port->type = builder->type;
port->ipc = ipc;
port->stream = NULL;
port->has_resumed_runtime = false;
return port;
}
void
ds_port_fini (DiagnosticsPort *port)
{
return;
}
void
ds_port_free_vcall (DiagnosticsPort *port)
{
ep_return_void_if_nok (port != NULL);
EP_ASSERT (port->vtable != NULL);
DiagnosticsPortVtable *vtable = port->vtable;
EP_ASSERT (vtable->free_func != NULL);
vtable->free_func (port);
}
bool
ds_port_get_ipc_poll_handle_vcall (
DiagnosticsPort *port,
DiagnosticsIpcPollHandle *handle,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (port != NULL);
EP_ASSERT (port->vtable != NULL);
DiagnosticsPortVtable *vtable = port->vtable;
EP_ASSERT (vtable->get_ipc_poll_handle_func != NULL);
return vtable->get_ipc_poll_handle_func (port, handle, callback);
}
DiagnosticsIpcStream *
ds_port_get_connected_stream_vcall (
DiagnosticsPort *port,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (port != NULL);
EP_ASSERT (port->vtable != NULL);
DiagnosticsPortVtable *vtable = port->vtable;
EP_ASSERT (vtable->get_connected_stream_func != NULL);
return vtable->get_connected_stream_func (port, callback);
}
void
ds_port_reset_vcall (
DiagnosticsPort *port,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (port != NULL);
EP_ASSERT (port->vtable != NULL);
DiagnosticsPortVtable *vtable = port->vtable;
EP_ASSERT (vtable->reset_func != NULL);
vtable->reset_func (port, callback);
}
void
ds_port_close (
DiagnosticsPort *port,
bool is_shutdown,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (port != NULL);
if (port->ipc)
ds_ipc_close (port->ipc, is_shutdown, callback);
if (port->stream && !is_shutdown)
ds_ipc_stream_close (port->stream, callback);
}
/*
* DiagnosticsPortBuilder.
*/
DiagnosticsPortBuilder *
ds_port_builder_init (DiagnosticsPortBuilder *builder)
{
EP_ASSERT (builder != NULL);
builder->path = NULL;
builder->suspend_mode = DS_PORT_SUSPEND_MODE_SUSPEND;
builder->type = DS_PORT_TYPE_CONNECT;
return builder;
}
void
ds_port_builder_fini (DiagnosticsPortBuilder *builder)
{
return;
}
void
ds_port_builder_set_tag (
DiagnosticsPortBuilder *builder,
ep_char8_t *tag)
{
EP_ASSERT (builder != NULL);
EP_ASSERT (tag != NULL);
if (ep_rt_utf8_string_compare_ignore_case (tag, "listen") == 0)
builder->type = DS_PORT_TYPE_LISTEN;
else if (ep_rt_utf8_string_compare_ignore_case (tag, "connect") == 0)
builder->type = DS_PORT_TYPE_CONNECT;
else if (ep_rt_utf8_string_compare_ignore_case (tag, "nosuspend") == 0)
builder->suspend_mode = DS_PORT_SUSPEND_MODE_NOSUSPEND;
else if (ep_rt_utf8_string_compare_ignore_case (tag, "suspend") == 0)
builder->suspend_mode = DS_PORT_SUSPEND_MODE_SUSPEND;
else
// don't mutate if it's not a valid option
DS_LOG_INFO_1 ("ds_port_builder_set_tag - Unknown tag '%s'.", tag);
}
/*
* DiagnosticsConnectPort.
*/
static
void
connect_port_free_func (void *object)
{
EP_ASSERT (object != NULL);
ds_connect_port_free ((DiagnosticsConnectPort *)object);
}
static
bool
connect_port_get_ipc_poll_handle_func (
void *object,
DiagnosticsIpcPollHandle *handle,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (object != NULL);
EP_ASSERT (handle != NULL);
bool success = false;
DiagnosticsConnectPort *connect_port = (DiagnosticsConnectPort *)object;
DiagnosticsIpcStream *connection = NULL;
DS_LOG_DEBUG_0 ("connect_port_get_ipc_poll_handle - ENTER.");
if (!connect_port->port.stream) {
DS_LOG_DEBUG_0 ("connect_port_get_ipc_poll_handle - cache was empty, trying to reconnect!");
// cache is empty, reconnect, e.g., there was a disconnect
bool timed_out = false;
connection = ds_ipc_connect (connect_port->port.ipc, 100 /*ms*/, callback, &timed_out);
if (!connection) {
if (callback && !timed_out)
callback("Failed to connect to client connection", -1);
else if (timed_out)
DS_LOG_DEBUG_0 ("connect_port_get_ipc_poll_handle - reconnect attempt timed out.");
ep_raise_error ();
}
ep_char8_t buffer [DS_IPC_MAX_TO_STRING_LEN];
if (!(ds_ipc_stream_to_string (connection, buffer, (uint32_t)ARRAY_SIZE (buffer))))
buffer [0] = '\0';
DS_LOG_DEBUG_1 ("connect_port_get_ipc_poll_handle - returned connection %s", buffer);
if (!ds_icp_advertise_v1_send (connection)) {
if (callback)
callback("Failed to send advertise message", -1);
ep_raise_error ();
}
//Transfer ownership.
connect_port->port.stream = connection;
connection = NULL;
}
handle->ipc = NULL;
handle->stream = connect_port->port.stream;
handle->events = 0;
handle->user_data = object;
success = true;
ep_on_exit:
DS_LOG_DEBUG_0 ("connect_port_get_ipc_poll_handle - EXIT.");
return success;
ep_on_error:
ds_ipc_stream_free (connection);
success = false;
ep_exit_error_handler ();
}
static
DiagnosticsIpcStream *
connect_port_get_connected_stream_func (
void *object,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (object != NULL);
DiagnosticsConnectPort *connect_port = (DiagnosticsConnectPort *)object;
DiagnosticsIpcStream *stream = connect_port->port.stream;
connect_port->port.stream = NULL;
return stream;
}
static
void
connect_port_reset (
void *object,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (object != NULL);
DiagnosticsConnectPort *connect_port = (DiagnosticsConnectPort *)object;
ds_ipc_stream_free (connect_port->port.stream);
connect_port->port.stream = NULL;
}
static DiagnosticsPortVtable connect_port_vtable = {
connect_port_free_func,
connect_port_get_ipc_poll_handle_func,
connect_port_get_connected_stream_func,
connect_port_reset };
DiagnosticsConnectPort *
ds_connect_port_alloc (
DiagnosticsIpc *ipc,
DiagnosticsPortBuilder *builder)
{
DiagnosticsConnectPort * instance = ep_rt_object_alloc (DiagnosticsConnectPort);
ep_raise_error_if_nok (instance != NULL);
ep_raise_error_if_nok (ds_port_init (
(DiagnosticsPort *)instance,
&connect_port_vtable,
ipc,
builder) != NULL);
ep_on_exit:
return instance;
ep_on_error:
ds_connect_port_free (instance);
instance = NULL;
ep_exit_error_handler ();
}
void
ds_connect_port_free (DiagnosticsConnectPort *connect_port)
{
ep_return_void_if_nok (connect_port != NULL);
ds_port_fini (&connect_port->port);
ep_rt_object_free (connect_port);
}
/*
* DiagnosticsListenPort.
*/
static
void
listen_port_free_func (void *object)
{
EP_ASSERT (object != NULL);
ds_listen_port_free ((DiagnosticsListenPort *)object);
}
static
bool
listen_port_get_ipc_poll_handle_func (
void *object,
DiagnosticsIpcPollHandle *handle,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (object != NULL);
EP_ASSERT (handle != NULL);
DiagnosticsListenPort *listen_port = (DiagnosticsListenPort *)object;
handle->ipc = listen_port->port.ipc;
handle->stream = NULL;
handle->events = 0;
handle->user_data = object;
return true;
}
static
DiagnosticsIpcStream *
listen_port_get_connected_stream_func (
void *object,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (object != NULL);
DiagnosticsListenPort *listen_port = (DiagnosticsListenPort *)object;
return ds_ipc_accept (listen_port->port.ipc, callback);
}
static
void
listen_port_reset (
void *object,
ds_ipc_error_callback_func callback)
{
EP_ASSERT (object != NULL);
DiagnosticsListenPort *listen_port = (DiagnosticsListenPort *)object;
ds_ipc_reset (listen_port->port.ipc);
ds_ipc_listen (listen_port->port.ipc, callback);
}
static DiagnosticsPortVtable listen_port_vtable = {
listen_port_free_func,
listen_port_get_ipc_poll_handle_func,
listen_port_get_connected_stream_func,
listen_port_reset };
DiagnosticsListenPort *
ds_listen_port_alloc (
DiagnosticsIpc *ipc,
DiagnosticsPortBuilder *builder)
{
DiagnosticsListenPort * instance = ep_rt_object_alloc (DiagnosticsListenPort);
ep_raise_error_if_nok (instance != NULL);
ep_raise_error_if_nok (ds_port_init (
(DiagnosticsPort *)instance,
&listen_port_vtable,
ipc,
builder) != NULL);
ep_on_exit:
return instance;
ep_on_error:
ds_listen_port_free (instance);
instance = NULL;
ep_exit_error_handler ();
}
void
ds_listen_port_free (DiagnosticsListenPort *listen_port)
{
ep_return_void_if_nok (listen_port != NULL);
ds_port_fini (&listen_port->port);
ep_rt_object_free (listen_port);
}
#endif /* !defined(DS_INCLUDE_SOURCE_FILES) || defined(DS_FORCE_INCLUDE_SOURCE_FILES) */
#endif /* ENABLE_PERFTRACING */
#if !defined(ENABLE_PERFTRACING) || (defined(DS_INCLUDE_SOURCE_FILES) && !defined(DS_FORCE_INCLUDE_SOURCE_FILES))
extern const char quiet_linker_empty_file_warning_diagnostics_ipc;
const char quiet_linker_empty_file_warning_diagnostics_ipc = 0;
#endif