/
libevent2.d
1251 lines (1079 loc) · 35.4 KB
/
libevent2.d
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
libevent based driver
Copyright: © 2012-2013 RejectedSoftware e.K.
Authors: Sönke Ludwig
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
*/
module vibe.core.drivers.libevent2;
version(VibeLibeventDriver)
{
import vibe.core.driver;
import vibe.core.drivers.libevent2_tcp;
import vibe.core.drivers.threadedfile;
import vibe.core.drivers.utils;
import vibe.core.log;
import vibe.utils.array : ArraySet;
import vibe.utils.hashmap;
import vibe.utils.memory;
import core.memory;
import core.atomic;
import core.stdc.config;
import core.stdc.errno;
import core.stdc.stdlib;
import core.sync.condition;
import core.sync.mutex;
import core.sync.rwmutex;
import core.sys.posix.netinet.in_;
import core.sys.posix.netinet.tcp;
import core.thread;
import deimos.event2.bufferevent;
import deimos.event2.dns;
import deimos.event2.event;
import deimos.event2.thread;
import deimos.event2.util;
import std.conv;
import std.encoding : sanitize;
import std.exception;
import std.range : assumeSorted;
import std.string;
version (Windows)
{
version(VibePragmaLib) pragma(lib, "event2");
pragma(lib, "ws2_32.lib");
}
else
version(VibePragmaLib) pragma(lib, "event");
version(Windows)
{
import std.c.windows.winsock;
alias WSAEWOULDBLOCK EWOULDBLOCK;
}
class Libevent2Driver : EventDriver {
import std.datetime : Clock;
private {
DriverCore m_core;
event_base* m_eventLoop;
evdns_base* m_dnsBase;
bool m_exit = false;
ArraySet!size_t m_ownedObjects;
debug Thread m_ownerThread;
event* m_timerEvent;
int m_timerIDCounter = 1;
HashMap!(size_t, TimerInfo) m_timers;
static if (__VERSION__ >= 2065) {
import std.container : Array, BinaryHeap, heapify;
BinaryHeap!(Array!TimeoutEntry, "a.timeout > b.timeout") m_timeoutHeap;
} else {
import std.container : DList;
DList!TimeoutEntry m_timeoutHeap;
}
}
this(DriverCore core)
{
debug m_ownerThread = Thread.getThis();
m_core = core;
s_driverCore = core;
if (!s_threadObjectsMutex) s_threadObjectsMutex = new Mutex;
// set the malloc/free versions of our runtime so we don't run into trouble
// because the libevent DLL uses a different one.
event_set_mem_functions(&lev_alloc, &lev_realloc, &lev_free);
evthread_lock_callbacks lcb;
lcb.lock_api_version = EVTHREAD_LOCK_API_VERSION;
lcb.supported_locktypes = EVTHREAD_LOCKTYPE_RECURSIVE|EVTHREAD_LOCKTYPE_READWRITE;
lcb.alloc = &lev_alloc_mutex;
lcb.free = &lev_free_mutex;
lcb.lock = &lev_lock_mutex;
lcb.unlock = &lev_unlock_mutex;
evthread_set_lock_callbacks(&lcb);
evthread_condition_callbacks ccb;
ccb.condition_api_version = EVTHREAD_CONDITION_API_VERSION;
ccb.alloc_condition = &lev_alloc_condition;
ccb.free_condition = &lev_free_condition;
ccb.signal_condition = &lev_signal_condition;
ccb.wait_condition = &lev_wait_condition;
evthread_set_condition_callbacks(&ccb);
evthread_set_id_callback(&lev_get_thread_id);
// initialize libevent
logDiagnostic("libevent version: %s", to!string(event_get_version()));
m_eventLoop = event_base_new();
s_eventLoop = m_eventLoop;
logDiagnostic("libevent is using %s for events.", to!string(event_base_get_method(m_eventLoop)));
evthread_make_base_notifiable(m_eventLoop);
m_dnsBase = evdns_base_new(m_eventLoop, 1);
if( !m_dnsBase ) logError("Failed to initialize DNS lookup.");
string hosts_file;
version (Windows) hosts_file = `C:\Windows\System32\drivers\etc\hosts`;
else hosts_file = `/etc/hosts`;
if (existsFile(hosts_file)) {
if (evdns_base_load_hosts(m_dnsBase, hosts_file.toStringz()) != 0)
logError("Failed to load hosts file at %s", hosts_file);
}
m_timerEvent = event_new(m_eventLoop, -1, EV_TIMEOUT, &onTimerTimeout, cast(void*)this);
}
~this()
{
debug assert(Thread.getThis() is m_ownerThread, "Event loop destroyed in foreign thread.");
event_free(m_timerEvent);
// notify all other living objects about the shutdown
synchronized (s_threadObjectsMutex) {
// destroy all living objects owned by this driver
foreach (ref key; m_ownedObjects) {
assert(key);
auto obj = cast(Libevent2Object)cast(void*)key;
debug assert(obj.m_ownerThread is m_ownerThread, "Owned object with foreign thread ID detected.");
debug assert(obj.m_driver is this, "Owned object with foreign driver reference detected.");
key = 0;
destroy(obj);
}
foreach (ref key; s_threadObjects) {
assert(key);
auto obj = cast(Libevent2Object)cast(void*)key;
debug assert(obj.m_ownerThread !is m_ownerThread, "Live object of this thread detected after all owned mutexes have been destroyed.");
debug assert(obj.m_driver !is this, "Live object of this driver detected with different thread ID after all owned mutexes have been destroyed.");
obj.onThreadShutdown();
}
}
// shutdown libevent for this thread
evdns_base_free(m_dnsBase, 1);
event_base_free(m_eventLoop);
s_eventLoop = null;
s_alreadyDeinitialized = true;
}
@property event_base* eventLoop() { return m_eventLoop; }
@property evdns_base* dnsEngine() { return m_dnsBase; }
int runEventLoop()
{
int ret;
m_exit = false;
while (!m_exit && (ret = event_base_loop(m_eventLoop, EVLOOP_ONCE)) == 0) {
processTimers();
s_driverCore.notifyIdle();
}
return ret;
}
int runEventLoopOnce()
{
auto ret = event_base_loop(m_eventLoop, EVLOOP_ONCE);
processTimers();
m_core.notifyIdle();
return ret;
}
bool processEvents()
{
event_base_loop(m_eventLoop, EVLOOP_NONBLOCK);
processTimers();
if (m_exit) {
m_exit = false;
return false;
}
return true;
}
void exitEventLoop()
{
m_exit = true;
enforce(event_base_loopbreak(m_eventLoop) == 0, "Failed to exit libevent event loop.");
}
FileStream openFile(Path path, FileMode mode)
{
return new ThreadedFileStream(path, mode);
}
DirectoryWatcher watchDirectory(Path path, bool recursive)
{
assert(false, "watchDirectory is not yet implemented in the libevent driver.");
}
NetworkAddress resolveHost(string host, ushort family = AF_UNSPEC, bool use_dns = true)
{
assert(m_dnsBase);
evutil_addrinfo hints;
hints.ai_family = family;
if (!use_dns) {
//When this flag is set, we only resolve numeric IPv4 and IPv6
//addresses; if the nodename would require a name lookup, we instead
//give an EVUTIL_EAI_NONAME error.
hints.ai_flags = EVUTIL_AI_NUMERICHOST;
}
logDebug("dnsresolve %s", host);
GetAddrInfoMsg msg;
msg.core = m_core;
evdns_getaddrinfo_request* dnsReq = evdns_getaddrinfo(m_dnsBase, toStringz(host), null,
&hints, &onAddrInfo, &msg);
// wait if the request couldn't be fulfilled instantly
if (!msg.done) {
assert(dnsReq !is null);
msg.task = Task.getThis();
logDebug("dnsresolve yield");
while (!msg.done) m_core.yieldForEvent();
}
logDebug("dnsresolve ret");
enforce(msg.err == DNS_ERR_NONE, format("Failed to lookup host '%s': %s", host, to!string(evutil_gai_strerror(msg.err))));
return msg.addr;
}
TCPConnection connectTCP(NetworkAddress addr)
{
auto sockfd_raw = socket(addr.family, SOCK_STREAM, 0);
// on Win64 socket() returns a 64-bit value but libevent expects an int
static if (typeof(sockfd_raw).max > int.max) assert(sockfd_raw <= int.max || sockfd_raw == ~0);
auto sockfd = cast(int)sockfd_raw;
socketEnforce(sockfd != -1, "Failed to create socket.");
NetworkAddress bind_addr;
bind_addr.family = addr.family;
if (addr.family == AF_INET) bind_addr.sockAddrInet4.sin_addr.s_addr = 0;
else bind_addr.sockAddrInet6.sin6_addr.s6_addr[] = 0;
socketEnforce(bind(sockfd, bind_addr.sockAddr, bind_addr.sockAddrLen) == 0, "Failed to bind socket.");
socklen_t balen = bind_addr.sockAddrLen;
socketEnforce(getsockname(sockfd, bind_addr.sockAddr, &balen) == 0, "getsockname failed.");
if( evutil_make_socket_nonblocking(sockfd) )
throw new Exception("Failed to make socket non-blocking.");
auto buf_event = bufferevent_socket_new(m_eventLoop, sockfd, bufferevent_options.BEV_OPT_CLOSE_ON_FREE);
if( !buf_event ) throw new Exception("Failed to create buffer event for socket.");
auto cctx = TCPContextAlloc.alloc(m_core, m_eventLoop, sockfd, buf_event, bind_addr, addr);
bufferevent_setcb(buf_event, &onSocketRead, &onSocketWrite, &onSocketEvent, cctx);
if( bufferevent_enable(buf_event, EV_READ|EV_WRITE) )
throw new Exception("Error enabling buffered I/O event for socket.");
cctx.readOwner = Task.getThis();
scope(exit) cctx.readOwner = Task();
assert(cctx.exception is null);
socketEnforce(bufferevent_socket_connect(buf_event, addr.sockAddr, addr.sockAddrLen) == 0,
"Failed to connect to " ~ addr.toString());
try {
cctx.checkForException();
// TODO: cctx.remote_addr6 = ...;
while (cctx.status == 0)
m_core.yieldForEvent();
} catch (Exception e) {
throw new Exception(format("Failed to connect to %s: %s", addr.toString(), e.msg));
}
logTrace("Connect result status: %d", cctx.status);
enforce(cctx.status == BEV_EVENT_CONNECTED, format("Failed to connect to host %s: %s", addr.toString(), cctx.status));
return new Libevent2TCPConnection(cctx);
}
TCPListener listenTCP(ushort port, void delegate(TCPConnection conn) connection_callback, string address, TCPListenOptions options)
{
auto bind_addr = resolveHost(address, AF_UNSPEC, false);
bind_addr.port = port;
auto listenfd_raw = socket(bind_addr.family, SOCK_STREAM, 0);
// on Win64 socket() returns a 64-bit value but libevent expects an int
static if (typeof(listenfd_raw).max > int.max) assert(listenfd_raw <= int.max || listenfd_raw == ~0);
auto listenfd = cast(int)listenfd_raw;
socketEnforce(listenfd != -1, "Error creating listening socket");
int tmp_reuse = 1;
socketEnforce(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &tmp_reuse, tmp_reuse.sizeof) == 0,
"Error enabling socket address reuse on listening socket");
socketEnforce(bind(listenfd, bind_addr.sockAddr, bind_addr.sockAddrLen) == 0,
"Error binding listening socket");
socketEnforce(listen(listenfd, 128) == 0,
"Error listening to listening socket");
// Set socket for non-blocking I/O
enforce(evutil_make_socket_nonblocking(listenfd) == 0,
"Error setting listening socket to non-blocking I/O.");
auto ret = new LibeventTCPListener;
static void setupConnectionHandler(shared(LibeventTCPListener) listener, typeof(listenfd) listenfd, NetworkAddress bind_addr, shared(void delegate(TCPConnection conn)) connection_callback, TCPListenOptions options)
{
auto evloop = getThreadLibeventEventLoop();
auto core = getThreadLibeventDriverCore();
// Add an event to wait for connections
auto ctx = TCPContextAlloc.alloc(core, evloop, listenfd, null, bind_addr, NetworkAddress());
ctx.connectionCallback = cast()connection_callback;
ctx.listenEvent = event_new(evloop, listenfd, EV_READ | EV_PERSIST, &onConnect, ctx);
ctx.listenOptions = options;
enforce(event_add(ctx.listenEvent, null) == 0,
"Error scheduling connection event on the event loop.");
(cast()listener).addContext(ctx);
}
// FIXME: the API needs improvement with proper shared annotations, so the the following casts are not necessary
if (options & TCPListenOptions.distribute) runWorkerTaskDist(&setupConnectionHandler, cast(shared)ret, listenfd, bind_addr, cast(shared)connection_callback, options);
else setupConnectionHandler(cast(shared)ret, listenfd, bind_addr, cast(shared)connection_callback, options);
return ret;
}
UDPConnection listenUDP(ushort port, string bind_address = "0.0.0.0")
{
NetworkAddress bindaddr = resolveHost(bind_address, AF_UNSPEC, false);
bindaddr.port = port;
return new Libevent2UDPConnection(bindaddr, this);
}
Libevent2ManualEvent createManualEvent()
{
return new Libevent2ManualEvent(this);
}
Libevent2FileDescriptorEvent createFileDescriptorEvent(int fd, FileDescriptorEvent.Trigger events)
{
return new Libevent2FileDescriptorEvent(this, fd, events);
}
size_t createTimer(void delegate() callback)
{
debug assert(m_ownerThread is Thread.getThis());
auto id = m_timerIDCounter++;
if (!id) id = m_timerIDCounter++;
m_timers[id] = TimerInfo(callback);
return id;
}
void acquireTimer(size_t timer_id) { m_timers[timer_id].refCount++; }
void releaseTimer(size_t timer_id)
{
if (!--m_timers[timer_id].refCount)
m_timers.remove(timer_id);
}
bool isTimerPending(size_t timer_id) { return m_timers[timer_id].pending; }
void rearmTimer(size_t timer_id, Duration dur, bool periodic)
{
debug assert(m_ownerThread is Thread.getThis());
auto timeout = Clock.currStdTime() + dur.total!"hnsecs";
auto pt = timer_id in m_timers;
assert(pt !is null, "Accessing non-existent timer ID.");
pt.timeout = timeout;
pt.repeatDuration = periodic ? dur.total!"hnsecs" : 0;
if (!pt.pending) {
pt.pending = true;
acquireTimer(timer_id);
}
logDebugV("rearming timer %s in %s s", timer_id, dur.total!"usecs" * 1e-6);
scheduleTimer(timeout, timer_id);
}
void stopTimer(size_t timer_id)
{
logTrace("Stopping timer %s", timer_id);
auto pt = timer_id in m_timers;
if (pt.pending) {
pt.pending = false;
releaseTimer(timer_id);
}
}
void waitTimer(size_t timer_id)
{
debug assert(m_ownerThread is Thread.getThis());
while (true) {
{
auto pt = timer_id in m_timers;
if (!pt || !pt.pending) return;
assert(pt.repeatDuration == 0, "Cannot wait for a periodic timer.");
assert(pt.owner == Task.init, "Waiting for the same timer from multiple tasks is not supported.");
pt.owner = Task.getThis();
}
scope (exit) {
auto pt = timer_id in m_timers;
if (pt) pt.owner = Task.init;
}
m_core.yieldForEvent();
}
}
private void processTimers()
{
logTrace("Processing due timers");
// process all timers that have expired up to now
auto now = Clock.currStdTime();
if (m_timeoutHeap.empty) logTrace("no timers scheduled");
else logTrace("first timeout: %s", (m_timeoutHeap.front.timeout - now) * 1e-7);
while (!m_timeoutHeap.empty && (m_timeoutHeap.front.timeout - now) / 10_000 <= 0) {
auto tm = m_timeoutHeap.front;
m_timeoutHeap.removeFront();
auto pt = tm.id in m_timers;
if (!pt || !pt.pending || pt.timeout != tm.timeout) continue;
Task owner = pt.owner;
auto callback = pt.callback;
if (pt.repeatDuration > 0) {
pt.timeout += pt.repeatDuration;
scheduleTimer(pt.timeout, tm.id);
} else {
pt.pending = false;
releaseTimer(tm.id);
}
logTrace("Timer %s fired (%s/%s)", tm.id, owner != Task.init, callback !is null);
if (owner && owner.running) m_core.resumeTask(owner);
if (callback) runTask(callback);
}
if (!m_timeoutHeap.empty) rescheduleTimerEvent((m_timeoutHeap.front.timeout - now).hnsecs);
}
private void scheduleTimer(long timeout, size_t id)
{
logTrace("Schedule timer %s", id);
auto now = Clock.currStdTime();
if (m_timeoutHeap.empty || timeout < m_timeoutHeap.front.timeout) {
rescheduleTimerEvent((timeout - now).hnsecs);
}
auto entry = TimeoutEntry(timeout, id);
static if (__VERSION__ >= 2065) {
m_timeoutHeap.insert(entry);
} else {
auto existing = m_timeoutHeap[];
while (!existing.empty && existing.front.timeout < entry.timeout)
existing.popFront();
m_timeoutHeap.insertBefore(existing, entry);
}
logDebugV("first timer %s in %s s", id, (timeout - now) * 1e-7);
}
private void rescheduleTimerEvent(Duration dur)
{
event_del(m_timerEvent);
assert(dur.total!"seconds"() <= int.max);
dur += 9.hnsecs(); // round up to the next usec to avoid premature timer events
timeval tvdur;
tvdur.tv_sec = cast(int)dur.total!"seconds"();
tvdur.tv_usec = dur.fracSec().usecs();
event_add(m_timerEvent, &tvdur);
assert(event_pending(m_timerEvent, EV_TIMEOUT, null));
logTrace("Rescheduled timer event for %s seconds", dur.total!"usecs" * 1e-6);
}
private static nothrow extern(C)
void onTimerTimeout(evutil_socket_t, short events, void* userptr)
{
logTrace("timer event fired");
auto drv = cast(Libevent2Driver)userptr;
try drv.processTimers();
catch (Exception e) {
logError("Failed to process timers: %s", e.msg);
try logDiagnostic("Full error: %s", e.toString().sanitize); catch {}
}
}
private static nothrow extern(C) void onAddrInfo(int err, evutil_addrinfo* res, void* arg)
{
auto msg = cast(GetAddrInfoMsg*)arg;
msg.err = err;
msg.done = true;
if (err == DNS_ERR_NONE) {
assert(res !is null);
scope (exit) evutil_freeaddrinfo(res);
// Note that we are only returning the first address and ignoring the
// rest. Ideally we should return all of the NetworkAddress
msg.addr.family = cast(ushort)res.ai_family;
assert(res.ai_addrlen == msg.addr.sockAddrLen());
switch (msg.addr.family) {
case AF_INET:
auto sock4 = cast(sockaddr_in*)res.ai_addr;
msg.addr.sockAddrInet4.sin_addr.s_addr = sock4.sin_addr.s_addr;
break;
case AF_INET6:
auto sock6 = cast(sockaddr_in6*)res.ai_addr;
msg.addr.sockAddrInet6.sin6_addr.s6_addr = sock6.sin6_addr.s6_addr;
break;
default:
logDiagnostic("DNS lookup yielded unknown address family: %s", msg.addr.family);
err = DNS_ERR_UNKNOWN;
break;
}
}
if (msg.task && msg.task.running) {
try msg.core.resumeTask(msg.task);
catch (Exception e) logWarn("Error resuming DNS query task: %s", e.msg);
}
}
private void registerObject(Libevent2Object obj)
{
debug assert(Thread.getThis() is m_ownerThread, "Event object created in foreign thread.");
auto key = cast(size_t)cast(void*)obj;
synchronized (s_threadObjectsMutex) {
m_ownedObjects.insert(key);
s_threadObjects.insert(key);
}
}
private void unregisterObject(Libevent2Object obj)
{
auto key = cast(size_t)cast(void*)obj;
synchronized (s_threadObjectsMutex) {
m_ownedObjects.remove(key);
s_threadObjects.remove(key);
}
}
}
private struct TimerInfo {
long timeout;
long repeatDuration;
size_t refCount = 1;
void delegate() callback;
Task owner;
bool pending;
this(void delegate() callback) { this.callback = callback; }
}
private struct TimeoutEntry {
long timeout;
size_t id;
}
private struct GetAddrInfoMsg {
NetworkAddress addr;
bool done = false;
int err = 0;
DriverCore core;
Task task;
}
private class Libevent2Object {
protected Libevent2Driver m_driver;
debug private Thread m_ownerThread;
this(Libevent2Driver driver)
{
m_driver = driver;
m_driver.registerObject(this);
debug m_ownerThread = driver.m_ownerThread;
}
~this()
{
// NOTE: m_driver will always be destroyed deterministically
// in static ~this(), so it can be used here safely
m_driver.unregisterObject(this);
}
protected void onThreadShutdown() {}
}
/// private
struct ThreadSlot {
Libevent2Driver driver;
deimos.event2.event.event* event;
ArraySet!Task tasks;
}
/// private
alias ThreadSlotMap = HashMap!(Thread, ThreadSlot);
class Libevent2ManualEvent : Libevent2Object, ManualEvent {
private {
shared(int) m_emitCount = 0;
core.sync.mutex.Mutex m_mutex;
ThreadSlotMap m_waiters;
}
this(Libevent2Driver driver)
{
super(driver);
m_mutex = new core.sync.mutex.Mutex;
m_waiters = ThreadSlotMap(manualAllocator());
}
~this()
{
foreach (ts; m_waiters)
event_free(ts.event);
}
void emit()
{
atomicOp!"+="(m_emitCount, 1);
synchronized (m_mutex) {
foreach (ref sl; m_waiters)
event_active(sl.event, 0, 0);
}
}
void wait()
{
wait(m_emitCount);
}
int wait(int reference_emit_count)
{
assert(!amOwner());
acquire();
scope(exit) release();
auto ec = this.emitCount;
while( ec == reference_emit_count ){
getThreadLibeventDriverCore().yieldForEvent();
ec = this.emitCount;
}
return ec;
}
int wait(Duration timeout, int reference_emit_count)
{
assert(!amOwner());
acquire();
scope(exit) release();
auto tm = m_driver.createTimer(null);
scope (exit) m_driver.releaseTimer(tm);
m_driver.m_timers[tm].owner = Task.getThis();
m_driver.rearmTimer(tm, timeout, false);
auto ec = this.emitCount;
while( ec == reference_emit_count ){
getThreadLibeventDriverCore().yieldForEvent();
ec = this.emitCount;
if (!m_driver.isTimerPending(tm)) break;
}
return ec;
}
void acquire()
{
auto task = Task.getThis();
assert(task != Task(), "ManualEvent.wait works only when called from a task.");
auto thread = task.thread;
synchronized (m_mutex) {
if (thread !in m_waiters) {
ThreadSlot slot;
slot.driver = cast(Libevent2Driver)getEventDriver();
slot.event = event_new(slot.driver.eventLoop, -1, EV_PERSIST, &onSignalTriggered, cast(void*)this);
event_add(slot.event, null);
m_waiters[thread] = slot;
}
assert(task !in m_waiters[thread].tasks, "Double acquisition of signal.");
m_waiters[thread].tasks.insert(task);
}
}
void release()
{
auto self = Task.getThis();
synchronized (m_mutex) {
assert(self.thread in m_waiters && self in m_waiters[self.thread].tasks,
"Releasing non-acquired signal.");
m_waiters[self.thread].tasks.remove(self);
}
}
bool amOwner()
{
auto self = Task.getThis();
synchronized (m_mutex) {
if (self.thread !in m_waiters) return false;
return self in m_waiters[self.thread].tasks;
}
}
@property int emitCount() const { return atomicLoad(m_emitCount); }
protected override void onThreadShutdown()
{
auto thr = Thread.getThis();
synchronized (m_mutex) {
if (thr in m_waiters) {
event_free(m_waiters[thr].event);
m_waiters.remove(thr);
}
}
}
private static nothrow extern(C)
void onSignalTriggered(evutil_socket_t, short events, void* userptr)
{
try {
auto sig = cast(Libevent2ManualEvent)userptr;
auto thread = Thread.getThis();
auto core = getThreadLibeventDriverCore();
ArraySet!Task lst;
synchronized (sig.m_mutex) {
assert(thread in sig.m_waiters);
lst = sig.m_waiters[thread].tasks.dup;
}
foreach (l; lst)
core.resumeTask(l);
} catch (Exception e) {
logError("Exception while handling signal event: %s", e.msg);
try logDiagnostic("Full error: %s", sanitize(e.msg));
catch(Exception) {}
debug assert(false);
}
}
}
class Libevent2FileDescriptorEvent : Libevent2Object, FileDescriptorEvent {
private {
int m_fd;
deimos.event2.event.event* m_event;
Trigger m_activeEvents;
Task m_waiter;
}
this(Libevent2Driver driver, int file_descriptor, Trigger events)
{
assert(events != Trigger.none);
super(driver);
m_fd = file_descriptor;
short evts = 0;
if (events & Trigger.read) evts |= EV_READ;
if (events & Trigger.write) evts |= EV_WRITE;
m_event = event_new(driver.eventLoop, file_descriptor, evts|EV_PERSIST, &onFileTriggered, cast(void*)this);
event_add(m_event, null);
}
~this()
{
event_free(m_event);
}
void wait(Trigger which)
{
assert(!m_waiter, "Only one task may wait on a Libevent2FileEvent.");
m_waiter = Task.getThis();
scope (exit) {
m_waiter = Task.init;
m_activeEvents &= ~which;
}
while ((m_activeEvents & which) == Trigger.none)
getThreadLibeventDriverCore().yieldForEvent();
}
bool wait(Duration timeout, Trigger which)
{
assert(!m_waiter, "Only one task may wait on a Libevent2FileEvent.");
m_waiter = Task.getThis();
scope (exit) {
m_waiter = Task.init;
m_activeEvents &= ~which;
}
auto tm = m_driver.createTimer(null);
scope (exit) m_driver.releaseTimer(tm);
m_driver.m_timers[tm].owner = Task.getThis();
m_driver.rearmTimer(tm, timeout, false);
while ((m_activeEvents & which) == Trigger.none) {
getThreadLibeventDriverCore().yieldForEvent();
if (!m_driver.isTimerPending(tm)) break;
}
return (m_activeEvents & which) != Trigger.none;
}
private static nothrow extern(C)
void onFileTriggered(evutil_socket_t fd, short events, void* userptr)
{
try {
auto core = getThreadLibeventDriverCore();
auto evt = cast(Libevent2FileDescriptorEvent)userptr;
evt.m_activeEvents = Trigger.none;
if (events & EV_READ) evt.m_activeEvents |= Trigger.read;
if (events & EV_WRITE) evt.m_activeEvents |= Trigger.write;
if (evt.m_waiter) core.resumeTask(evt.m_waiter);
} catch (Exception e) {
logError("Exception while handling file event: %s", e.msg);
try logDiagnostic("Full error: %s", sanitize(e.msg));
catch(Exception) {}
debug assert(false);
}
}
}
class Libevent2UDPConnection : UDPConnection {
private {
Libevent2Driver m_driver;
TCPContext* m_ctx;
NetworkAddress m_bindAddress;
string m_bindAddressString;
bool m_canBroadcast = false;
}
this(NetworkAddress bind_addr, Libevent2Driver driver)
{
m_driver = driver;
m_bindAddress = bind_addr;
char buf[64];
void* ptr;
if( bind_addr.family == AF_INET ) ptr = &bind_addr.sockAddrInet4.sin_addr;
else ptr = &bind_addr.sockAddrInet6.sin6_addr;
evutil_inet_ntop(bind_addr.family, ptr, buf.ptr, buf.length);
m_bindAddressString = to!string(buf.ptr);
auto sockfd_raw = socket(bind_addr.family, SOCK_DGRAM, IPPROTO_UDP);
// on Win64 socket() returns a 64-bit value but libevent expects an int
static if (typeof(sockfd_raw).max > int.max) assert(sockfd_raw <= int.max || sockfd_raw == ~0);
auto sockfd = cast(int)sockfd_raw;
socketEnforce(sockfd != -1, "Failed to create socket.");
enforce(evutil_make_socket_nonblocking(sockfd) == 0, "Failed to make socket non-blocking.");
int tmp_reuse = 1;
socketEnforce(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &tmp_reuse, tmp_reuse.sizeof) == 0,
"Error enabling socket address reuse on listening socket");
if( bind_addr.port )
socketEnforce(bind(sockfd, bind_addr.sockAddr, bind_addr.sockAddrLen) == 0, "Failed to bind UDP socket.");
m_ctx = TCPContextAlloc.alloc(driver.m_core, driver.m_eventLoop, sockfd, null, bind_addr, NetworkAddress());
auto evt = event_new(driver.m_eventLoop, sockfd, EV_READ|EV_PERSIST, &onUDPRead, m_ctx);
if( !evt ) throw new Exception("Failed to create buffer event for socket.");
enforce(event_add(evt, null) == 0);
}
@property string bindAddress() const { return m_bindAddressString; }
@property NetworkAddress localAddress() const { return m_bindAddress; }
@property bool canBroadcast() const { return m_canBroadcast; }
@property void canBroadcast(bool val)
{
int tmp_broad = val;
enforce(setsockopt(m_ctx.socketfd, SOL_SOCKET, SO_BROADCAST, &tmp_broad, tmp_broad.sizeof) == 0,
"Failed to change the socket broadcast flag.");
m_canBroadcast = val;
}
bool amOwner() {
return m_ctx !is null && m_ctx.readOwner != Task() && m_ctx.readOwner == Task.getThis() && m_ctx.readOwner == m_ctx.writeOwner;
}
void acquire()
{
assert(m_ctx, "Trying to acquire a closed UDP connection.");
assert(m_ctx.readOwner == Task() && m_ctx.writeOwner == Task(),
"Trying to acquire a UDP connection that is currently owned.");
m_ctx.readOwner = m_ctx.writeOwner = Task.getThis();
}
void release()
{
if (!m_ctx) return;
assert(m_ctx.readOwner == Task.getThis() && m_ctx.readOwner == m_ctx.writeOwner,
"Trying to release a UDP connection that is not owned by the current task.");
m_ctx.readOwner = m_ctx.writeOwner = Task.init;
}
void connect(string host, ushort port)
{
NetworkAddress addr = m_driver.resolveHost(host, m_ctx.local_addr.family);
addr.port = port;
connect(addr);
}
void connect(NetworkAddress addr)
{
enforce(.connect(m_ctx.socketfd, addr.sockAddr, addr.sockAddrLen) == 0, "Failed to connect UDP socket."~to!string(getLastSocketError()));
}
void send(in ubyte[] data, in NetworkAddress* peer_address = null)
{
sizediff_t ret;
assert(data.length <= int.max);
if( peer_address ){
ret = .sendto(m_ctx.socketfd, data.ptr, cast(int)data.length, 0, peer_address.sockAddr, peer_address.sockAddrLen);
} else {
ret = .send(m_ctx.socketfd, data.ptr, cast(int)data.length, 0);
}
logTrace("send ret: %s, %s", ret, getLastSocketError());
enforce(ret >= 0, "Error sending UDP packet.");
enforce(ret == data.length, "Unable to send full packet.");
}
ubyte[] recv(ubyte[] buf = null, NetworkAddress* peer_address = null)
{
return recv(Duration.max, buf, peer_address);
}
ubyte[] recv(Duration timeout, ubyte[] buf = null, NetworkAddress* peer_address = null)
{
size_t tm = size_t.max;
if (timeout >= 0.seconds && timeout != Duration.max) {
tm = m_driver.createTimer(null);
m_driver.rearmTimer(tm, timeout, false);
m_driver.acquireTimer(tm);
}
acquire();
scope (exit) {
release();
if (tm != size_t.max) m_driver.releaseTimer(tm);
}
if (buf.length == 0) buf.length = 65507;
NetworkAddress from;
from.family = m_ctx.local_addr.family;
assert(buf.length <= int.max);
while (true) {
socklen_t addr_len = from.sockAddrLen;
auto ret = .recvfrom(m_ctx.socketfd, buf.ptr, cast(int)buf.length, 0, from.sockAddr, &addr_len);
if (ret > 0) {
if( peer_address ) *peer_address = from;
return buf[0 .. ret];
}
if (ret < 0) {
auto err = getLastSocketError();
if (err != EWOULDBLOCK) {
logDebugV("UDP recv err: %s", err);
throw new Exception("Error receiving UDP packet.");
}
if (timeout != Duration.max) {
enforce(timeout > 0.seconds && m_driver.isTimerPending(tm), "UDP receive timeout.");
}
}
m_ctx.core.yieldForEvent();
}
}
private static nothrow extern(C) void onUDPRead(evutil_socket_t sockfd, short evts, void* arg)
{
auto ctx = cast(TCPContext*)arg;
logTrace("udp socket %d read event!", ctx.socketfd);
try {
auto f = ctx.readOwner;
if (f && f.running)
ctx.core.resumeTask(f);
} catch( Throwable e ){
logError("Exception onUDPRead: %s", e.msg);
debug assert(false);