Merge pull request #70 from vibe-d/lazy_dns_event

Create the DNS helper event lazily.
This commit is contained in:
Sönke Ludwig 2018-03-18 09:58:14 +01:00 committed by GitHub
commit df9e9fce3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -38,21 +38,23 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver
}
ChoppedVector!Lookup m_lookups;
Events m_events;
EventID m_event;
EventID m_event = EventID.invalid;
size_t m_maxHandle;
}
this(Events events, Signals signals)
{
m_events = events;
m_event = events.createInternal();
m_events.wait(m_event, &onDNSSignal);
setupEvent();
}
void dispose()
{
if (m_event != EventID.invalid) {
m_events.cancelWait(m_event, &onDNSSignal);
m_events.releaseRef(m_event);
m_event = EventID.invalid;
}
}
override DNSLookupID lookupHost(string name, DNSLookupCallback on_lookup_finished)
@ -61,6 +63,8 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver
auto handle = getFreeHandle();
if (handle > m_maxHandle) m_maxHandle = handle;
setupEvent();
assert(!m_lookups[handle].result);
Lookup* l = () @trusted { return &m_lookups[handle]; } ();
l.name = name;
@ -131,6 +135,14 @@ final class EventDriverDNS_GAI(Events : EventDriverEvents, Signals : EventDriver
return cast(DNSLookupID)cast(int)i;
return cast(DNSLookupID)cast(int)m_lookups.length;
}
private void setupEvent()
{
if (m_event == EventID.invalid) {
m_event = m_events.createInternal();
m_events.wait(m_event, &onDNSSignal);
}
}
}