Compare commits
6 Commits
d9fda61fe1
...
v0.18.0
Author | SHA1 | Date | |
---|---|---|---|
db607f7602
|
|||
cfcb1e727a
|
|||
c7bfbe0657
|
|||
0155039071
|
|||
c15a8993ec
|
|||
be8fcb3e1c
|
@ -13,14 +13,13 @@ Garbage Collector heap. Everything in the library is usable in @nogc code.
|
|||||||
Tanya provides data structures and utilities to facilitate painless systems
|
Tanya provides data structures and utilities to facilitate painless systems
|
||||||
programming in D.
|
programming in D.
|
||||||
|
|
||||||
- [API Documentation](https://docs.caraus.io/tanya)
|
- [API Documentation](https://docs.caraus.tech/tanya)
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
Tanya consists of the following packages and (top-level) modules:
|
Tanya consists of the following packages and (top-level) modules:
|
||||||
|
|
||||||
* `algorithm`: Collection of generic algorithms.
|
* `algorithm`: Collection of generic algorithms.
|
||||||
* `async`: Event loop (epoll, kqueue and IOCP).
|
|
||||||
* `bitmanip`: Bit manipulation.
|
* `bitmanip`: Bit manipulation.
|
||||||
* `container`: Queue, Array, Singly and doubly linked lists, Buffers, UTF-8
|
* `container`: Queue, Array, Singly and doubly linked lists, Buffers, UTF-8
|
||||||
string, Set, Hash table.
|
string, Set, Hash table.
|
||||||
@ -35,9 +34,6 @@ type information at compile-time, to transform from one type to another. It has
|
|||||||
also different algorithms for iterating, searching and modifying template
|
also different algorithms for iterating, searching and modifying template
|
||||||
arguments.
|
arguments.
|
||||||
* `net`: URL-Parsing, network programming.
|
* `net`: URL-Parsing, network programming.
|
||||||
* `network`: Socket implementation. `network` is currently under rework.
|
|
||||||
After finishing the new socket implementation will land in the `net` package and
|
|
||||||
`network` will be deprecated.
|
|
||||||
* `os`: Platform-independent interfaces to operating system functionality.
|
* `os`: Platform-independent interfaces to operating system functionality.
|
||||||
* `range`: Generic functions and templates for D ranges.
|
* `range`: Generic functions and templates for D ranges.
|
||||||
* `test`: Test suite for unittest-blocks.
|
* `test`: Test suite for unittest-blocks.
|
||||||
@ -167,7 +163,7 @@ parameter is used)
|
|||||||
|
|
||||||
| DMD | GCC |
|
| DMD | GCC |
|
||||||
|:-------:|:---------:|
|
|:-------:|:---------:|
|
||||||
| 2.091.1 | gdc trunk |
|
| 2.098.1 | 11.2 |
|
||||||
|
|
||||||
## Further characteristics
|
## Further characteristics
|
||||||
|
|
||||||
|
@ -226,7 +226,7 @@ if (F.length == 1)
|
|||||||
*
|
*
|
||||||
* Returns: Accumulated value.
|
* Returns: Accumulated value.
|
||||||
*/
|
*/
|
||||||
auto foldr(R, T)(R range, auto ref T init)
|
auto foldr(R, T)(scope R range, scope return auto ref T init)
|
||||||
if (isBidirectionalRange!R)
|
if (isBidirectionalRange!R)
|
||||||
{
|
{
|
||||||
if (range.empty)
|
if (range.empty)
|
||||||
|
@ -177,9 +177,7 @@ if (isInputRange!Range && hasLvalueElements!Range)
|
|||||||
import tanya.memory.op : copy, fill;
|
import tanya.memory.op : copy, fill;
|
||||||
alias T = ElementType!Range;
|
alias T = ElementType!Range;
|
||||||
|
|
||||||
static if (__VERSION__ >= 2083
|
static if (isDynamicArray!Range && __traits(isZeroInit, T))
|
||||||
&& isDynamicArray!Range
|
|
||||||
&& __traits(isZeroInit, T))
|
|
||||||
{
|
{
|
||||||
fill!0(range);
|
fill!0(range);
|
||||||
}
|
}
|
||||||
@ -244,54 +242,3 @@ if (isInputRange!Range && hasLvalueElements!Range)
|
|||||||
|
|
||||||
assert(counter == 2);
|
assert(counter == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Rotates the elements of a union of two ranges.
|
|
||||||
*
|
|
||||||
* Performs a left rotation on the given ranges, as if it would be a signle
|
|
||||||
* range, so that [`front.front`, `back.front`$(RPAREN) is a valid range, that
|
|
||||||
* is $(D_PARAM back) would continue $(D_PARAM front).
|
|
||||||
*
|
|
||||||
* The elements are moved so, that the first element of $(D_PARAM back) becomes
|
|
||||||
* the first element of $(D_PARAM front) without changing the relative order of
|
|
||||||
* their elements.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* Range = Range type.
|
|
||||||
* front = Left half.
|
|
||||||
* back = Right half.
|
|
||||||
*/
|
|
||||||
void rotate(Range)(Range front, Range back)
|
|
||||||
if (isForwardRange!Range && hasSwappableElements!Range)
|
|
||||||
{
|
|
||||||
auto next = back.save();
|
|
||||||
|
|
||||||
while (!front.empty && !next.empty && !sameHead(front, next))
|
|
||||||
{
|
|
||||||
tanya.memory.lifetime.swap(front.front, next.front);
|
|
||||||
front.popFront();
|
|
||||||
next.popFront();
|
|
||||||
|
|
||||||
if (next.empty)
|
|
||||||
{
|
|
||||||
next = back.save();
|
|
||||||
}
|
|
||||||
else if (front.empty)
|
|
||||||
{
|
|
||||||
front = back.save();
|
|
||||||
back = next.save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
|
||||||
@nogc nothrow pure @safe unittest
|
|
||||||
{
|
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
|
|
||||||
const int[7] expected = [1, 2, 3, 4, 5, 6, 7];
|
|
||||||
int[7] actual = [5, 6, 3, 4, 1, 2, 7];
|
|
||||||
|
|
||||||
rotate(actual[0 .. 2], actual[4 .. 6]);
|
|
||||||
assert(equal(actual[], expected[]));
|
|
||||||
}
|
|
||||||
|
@ -1,187 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event loop implementation for Linux.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/epoll.d,
|
|
||||||
* tanya/async/event/epoll.d)
|
|
||||||
*/
|
|
||||||
module tanya.async.event.epoll;
|
|
||||||
|
|
||||||
version (D_Ddoc)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else version (linux):
|
|
||||||
|
|
||||||
import core.stdc.errno;
|
|
||||||
public import core.sys.linux.epoll;
|
|
||||||
import core.sys.posix.unistd;
|
|
||||||
import core.time;
|
|
||||||
import std.algorithm.comparison;
|
|
||||||
import tanya.async.event.selector;
|
|
||||||
import tanya.async.loop;
|
|
||||||
import tanya.async.protocol;
|
|
||||||
import tanya.async.transport;
|
|
||||||
import tanya.async.watcher;
|
|
||||||
import tanya.container.array;
|
|
||||||
import tanya.memory.allocator;
|
|
||||||
import tanya.network.socket;
|
|
||||||
|
|
||||||
extern (C) nothrow @nogc
|
|
||||||
{
|
|
||||||
int epoll_create1(int flags);
|
|
||||||
int epoll_ctl (int epfd, int op, int fd, epoll_event *event);
|
|
||||||
int epoll_wait (int epfd, epoll_event *events, int maxevents, int timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
final class EpollLoop : SelectorLoop
|
|
||||||
{
|
|
||||||
protected int fd;
|
|
||||||
private Array!epoll_event events;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the loop.
|
|
||||||
*/
|
|
||||||
this() @nogc
|
|
||||||
{
|
|
||||||
if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
|
|
||||||
{
|
|
||||||
throw defaultAllocator.make!BadLoopException("epoll initialization failed");
|
|
||||||
}
|
|
||||||
super();
|
|
||||||
events = Array!epoll_event(maxEvents);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Frees loop internals.
|
|
||||||
*/
|
|
||||||
~this() @nogc
|
|
||||||
{
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Should be called if the backend configuration changes.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* watcher = Watcher.
|
|
||||||
* oldEvents = The events were already set.
|
|
||||||
* events = The events should be set.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
|
||||||
*/
|
|
||||||
protected override bool reify(SocketWatcher watcher,
|
|
||||||
EventMask oldEvents,
|
|
||||||
EventMask events) @nogc
|
|
||||||
{
|
|
||||||
int op = EPOLL_CTL_DEL;
|
|
||||||
epoll_event ev;
|
|
||||||
|
|
||||||
if (events == oldEvents)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (events && oldEvents)
|
|
||||||
{
|
|
||||||
op = EPOLL_CTL_MOD;
|
|
||||||
}
|
|
||||||
else if (events && !oldEvents)
|
|
||||||
{
|
|
||||||
op = EPOLL_CTL_ADD;
|
|
||||||
}
|
|
||||||
|
|
||||||
ev.data.fd = watcher.socket.handle;
|
|
||||||
ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0)
|
|
||||||
| (events & Event.write ? EPOLLOUT : 0)
|
|
||||||
| EPOLLET;
|
|
||||||
|
|
||||||
return epoll_ctl(fd, op, watcher.socket.handle, &ev) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does the actual polling.
|
|
||||||
*/
|
|
||||||
protected override void poll() @nogc
|
|
||||||
{
|
|
||||||
// Don't block
|
|
||||||
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
|
||||||
auto eventCount = epoll_wait(fd, events.get().ptr, maxEvents, timeout);
|
|
||||||
|
|
||||||
if (eventCount < 0)
|
|
||||||
{
|
|
||||||
if (errno != EINTR)
|
|
||||||
{
|
|
||||||
throw defaultAllocator.make!BadLoopException();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto i = 0; i < eventCount; ++i)
|
|
||||||
{
|
|
||||||
auto transport = cast(StreamTransport) connections[events[i].data.fd];
|
|
||||||
|
|
||||||
if (transport is null)
|
|
||||||
{
|
|
||||||
auto connection = cast(ConnectionWatcher) connections[events[i].data.fd];
|
|
||||||
assert(connection !is null);
|
|
||||||
|
|
||||||
acceptConnections(connection);
|
|
||||||
}
|
|
||||||
else if (events[i].events & EPOLLERR)
|
|
||||||
{
|
|
||||||
kill(transport);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if (events[i].events & (EPOLLIN | EPOLLPRI | EPOLLHUP))
|
|
||||||
{
|
|
||||||
SocketException exception;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ptrdiff_t received;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
received = transport.socket.receive(transport.output[]);
|
|
||||||
transport.output += received;
|
|
||||||
}
|
|
||||||
while (received);
|
|
||||||
}
|
|
||||||
catch (SocketException e)
|
|
||||||
{
|
|
||||||
exception = e;
|
|
||||||
}
|
|
||||||
if (transport.socket.disconnected)
|
|
||||||
{
|
|
||||||
kill(transport, exception);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if (transport.output.length)
|
|
||||||
{
|
|
||||||
pendings.insertBack(transport);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (events[i].events & EPOLLOUT)
|
|
||||||
{
|
|
||||||
transport.writeReady = true;
|
|
||||||
if (transport.input.length)
|
|
||||||
{
|
|
||||||
feed(transport);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: The blocking time.
|
|
||||||
*/
|
|
||||||
override protected @property inout(Duration) blockTime()
|
|
||||||
inout @safe pure nothrow
|
|
||||||
{
|
|
||||||
return min(super.blockTime, 1.dur!"seconds");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,390 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event loop implementation for Windows.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/iocp.d,
|
|
||||||
* tanya/async/event/iocp.d)
|
|
||||||
*/
|
|
||||||
module tanya.async.event.iocp;
|
|
||||||
|
|
||||||
version (D_Ddoc)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else version (Windows):
|
|
||||||
|
|
||||||
import core.sys.windows.mswsock;
|
|
||||||
import core.sys.windows.winsock2;
|
|
||||||
import tanya.async.loop;
|
|
||||||
import tanya.async.protocol;
|
|
||||||
import tanya.async.transport;
|
|
||||||
import tanya.async.watcher;
|
|
||||||
import tanya.container.buffer;
|
|
||||||
import tanya.memory.allocator;
|
|
||||||
import tanya.network.socket;
|
|
||||||
import tanya.sys.windows.winbase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transport for stream sockets.
|
|
||||||
*/
|
|
||||||
final class StreamTransport : SocketWatcher, DuplexTransport, SocketTransport
|
|
||||||
{
|
|
||||||
private SocketException exception;
|
|
||||||
|
|
||||||
private ReadBuffer!ubyte output;
|
|
||||||
|
|
||||||
private WriteBuffer!ubyte input;
|
|
||||||
|
|
||||||
private Protocol protocol_;
|
|
||||||
|
|
||||||
private bool closing;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates new completion port transport.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* socket = Socket.
|
|
||||||
*
|
|
||||||
* Precondition: $(D_INLINECODE socket !is null)
|
|
||||||
*/
|
|
||||||
this(OverlappedConnectedSocket socket) @nogc
|
|
||||||
{
|
|
||||||
super(socket);
|
|
||||||
output = ReadBuffer!ubyte(8192, 1024);
|
|
||||||
input = WriteBuffer!ubyte(8192);
|
|
||||||
active = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Socket.
|
|
||||||
*
|
|
||||||
* Postcondition: $(D_INLINECODE socket !is null)
|
|
||||||
*/
|
|
||||||
override @property OverlappedConnectedSocket socket() pure nothrow @safe @nogc
|
|
||||||
out (socket)
|
|
||||||
{
|
|
||||||
assert(socket !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
return cast(OverlappedConnectedSocket) socket_;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns $(D_PARAM true) if the transport is closing or closed.
|
|
||||||
*/
|
|
||||||
bool isClosing() const pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return closing;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the transport.
|
|
||||||
*
|
|
||||||
* Buffered data will be flushed. No more data will be received.
|
|
||||||
*/
|
|
||||||
void close() pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
closing = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write some data to the transport.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* data = Data to send.
|
|
||||||
*/
|
|
||||||
void write(ubyte[] data) @nogc
|
|
||||||
{
|
|
||||||
input ~= data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Application protocol.
|
|
||||||
*/
|
|
||||||
@property Protocol protocol() pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return protocol_;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Switches the protocol.
|
|
||||||
*
|
|
||||||
* The protocol is deallocated by the event loop.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* protocol = Application protocol.
|
|
||||||
*
|
|
||||||
* Precondition: $(D_INLINECODE protocol !is null)
|
|
||||||
*/
|
|
||||||
@property void protocol(Protocol protocol) pure nothrow @safe @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(protocol !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
protocol_ = protocol;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Invokes the watcher callback.
|
|
||||||
*/
|
|
||||||
override void invoke() @nogc
|
|
||||||
{
|
|
||||||
if (output.length)
|
|
||||||
{
|
|
||||||
immutable empty = input.length == 0;
|
|
||||||
protocol.received(output[0 .. $]);
|
|
||||||
output.clear();
|
|
||||||
if (empty)
|
|
||||||
{
|
|
||||||
SocketState overlapped;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
overlapped = defaultAllocator.make!SocketState;
|
|
||||||
socket.beginSend(input[], overlapped);
|
|
||||||
}
|
|
||||||
catch (SocketException e)
|
|
||||||
{
|
|
||||||
defaultAllocator.dispose(overlapped);
|
|
||||||
defaultAllocator.dispose(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
protocol.disconnected(exception);
|
|
||||||
defaultAllocator.dispose(protocol_);
|
|
||||||
defaultAllocator.dispose(exception);
|
|
||||||
active = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final class IOCPLoop : Loop
|
|
||||||
{
|
|
||||||
protected HANDLE completionPort;
|
|
||||||
|
|
||||||
protected OVERLAPPED overlap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the loop.
|
|
||||||
*/
|
|
||||||
this() @nogc
|
|
||||||
{
|
|
||||||
super();
|
|
||||||
|
|
||||||
completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, null, 0, 0);
|
|
||||||
if (!completionPort)
|
|
||||||
{
|
|
||||||
throw make!BadLoopException(defaultAllocator,
|
|
||||||
"Creating completion port failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Should be called if the backend configuration changes.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* watcher = Watcher.
|
|
||||||
* oldEvents = The events were already set.
|
|
||||||
* events = The events should be set.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
|
||||||
*/
|
|
||||||
override protected bool reify(SocketWatcher watcher,
|
|
||||||
EventMask oldEvents,
|
|
||||||
EventMask events) @nogc
|
|
||||||
{
|
|
||||||
SocketState overlapped;
|
|
||||||
if (!(oldEvents & Event.accept) && (events & Event.accept))
|
|
||||||
{
|
|
||||||
auto socket = cast(OverlappedStreamSocket) watcher.socket;
|
|
||||||
assert(socket !is null);
|
|
||||||
|
|
||||||
if (CreateIoCompletionPort(cast(HANDLE) socket.handle,
|
|
||||||
completionPort,
|
|
||||||
cast(size_t) (cast(void*) watcher),
|
|
||||||
0) !is completionPort)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
overlapped = defaultAllocator.make!SocketState;
|
|
||||||
socket.beginAccept(overlapped);
|
|
||||||
}
|
|
||||||
catch (SocketException e)
|
|
||||||
{
|
|
||||||
defaultAllocator.dispose(overlapped);
|
|
||||||
defaultAllocator.dispose(e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((!(oldEvents & Event.read) && (events & Event.read))
|
|
||||||
|| (!(oldEvents & Event.write) && (events & Event.write)))
|
|
||||||
{
|
|
||||||
auto transport = cast(StreamTransport) watcher;
|
|
||||||
assert(transport !is null);
|
|
||||||
|
|
||||||
if (CreateIoCompletionPort(cast(HANDLE) transport.socket.handle,
|
|
||||||
completionPort,
|
|
||||||
cast(size_t) (cast(void*) watcher),
|
|
||||||
0) !is completionPort)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Begin to read
|
|
||||||
if (!(oldEvents & Event.read) && (events & Event.read))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
overlapped = defaultAllocator.make!SocketState;
|
|
||||||
transport.socket.beginReceive(transport.output[], overlapped);
|
|
||||||
}
|
|
||||||
catch (SocketException e)
|
|
||||||
{
|
|
||||||
defaultAllocator.dispose(overlapped);
|
|
||||||
defaultAllocator.dispose(e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void kill(StreamTransport transport,
|
|
||||||
SocketException exception = null) @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(transport !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
transport.socket.shutdown();
|
|
||||||
defaultAllocator.dispose(transport.socket);
|
|
||||||
transport.exception = exception;
|
|
||||||
pendings.insertBack(transport);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does the actual polling.
|
|
||||||
*/
|
|
||||||
override protected void poll() @nogc
|
|
||||||
{
|
|
||||||
DWORD lpNumberOfBytes;
|
|
||||||
size_t key;
|
|
||||||
OVERLAPPED* overlap;
|
|
||||||
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
|
||||||
|
|
||||||
auto result = GetQueuedCompletionStatus(completionPort,
|
|
||||||
&lpNumberOfBytes,
|
|
||||||
&key,
|
|
||||||
&overlap,
|
|
||||||
timeout);
|
|
||||||
if (result == FALSE && overlap is null)
|
|
||||||
{
|
|
||||||
return; // Timeout
|
|
||||||
}
|
|
||||||
|
|
||||||
enum size_t offset = size_t.sizeof * 2;
|
|
||||||
auto overlapped = cast(SocketState) ((cast(void*) overlap) - offset);
|
|
||||||
assert(overlapped !is null);
|
|
||||||
scope (failure)
|
|
||||||
{
|
|
||||||
defaultAllocator.dispose(overlapped);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (overlapped.event)
|
|
||||||
{
|
|
||||||
case OverlappedSocketEvent.accept:
|
|
||||||
auto connection = cast(ConnectionWatcher) (cast(void*) key);
|
|
||||||
assert(connection !is null);
|
|
||||||
|
|
||||||
auto listener = cast(OverlappedStreamSocket) connection.socket;
|
|
||||||
assert(listener !is null);
|
|
||||||
|
|
||||||
auto socket = listener.endAccept(overlapped);
|
|
||||||
auto transport = defaultAllocator.make!StreamTransport(socket);
|
|
||||||
|
|
||||||
connection.incoming.insertBack(transport);
|
|
||||||
|
|
||||||
reify(transport,
|
|
||||||
EventMask(Event.none),
|
|
||||||
EventMask(Event.read | Event.write));
|
|
||||||
|
|
||||||
pendings.insertBack(connection);
|
|
||||||
listener.beginAccept(overlapped);
|
|
||||||
break;
|
|
||||||
case OverlappedSocketEvent.read:
|
|
||||||
auto transport = cast(StreamTransport) (cast(void*) key);
|
|
||||||
assert(transport !is null);
|
|
||||||
|
|
||||||
if (!transport.active)
|
|
||||||
{
|
|
||||||
defaultAllocator.dispose(transport);
|
|
||||||
defaultAllocator.dispose(overlapped);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int received;
|
|
||||||
SocketException exception;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
received = transport.socket.endReceive(overlapped);
|
|
||||||
}
|
|
||||||
catch (SocketException e)
|
|
||||||
{
|
|
||||||
exception = e;
|
|
||||||
}
|
|
||||||
if (transport.socket.disconnected)
|
|
||||||
{
|
|
||||||
// We want to get one last notification to destroy the watcher.
|
|
||||||
transport.socket.beginReceive(transport.output[], overlapped);
|
|
||||||
kill(transport, exception);
|
|
||||||
}
|
|
||||||
else if (received > 0)
|
|
||||||
{
|
|
||||||
immutable full = transport.output.free == received;
|
|
||||||
|
|
||||||
transport.output += received;
|
|
||||||
// Receive was interrupted because the buffer is full. We have to continue.
|
|
||||||
if (full)
|
|
||||||
{
|
|
||||||
transport.socket.beginReceive(transport.output[], overlapped);
|
|
||||||
}
|
|
||||||
pendings.insertBack(transport);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case OverlappedSocketEvent.write:
|
|
||||||
auto transport = cast(StreamTransport) (cast(void*) key);
|
|
||||||
assert(transport !is null);
|
|
||||||
|
|
||||||
transport.input += transport.socket.endSend(overlapped);
|
|
||||||
if (transport.input.length > 0)
|
|
||||||
{
|
|
||||||
transport.socket.beginSend(transport.input[], overlapped);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
transport.socket.beginReceive(transport.output[], overlapped);
|
|
||||||
if (transport.isClosing())
|
|
||||||
{
|
|
||||||
kill(transport);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
assert(false, "Unknown event");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,331 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Event loop implementation for *BSD.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/kqueue.d,
|
|
||||||
* tanya/async/event/kqueue.d)
|
|
||||||
*/
|
|
||||||
module tanya.async.event.kqueue;
|
|
||||||
|
|
||||||
version (D_Ddoc)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else version (OSX)
|
|
||||||
{
|
|
||||||
version = MacBSD;
|
|
||||||
}
|
|
||||||
else version (iOS)
|
|
||||||
{
|
|
||||||
version = MacBSD;
|
|
||||||
}
|
|
||||||
else version (TVOS)
|
|
||||||
{
|
|
||||||
version = MacBSD;
|
|
||||||
}
|
|
||||||
else version (WatchOS)
|
|
||||||
{
|
|
||||||
version = MacBSD;
|
|
||||||
}
|
|
||||||
else version (FreeBSD)
|
|
||||||
{
|
|
||||||
version = MacBSD;
|
|
||||||
}
|
|
||||||
else version (OpenBSD)
|
|
||||||
{
|
|
||||||
version = MacBSD;
|
|
||||||
}
|
|
||||||
else version (DragonFlyBSD)
|
|
||||||
{
|
|
||||||
version = MacBSD;
|
|
||||||
}
|
|
||||||
|
|
||||||
version (MacBSD):
|
|
||||||
|
|
||||||
import core.stdc.errno;
|
|
||||||
import core.sys.posix.time; // timespec
|
|
||||||
import core.sys.posix.unistd;
|
|
||||||
import core.time;
|
|
||||||
import tanya.algorithm.comparison;
|
|
||||||
import tanya.async.event.selector;
|
|
||||||
import tanya.async.loop;
|
|
||||||
import tanya.async.transport;
|
|
||||||
import tanya.async.watcher;
|
|
||||||
import tanya.container.array;
|
|
||||||
import tanya.memory.allocator;
|
|
||||||
import tanya.network.socket;
|
|
||||||
|
|
||||||
void EV_SET(kevent_t* kevp, typeof(kevent_t.tupleof) args) pure nothrow @nogc
|
|
||||||
{
|
|
||||||
*kevp = kevent_t(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
enum : short
|
|
||||||
{
|
|
||||||
EVFILT_READ = -1,
|
|
||||||
EVFILT_WRITE = -2,
|
|
||||||
EVFILT_AIO = -3, /* attached to aio requests */
|
|
||||||
EVFILT_VNODE = -4, /* attached to vnodes */
|
|
||||||
EVFILT_PROC = -5, /* attached to struct proc */
|
|
||||||
EVFILT_SIGNAL = -6, /* attached to struct proc */
|
|
||||||
EVFILT_TIMER = -7, /* timers */
|
|
||||||
EVFILT_MACHPORT = -8, /* Mach portsets */
|
|
||||||
EVFILT_FS = -9, /* filesystem events */
|
|
||||||
EVFILT_USER = -10, /* User events */
|
|
||||||
EVFILT_VM = -12, /* virtual memory events */
|
|
||||||
EVFILT_SYSCOUNT = 11
|
|
||||||
}
|
|
||||||
|
|
||||||
struct kevent_t
|
|
||||||
{
|
|
||||||
uintptr_t ident; // Identifier for this event
|
|
||||||
short filter; // Filter for event
|
|
||||||
ushort flags;
|
|
||||||
uint fflags;
|
|
||||||
intptr_t data;
|
|
||||||
void* udata; // Opaque user data identifier
|
|
||||||
}
|
|
||||||
|
|
||||||
enum
|
|
||||||
{
|
|
||||||
/* actions */
|
|
||||||
EV_ADD = 0x0001, /* add event to kq (implies enable) */
|
|
||||||
EV_DELETE = 0x0002, /* delete event from kq */
|
|
||||||
EV_ENABLE = 0x0004, /* enable event */
|
|
||||||
EV_DISABLE = 0x0008, /* disable event (not reported) */
|
|
||||||
|
|
||||||
/* flags */
|
|
||||||
EV_ONESHOT = 0x0010, /* only report one occurrence */
|
|
||||||
EV_CLEAR = 0x0020, /* clear event state after reporting */
|
|
||||||
EV_RECEIPT = 0x0040, /* force EV_ERROR on success, data=0 */
|
|
||||||
EV_DISPATCH = 0x0080, /* disable event after reporting */
|
|
||||||
|
|
||||||
EV_SYSFLAGS = 0xF000, /* reserved by system */
|
|
||||||
EV_FLAG1 = 0x2000, /* filter-specific flag */
|
|
||||||
|
|
||||||
/* returned values */
|
|
||||||
EV_EOF = 0x8000, /* EOF detected */
|
|
||||||
EV_ERROR = 0x4000, /* error, data contains errno */
|
|
||||||
}
|
|
||||||
|
|
||||||
extern(C) int kqueue() nothrow @nogc;
|
|
||||||
extern(C) int kevent(int kq, const kevent_t *changelist, int nchanges,
|
|
||||||
kevent_t *eventlist, int nevents, const timespec *timeout)
|
|
||||||
nothrow @nogc;
|
|
||||||
|
|
||||||
final class KqueueLoop : SelectorLoop
|
|
||||||
{
|
|
||||||
protected int fd;
|
|
||||||
private Array!kevent_t events;
|
|
||||||
private Array!kevent_t changes;
|
|
||||||
private size_t changeCount;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Maximal event count can be got at a time
|
|
||||||
* (should be supported by the backend).
|
|
||||||
*/
|
|
||||||
override protected @property uint maxEvents()
|
|
||||||
const pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return cast(uint) events.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
this() @nogc
|
|
||||||
{
|
|
||||||
super();
|
|
||||||
|
|
||||||
if ((fd = kqueue()) == -1)
|
|
||||||
{
|
|
||||||
throw make!BadLoopException(defaultAllocator,
|
|
||||||
"kqueue initialization failed");
|
|
||||||
}
|
|
||||||
events = Array!kevent_t(64);
|
|
||||||
changes = Array!kevent_t(64);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Frees loop internals.
|
|
||||||
*/
|
|
||||||
~this() @nogc
|
|
||||||
{
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void set(SocketType socket, short filter, ushort flags) @nogc
|
|
||||||
{
|
|
||||||
if (changes.length <= changeCount)
|
|
||||||
{
|
|
||||||
changes.length = changeCount + maxEvents;
|
|
||||||
}
|
|
||||||
EV_SET(&changes[changeCount],
|
|
||||||
cast(ulong) socket,
|
|
||||||
filter,
|
|
||||||
flags,
|
|
||||||
0U,
|
|
||||||
0,
|
|
||||||
null);
|
|
||||||
++changeCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Should be called if the backend configuration changes.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* watcher = Watcher.
|
|
||||||
* oldEvents = The events were already set.
|
|
||||||
* events = The events should be set.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
|
||||||
*/
|
|
||||||
override protected bool reify(SocketWatcher watcher,
|
|
||||||
EventMask oldEvents,
|
|
||||||
EventMask events) @nogc
|
|
||||||
{
|
|
||||||
if (events != oldEvents)
|
|
||||||
{
|
|
||||||
if (oldEvents & Event.read || oldEvents & Event.accept)
|
|
||||||
{
|
|
||||||
set(watcher.socket.handle, EVFILT_READ, EV_DELETE);
|
|
||||||
}
|
|
||||||
if (oldEvents & Event.write)
|
|
||||||
{
|
|
||||||
set(watcher.socket.handle, EVFILT_WRITE, EV_DELETE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (events & (Event.read | events & Event.accept))
|
|
||||||
{
|
|
||||||
set(watcher.socket.handle, EVFILT_READ, EV_ADD | EV_ENABLE);
|
|
||||||
}
|
|
||||||
if (events & Event.write)
|
|
||||||
{
|
|
||||||
set(watcher.socket.handle, EVFILT_WRITE, EV_ADD | EV_DISPATCH);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does the actual polling.
|
|
||||||
*/
|
|
||||||
protected override void poll() @nogc
|
|
||||||
{
|
|
||||||
timespec ts;
|
|
||||||
blockTime.split!("seconds", "nsecs")(ts.tv_sec, ts.tv_nsec);
|
|
||||||
|
|
||||||
if (changeCount > maxEvents)
|
|
||||||
{
|
|
||||||
events.length = changes.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto eventCount = kevent(fd,
|
|
||||||
changes.get().ptr,
|
|
||||||
cast(int) changeCount,
|
|
||||||
events.get().ptr,
|
|
||||||
maxEvents,
|
|
||||||
&ts);
|
|
||||||
changeCount = 0;
|
|
||||||
|
|
||||||
if (eventCount < 0)
|
|
||||||
{
|
|
||||||
if (errno != EINTR)
|
|
||||||
{
|
|
||||||
throw defaultAllocator.make!BadLoopException();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i; i < eventCount; ++i)
|
|
||||||
{
|
|
||||||
assert(connections.length > events[i].ident);
|
|
||||||
|
|
||||||
auto transport = cast(StreamTransport) connections[events[i].ident];
|
|
||||||
// If it is a ConnectionWatcher. Accept connections.
|
|
||||||
if (transport is null)
|
|
||||||
{
|
|
||||||
auto connection = cast(ConnectionWatcher) connections[events[i].ident];
|
|
||||||
assert(connection !is null);
|
|
||||||
|
|
||||||
acceptConnections(connection);
|
|
||||||
}
|
|
||||||
else if (events[i].flags & EV_ERROR)
|
|
||||||
{
|
|
||||||
kill(transport);
|
|
||||||
}
|
|
||||||
else if (events[i].filter == EVFILT_READ)
|
|
||||||
{
|
|
||||||
SocketException exception;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ptrdiff_t received;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
received = transport.socket.receive(transport.output[]);
|
|
||||||
transport.output += received;
|
|
||||||
}
|
|
||||||
while (received);
|
|
||||||
}
|
|
||||||
catch (SocketException e)
|
|
||||||
{
|
|
||||||
exception = e;
|
|
||||||
}
|
|
||||||
if (transport.socket.disconnected)
|
|
||||||
{
|
|
||||||
kill(transport, exception);
|
|
||||||
}
|
|
||||||
else if (transport.output.length)
|
|
||||||
{
|
|
||||||
pendings.insertBack(transport);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (events[i].filter == EVFILT_WRITE)
|
|
||||||
{
|
|
||||||
transport.writeReady = true;
|
|
||||||
if (transport.input.length)
|
|
||||||
{
|
|
||||||
feed(transport);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: The blocking time.
|
|
||||||
*/
|
|
||||||
override protected @property inout(Duration) blockTime()
|
|
||||||
inout @nogc @safe pure nothrow
|
|
||||||
{
|
|
||||||
return min(super.blockTime, 1.dur!"seconds");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* If the transport couldn't send the data, the further sending should
|
|
||||||
* be handled by the event loop.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* transport = Transport.
|
|
||||||
* exception = Exception thrown on sending.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
|
||||||
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
|
||||||
* transport will be destroyed then).
|
|
||||||
*/
|
|
||||||
protected override bool feed(StreamTransport transport,
|
|
||||||
SocketException exception = null) @nogc
|
|
||||||
{
|
|
||||||
if (!super.feed(transport, exception))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!transport.writeReady)
|
|
||||||
{
|
|
||||||
set(transport.socket.handle, EVFILT_WRITE, EV_DISPATCH);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,407 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This module contains base implementations for reactor event loops.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/selector.d,
|
|
||||||
* tanya/async/event/selector.d)
|
|
||||||
*/
|
|
||||||
module tanya.async.event.selector;
|
|
||||||
|
|
||||||
version (D_Ddoc)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else version (Posix):
|
|
||||||
|
|
||||||
import tanya.async.loop;
|
|
||||||
import tanya.async.protocol;
|
|
||||||
import tanya.async.transport;
|
|
||||||
import tanya.async.watcher;
|
|
||||||
import tanya.container.array;
|
|
||||||
import tanya.container.buffer;
|
|
||||||
import tanya.memory.allocator;
|
|
||||||
import tanya.network.socket;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transport for stream sockets.
|
|
||||||
*/
|
|
||||||
package class StreamTransport : SocketWatcher, DuplexTransport, SocketTransport
|
|
||||||
{
|
|
||||||
private SelectorLoop loop;
|
|
||||||
|
|
||||||
private SocketException exception;
|
|
||||||
|
|
||||||
package ReadBuffer!ubyte output;
|
|
||||||
|
|
||||||
package WriteBuffer!ubyte input;
|
|
||||||
|
|
||||||
private Protocol protocol_;
|
|
||||||
|
|
||||||
private bool closing;
|
|
||||||
|
|
||||||
/// Received notification that the underlying socket is write-ready.
|
|
||||||
package bool writeReady;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Params:
|
|
||||||
* loop = Event loop.
|
|
||||||
* socket = Socket.
|
|
||||||
*
|
|
||||||
* Precondition: $(D_INLINECODE loop !is null && socket !is null)
|
|
||||||
*/
|
|
||||||
this(SelectorLoop loop, ConnectedSocket socket) @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(loop !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
super(socket);
|
|
||||||
this.loop = loop;
|
|
||||||
output = ReadBuffer!ubyte(8192, 1024);
|
|
||||||
input = WriteBuffer!ubyte(8192);
|
|
||||||
active = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Socket.
|
|
||||||
*
|
|
||||||
* Postcondition: $(D_INLINECODE socket !is null)
|
|
||||||
*/
|
|
||||||
override @property ConnectedSocket socket() pure nothrow @safe @nogc
|
|
||||||
out (socket)
|
|
||||||
{
|
|
||||||
assert(socket !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
return cast(ConnectedSocket) socket_;
|
|
||||||
}
|
|
||||||
|
|
||||||
private @property void socket(ConnectedSocket socket)
|
|
||||||
pure nothrow @safe @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(socket !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
socket_ = socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Application protocol.
|
|
||||||
*/
|
|
||||||
@property Protocol protocol() pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return protocol_;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Switches the protocol.
|
|
||||||
*
|
|
||||||
* The protocol is deallocated by the event loop.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* protocol = Application protocol.
|
|
||||||
*
|
|
||||||
* Precondition: $(D_INLINECODE protocol !is null)
|
|
||||||
*/
|
|
||||||
@property void protocol(Protocol protocol) pure nothrow @safe @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(protocol !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
protocol_ = protocol;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns $(D_PARAM true) if the transport is closing or closed.
|
|
||||||
*/
|
|
||||||
bool isClosing() const pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return closing;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the transport.
|
|
||||||
*
|
|
||||||
* Buffered data will be flushed. No more data will be received.
|
|
||||||
*/
|
|
||||||
void close() @nogc
|
|
||||||
{
|
|
||||||
closing = true;
|
|
||||||
loop.reify(this,
|
|
||||||
EventMask(Event.read | Event.write),
|
|
||||||
EventMask(Event.write));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Invokes the watcher callback.
|
|
||||||
*/
|
|
||||||
override void invoke() @nogc
|
|
||||||
{
|
|
||||||
if (output.length)
|
|
||||||
{
|
|
||||||
protocol.received(output[0 .. $]);
|
|
||||||
output.clear();
|
|
||||||
if (isClosing() && input.length == 0)
|
|
||||||
{
|
|
||||||
loop.kill(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
protocol.disconnected(exception);
|
|
||||||
defaultAllocator.dispose(protocol_);
|
|
||||||
defaultAllocator.dispose(exception);
|
|
||||||
active = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write some data to the transport.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* data = Data to send.
|
|
||||||
*/
|
|
||||||
void write(ubyte[] data) @nogc
|
|
||||||
{
|
|
||||||
if (!data.length)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Try to write if the socket is write ready.
|
|
||||||
if (writeReady)
|
|
||||||
{
|
|
||||||
ptrdiff_t sent;
|
|
||||||
SocketException exception;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
sent = socket.send(data);
|
|
||||||
if (sent == 0)
|
|
||||||
{
|
|
||||||
writeReady = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (SocketException e)
|
|
||||||
{
|
|
||||||
writeReady = false;
|
|
||||||
exception = e;
|
|
||||||
}
|
|
||||||
if (sent < data.length)
|
|
||||||
{
|
|
||||||
input ~= data[sent..$];
|
|
||||||
loop.feed(this, exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
input ~= data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class SelectorLoop : Loop
|
|
||||||
{
|
|
||||||
/// Pending connections.
|
|
||||||
protected Array!SocketWatcher connections;
|
|
||||||
|
|
||||||
this() @nogc
|
|
||||||
{
|
|
||||||
super();
|
|
||||||
this.connections = Array!SocketWatcher(maxEvents);
|
|
||||||
}
|
|
||||||
|
|
||||||
~this() @nogc
|
|
||||||
{
|
|
||||||
foreach (ref connection; this.connections[])
|
|
||||||
{
|
|
||||||
// We want to free only the transports. ConnectionWatcher are
|
|
||||||
// created by the user and should be freed by himself.
|
|
||||||
if (cast(StreamTransport) connection !is null)
|
|
||||||
{
|
|
||||||
defaultAllocator.dispose(connection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Should be called if the backend configuration changes.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* watcher = Watcher.
|
|
||||||
* oldEvents = The events were already set.
|
|
||||||
* events = The events should be set.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
|
||||||
*/
|
|
||||||
override abstract protected bool reify(SocketWatcher watcher,
|
|
||||||
EventMask oldEvents,
|
|
||||||
EventMask events) @nogc;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Kills the watcher and closes the connection.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* transport = Transport.
|
|
||||||
* exception = Occurred exception.
|
|
||||||
*/
|
|
||||||
protected void kill(StreamTransport transport,
|
|
||||||
SocketException exception = null) @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(transport !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
transport.socket.shutdown();
|
|
||||||
defaultAllocator.dispose(transport.socket);
|
|
||||||
transport.exception = exception;
|
|
||||||
pendings.insertBack(transport);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* If the transport couldn't send the data, the further sending should
|
|
||||||
* be handled by the event loop.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* transport = Transport.
|
|
||||||
* exception = Exception thrown on sending.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
|
||||||
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
|
||||||
* transport will be destroyed then).
|
|
||||||
*/
|
|
||||||
protected bool feed(StreamTransport transport,
|
|
||||||
SocketException exception = null) @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(transport !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
while (transport.input.length && transport.writeReady)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ptrdiff_t sent = transport.socket.send(transport.input[]);
|
|
||||||
if (sent == 0)
|
|
||||||
{
|
|
||||||
transport.writeReady = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
transport.input += sent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (SocketException e)
|
|
||||||
{
|
|
||||||
exception = e;
|
|
||||||
transport.writeReady = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (exception !is null)
|
|
||||||
{
|
|
||||||
kill(transport, exception);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (transport.input.length == 0 && transport.isClosing())
|
|
||||||
{
|
|
||||||
kill(transport);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start watching.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* watcher = Watcher.
|
|
||||||
*/
|
|
||||||
override void start(ConnectionWatcher watcher) @nogc
|
|
||||||
{
|
|
||||||
if (watcher.active)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (connections.length <= watcher.socket)
|
|
||||||
{
|
|
||||||
connections.length = watcher.socket.handle + maxEvents / 2;
|
|
||||||
}
|
|
||||||
connections[watcher.socket.handle] = watcher;
|
|
||||||
|
|
||||||
super.start(watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Accept incoming connections.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* connection = Connection watcher ready to accept.
|
|
||||||
*/
|
|
||||||
package void acceptConnections(ConnectionWatcher connection) @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(connection !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
ConnectedSocket client;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
client = (cast(StreamSocket) connection.socket).accept();
|
|
||||||
}
|
|
||||||
catch (SocketException e)
|
|
||||||
{
|
|
||||||
defaultAllocator.dispose(e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (client is null)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamTransport transport;
|
|
||||||
|
|
||||||
if (connections.length > client.handle)
|
|
||||||
{
|
|
||||||
transport = cast(StreamTransport) connections[client.handle];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
connections.length = client.handle + maxEvents / 2;
|
|
||||||
}
|
|
||||||
if (transport is null)
|
|
||||||
{
|
|
||||||
transport = defaultAllocator.make!StreamTransport(this, client);
|
|
||||||
connections[client.handle] = transport;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
transport.socket = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
reify(transport,
|
|
||||||
EventMask(Event.none),
|
|
||||||
EventMask(Event.read | Event.write));
|
|
||||||
connection.incoming.insertBack(transport);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!connection.incoming.empty)
|
|
||||||
{
|
|
||||||
pendings.insertBack(connection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This module provides API for Windows I/O Completion Ports.
|
|
||||||
*
|
|
||||||
* Note: Available only on Windows.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/iocp.d,
|
|
||||||
* tanya/async/iocp.d)
|
|
||||||
*/
|
|
||||||
module tanya.async.iocp;
|
|
||||||
|
|
||||||
version (Windows)
|
|
||||||
{
|
|
||||||
version = WindowsDoc;
|
|
||||||
}
|
|
||||||
else version (D_Ddoc)
|
|
||||||
{
|
|
||||||
version = WindowsDoc;
|
|
||||||
version (Windows)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
private struct OVERLAPPED
|
|
||||||
{
|
|
||||||
}
|
|
||||||
private alias HANDLE = void*;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
version (WindowsDoc):
|
|
||||||
|
|
||||||
import core.sys.windows.winbase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides an extendable representation of a Win32 $(D_PSYMBOL OVERLAPPED)
|
|
||||||
* structure.
|
|
||||||
*/
|
|
||||||
class State
|
|
||||||
{
|
|
||||||
/// For internal use by Windows API.
|
|
||||||
align(1) OVERLAPPED overlapped;
|
|
||||||
|
|
||||||
/// File/socket handle.
|
|
||||||
HANDLE handle;
|
|
||||||
|
|
||||||
/// For keeping events or event masks.
|
|
||||||
int event;
|
|
||||||
}
|
|
@ -1,352 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for the event loop implementations and the default event loop
|
|
||||||
* chooser.
|
|
||||||
*
|
|
||||||
* ---
|
|
||||||
* import tanya.async;
|
|
||||||
* import tanya.memory;
|
|
||||||
* import tanya.network.socket;
|
|
||||||
*
|
|
||||||
* class EchoProtocol : TransmissionControlProtocol
|
|
||||||
* {
|
|
||||||
* private DuplexTransport transport;
|
|
||||||
*
|
|
||||||
* void received(in ubyte[] data) @nogc
|
|
||||||
* {
|
|
||||||
* ubyte[512] buffer;
|
|
||||||
* buffer[0 .. data.length] = data;
|
|
||||||
* transport.write(buffer[]);
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
* void connected(DuplexTransport transport) @nogc
|
|
||||||
* {
|
|
||||||
* this.transport = transport;
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
* void disconnected(SocketException e) @nogc
|
|
||||||
* {
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
* void main()
|
|
||||||
* {
|
|
||||||
* auto address = defaultAllocator.make!InternetAddress("127.0.0.1", cast(ushort) 8192);
|
|
||||||
*
|
|
||||||
* version (Windows)
|
|
||||||
* {
|
|
||||||
* auto sock = defaultAllocator.make!OverlappedStreamSocket(AddressFamily.inet);
|
|
||||||
* }
|
|
||||||
* else
|
|
||||||
* {
|
|
||||||
* auto sock = defaultAllocator.make!StreamSocket(AddressFamily.inet);
|
|
||||||
* sock.blocking = false;
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
* sock.bind(address);
|
|
||||||
* sock.listen(5);
|
|
||||||
*
|
|
||||||
* auto io = defaultAllocator.make!ConnectionWatcher(sock);
|
|
||||||
* io.setProtocol!EchoProtocol;
|
|
||||||
*
|
|
||||||
* defaultLoop.start(io);
|
|
||||||
* defaultLoop.run();
|
|
||||||
*
|
|
||||||
* sock.shutdown();
|
|
||||||
* defaultAllocator.dispose(io);
|
|
||||||
* defaultAllocator.dispose(sock);
|
|
||||||
* defaultAllocator.dispose(address);
|
|
||||||
* }
|
|
||||||
* ---
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/loop.d,
|
|
||||||
* tanya/async/loop.d)
|
|
||||||
*/
|
|
||||||
module tanya.async.loop;
|
|
||||||
|
|
||||||
import core.time;
|
|
||||||
import tanya.async.transport;
|
|
||||||
import tanya.async.watcher;
|
|
||||||
import tanya.bitmanip;
|
|
||||||
import tanya.container.buffer;
|
|
||||||
import tanya.container.list;
|
|
||||||
import tanya.memory.allocator;
|
|
||||||
import tanya.network.socket;
|
|
||||||
|
|
||||||
version (DisableBackends)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else version (D_Ddoc)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else version (linux)
|
|
||||||
{
|
|
||||||
import tanya.async.event.epoll;
|
|
||||||
version = Epoll;
|
|
||||||
}
|
|
||||||
else version (Windows)
|
|
||||||
{
|
|
||||||
import tanya.async.event.iocp;
|
|
||||||
version = IOCP;
|
|
||||||
}
|
|
||||||
else version (OSX)
|
|
||||||
{
|
|
||||||
version = Kqueue;
|
|
||||||
}
|
|
||||||
else version (iOS)
|
|
||||||
{
|
|
||||||
version = Kqueue;
|
|
||||||
}
|
|
||||||
else version (FreeBSD)
|
|
||||||
{
|
|
||||||
version = Kqueue;
|
|
||||||
}
|
|
||||||
else version (OpenBSD)
|
|
||||||
{
|
|
||||||
version = Kqueue;
|
|
||||||
}
|
|
||||||
else version (DragonFlyBSD)
|
|
||||||
{
|
|
||||||
version = Kqueue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Events.
|
|
||||||
*/
|
|
||||||
enum Event : uint
|
|
||||||
{
|
|
||||||
none = 0x00, /// No events.
|
|
||||||
read = 0x01, /// Non-blocking read call.
|
|
||||||
write = 0x02, /// Non-blocking write call.
|
|
||||||
accept = 0x04, /// Connection made.
|
|
||||||
error = 0x80000000, /// Sent when an error occurs.
|
|
||||||
}
|
|
||||||
|
|
||||||
alias EventMask = BitFlags!Event;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event loop.
|
|
||||||
*/
|
|
||||||
abstract class Loop
|
|
||||||
{
|
|
||||||
protected bool done = true;
|
|
||||||
|
|
||||||
/// Pending watchers.
|
|
||||||
protected DList!Watcher pendings;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Maximal event count can be got at a time
|
|
||||||
* (should be supported by the backend).
|
|
||||||
*/
|
|
||||||
protected @property uint maxEvents()
|
|
||||||
const pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return 128U;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes the loop.
|
|
||||||
*/
|
|
||||||
this() @nogc
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Frees loop internals.
|
|
||||||
*/
|
|
||||||
~this() @nogc
|
|
||||||
{
|
|
||||||
for (; !this.pendings.empty; this.pendings.removeFront())
|
|
||||||
{
|
|
||||||
defaultAllocator.dispose(this.pendings.front);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts the loop.
|
|
||||||
*/
|
|
||||||
void run() @nogc
|
|
||||||
{
|
|
||||||
this.done = false;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
poll();
|
|
||||||
|
|
||||||
// Invoke pendings
|
|
||||||
for (; !this.pendings.empty; this.pendings.removeFront())
|
|
||||||
{
|
|
||||||
this.pendings.front.invoke();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (!this.done);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Break out of the loop.
|
|
||||||
*/
|
|
||||||
void unloop() @safe pure nothrow @nogc
|
|
||||||
{
|
|
||||||
this.done = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start watching.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* watcher = Watcher.
|
|
||||||
*/
|
|
||||||
void start(ConnectionWatcher watcher) @nogc
|
|
||||||
{
|
|
||||||
if (watcher.active)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
watcher.active = true;
|
|
||||||
|
|
||||||
reify(watcher, EventMask(Event.none), EventMask(Event.accept));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop watching.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* watcher = Watcher.
|
|
||||||
*/
|
|
||||||
void stop(ConnectionWatcher watcher) @nogc
|
|
||||||
{
|
|
||||||
if (!watcher.active)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
watcher.active = false;
|
|
||||||
|
|
||||||
reify(watcher, EventMask(Event.accept), EventMask(Event.none));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Should be called if the backend configuration changes.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* watcher = Watcher.
|
|
||||||
* oldEvents = The events were already set.
|
|
||||||
* events = The events should be set.
|
|
||||||
*
|
|
||||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
|
||||||
*/
|
|
||||||
abstract protected bool reify(SocketWatcher watcher,
|
|
||||||
EventMask oldEvents,
|
|
||||||
EventMask events) @nogc;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: The blocking time.
|
|
||||||
*/
|
|
||||||
protected @property inout(Duration) blockTime()
|
|
||||||
inout @safe pure nothrow @nogc
|
|
||||||
{
|
|
||||||
// Don't block if we have to do.
|
|
||||||
return pendings.empty ? blockTime_ : Duration.zero;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the blocking time for IO watchers.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* blockTime = The blocking time. Cannot be larger than
|
|
||||||
* $(D_PSYMBOL maxBlockTime).
|
|
||||||
*/
|
|
||||||
protected @property void blockTime(in Duration blockTime) @safe pure nothrow @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(blockTime <= 1.dur!"hours", "Too long to wait.");
|
|
||||||
assert(!blockTime.isNegative);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
blockTime_ = blockTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Does the actual polling.
|
|
||||||
*/
|
|
||||||
abstract protected void poll() @nogc;
|
|
||||||
|
|
||||||
/// Maximal block time.
|
|
||||||
protected Duration blockTime_ = 1.dur!"minutes";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exception thrown on errors in the event loop.
|
|
||||||
*/
|
|
||||||
class BadLoopException : Exception
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Params:
|
|
||||||
* file = The file where the exception occurred.
|
|
||||||
* line = The line number where the exception occurred.
|
|
||||||
* next = The previous exception in the chain of exceptions, if any.
|
|
||||||
*/
|
|
||||||
this(string file = __FILE__, size_t line = __LINE__, Throwable next = null)
|
|
||||||
pure nothrow const @safe @nogc
|
|
||||||
{
|
|
||||||
super("Event loop cannot be initialized.", file, line, next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the event loop used by default. If an event loop wasn't set with
|
|
||||||
* $(D_PSYMBOL defaultLoop) before, $(D_PSYMBOL defaultLoop) will try to
|
|
||||||
* choose an event loop supported on the system.
|
|
||||||
*
|
|
||||||
* Returns: The default event loop.
|
|
||||||
*/
|
|
||||||
@property Loop defaultLoop() @nogc
|
|
||||||
{
|
|
||||||
if (defaultLoop_ !is null)
|
|
||||||
{
|
|
||||||
return defaultLoop_;
|
|
||||||
}
|
|
||||||
version (Epoll)
|
|
||||||
{
|
|
||||||
defaultLoop_ = defaultAllocator.make!EpollLoop;
|
|
||||||
}
|
|
||||||
else version (IOCP)
|
|
||||||
{
|
|
||||||
defaultLoop_ = defaultAllocator.make!IOCPLoop;
|
|
||||||
}
|
|
||||||
else version (Kqueue)
|
|
||||||
{
|
|
||||||
import tanya.async.event.kqueue;
|
|
||||||
defaultLoop_ = defaultAllocator.make!KqueueLoop;
|
|
||||||
}
|
|
||||||
return defaultLoop_;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the default event loop.
|
|
||||||
*
|
|
||||||
* This property makes it possible to implement your own backends or event
|
|
||||||
* loops, for example, if the system is not supported or if you want to
|
|
||||||
* extend the supported implementation. Just extend $(D_PSYMBOL Loop) and pass
|
|
||||||
* your implementation to this property.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* loop = The event loop.
|
|
||||||
*/
|
|
||||||
@property void defaultLoop(Loop loop) @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(loop !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
defaultLoop_ = loop;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Loop defaultLoop_;
|
|
@ -1,20 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This package provides asynchronous capabilities.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/package.d,
|
|
||||||
* tanya/async/package.d)
|
|
||||||
*/
|
|
||||||
module tanya.async;
|
|
||||||
|
|
||||||
public import tanya.async.loop;
|
|
||||||
public import tanya.async.protocol;
|
|
||||||
public import tanya.async.transport;
|
|
||||||
public import tanya.async.watcher;
|
|
@ -1,58 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This module contains protocol which handle data in asynchronous
|
|
||||||
* applications.
|
|
||||||
*
|
|
||||||
* When an event from the network arrives, a protocol method gets
|
|
||||||
* called and can respond to the event.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/protocol.d,
|
|
||||||
* tanya/async/protocol.d)
|
|
||||||
*/
|
|
||||||
module tanya.async.protocol;
|
|
||||||
|
|
||||||
import tanya.async.transport;
|
|
||||||
import tanya.network.socket;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Common protocol interface.
|
|
||||||
*/
|
|
||||||
interface Protocol
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Params:
|
|
||||||
* data = Read data.
|
|
||||||
*/
|
|
||||||
void received(in ubyte[] data) @nogc;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when a connection is made.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* transport = Protocol transport.
|
|
||||||
*/
|
|
||||||
void connected(DuplexTransport transport) @nogc;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when a connection is lost.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* exception = $(D_PSYMBOL Exception) if an error caused
|
|
||||||
* the disconnect, $(D_KEYWORD null) otherwise.
|
|
||||||
*/
|
|
||||||
void disconnected(SocketException exception) @nogc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for TCP.
|
|
||||||
*/
|
|
||||||
interface TransmissionControlProtocol : Protocol
|
|
||||||
{
|
|
||||||
}
|
|
@ -1,104 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This module contains transports which are responsible for data dilvery
|
|
||||||
* between two parties of an asynchronous communication.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/transport.d,
|
|
||||||
* tanya/async/transport.d)
|
|
||||||
*/
|
|
||||||
module tanya.async.transport;
|
|
||||||
|
|
||||||
import tanya.async.protocol;
|
|
||||||
import tanya.network.socket;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Base transport interface.
|
|
||||||
*/
|
|
||||||
interface Transport
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for read-only transports.
|
|
||||||
*/
|
|
||||||
interface ReadTransport : Transport
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for write-only transports.
|
|
||||||
*/
|
|
||||||
interface WriteTransport : Transport
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Write some data to the transport.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* data = Data to send.
|
|
||||||
*/
|
|
||||||
void write(ubyte[] data) @nogc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a bidirectional transport.
|
|
||||||
*/
|
|
||||||
interface DuplexTransport : ReadTransport, WriteTransport
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Returns: Application protocol.
|
|
||||||
*
|
|
||||||
* Postcondition: $(D_INLINECODE protocol !is null)
|
|
||||||
*/
|
|
||||||
@property Protocol protocol() pure nothrow @safe @nogc
|
|
||||||
out (protocol)
|
|
||||||
{
|
|
||||||
assert(protocol !is null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Switches the protocol.
|
|
||||||
*
|
|
||||||
* The protocol is deallocated by the event loop.
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* protocol = Application protocol.
|
|
||||||
*
|
|
||||||
* Precondition: $(D_INLINECODE protocol !is null)
|
|
||||||
*/
|
|
||||||
@property void protocol(Protocol protocol) pure nothrow @safe @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(protocol !is null);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns $(D_PARAM true) if the transport is closing or closed.
|
|
||||||
*/
|
|
||||||
bool isClosing() const pure nothrow @safe @nogc;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the transport.
|
|
||||||
*
|
|
||||||
* Buffered data will be flushed. No more data will be received.
|
|
||||||
*/
|
|
||||||
void close() @nogc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a socket transport.
|
|
||||||
*/
|
|
||||||
interface SocketTransport : Transport
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Returns: Socket.
|
|
||||||
*/
|
|
||||||
@property Socket socket() pure nothrow @safe @nogc;
|
|
||||||
}
|
|
@ -1,117 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Watchers register user's interest in some event.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/watcher.d,
|
|
||||||
* tanya/async/watcher.d)
|
|
||||||
*/
|
|
||||||
module tanya.async.watcher;
|
|
||||||
|
|
||||||
import tanya.async.loop;
|
|
||||||
import tanya.async.protocol;
|
|
||||||
import tanya.async.transport;
|
|
||||||
import tanya.container.buffer;
|
|
||||||
import tanya.container.list;
|
|
||||||
import tanya.memory.allocator;
|
|
||||||
import tanya.network.socket;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A watcher is an opaque structure that you allocate and register to record
|
|
||||||
* your interest in some event.
|
|
||||||
*/
|
|
||||||
abstract class Watcher
|
|
||||||
{
|
|
||||||
/// Whether the watcher is active.
|
|
||||||
bool active;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Invoke some action on event.
|
|
||||||
*/
|
|
||||||
void invoke() @nogc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Socket watcher.
|
|
||||||
*/
|
|
||||||
abstract class SocketWatcher : Watcher
|
|
||||||
{
|
|
||||||
/// Watched socket.
|
|
||||||
protected Socket socket_;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Params:
|
|
||||||
* socket = Socket.
|
|
||||||
*
|
|
||||||
* Precondition: $(D_INLINECODE socket !is null)
|
|
||||||
*/
|
|
||||||
this(Socket socket) pure nothrow @safe @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(socket !is null);
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
socket_ = socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns: Socket.
|
|
||||||
*/
|
|
||||||
@property Socket socket() pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return socket_;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connection watcher.
|
|
||||||
*/
|
|
||||||
class ConnectionWatcher : SocketWatcher
|
|
||||||
{
|
|
||||||
/// Incoming connection queue.
|
|
||||||
DList!DuplexTransport incoming;
|
|
||||||
|
|
||||||
private Protocol delegate() @nogc protocolFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Params:
|
|
||||||
* socket = Socket.
|
|
||||||
*/
|
|
||||||
this(Socket socket) @nogc
|
|
||||||
{
|
|
||||||
super(socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Params:
|
|
||||||
* P = Protocol should be used.
|
|
||||||
*/
|
|
||||||
void setProtocol(P : Protocol)() @nogc
|
|
||||||
{
|
|
||||||
this.protocolFactory = () @nogc => cast(Protocol) defaultAllocator.make!P;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Invokes new connection callback.
|
|
||||||
*/
|
|
||||||
override void invoke() @nogc
|
|
||||||
in
|
|
||||||
{
|
|
||||||
assert(protocolFactory !is null, "Protocol isn't set.");
|
|
||||||
}
|
|
||||||
do
|
|
||||||
{
|
|
||||||
for (; !this.incoming.empty; this.incoming.removeFront())
|
|
||||||
{
|
|
||||||
this.incoming.front.protocol = protocolFactory();
|
|
||||||
this.incoming.front.protocol.connected(this.incoming.front);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,6 +17,7 @@ module tanya.container.array;
|
|||||||
import core.checkedint;
|
import core.checkedint;
|
||||||
import std.algorithm.comparison;
|
import std.algorithm.comparison;
|
||||||
import std.algorithm.iteration;
|
import std.algorithm.iteration;
|
||||||
|
import std.algorithm.mutation : bringToFront;
|
||||||
import tanya.algorithm.mutation;
|
import tanya.algorithm.mutation;
|
||||||
import tanya.memory.allocator;
|
import tanya.memory.allocator;
|
||||||
import tanya.memory.lifetime;
|
import tanya.memory.lifetime;
|
||||||
@ -815,7 +816,7 @@ struct Array(T)
|
|||||||
const after = r.end - this.data;
|
const after = r.end - this.data;
|
||||||
const inserted = insertBack(el);
|
const inserted = insertBack(el);
|
||||||
|
|
||||||
rotate(this.data[after .. oldLength], this.data[oldLength .. length]);
|
bringToFront(this.data[after .. oldLength], this.data[oldLength .. length]);
|
||||||
return inserted;
|
return inserted;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -854,7 +855,7 @@ struct Array(T)
|
|||||||
{
|
{
|
||||||
moveBack(el);
|
moveBack(el);
|
||||||
}
|
}
|
||||||
rotate(this.data[offset .. oldLen], this.data[oldLen .. length]);
|
bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -910,7 +911,7 @@ struct Array(T)
|
|||||||
{
|
{
|
||||||
moveBack(el);
|
moveBack(el);
|
||||||
}
|
}
|
||||||
rotate(this.data[offset .. oldLen], this.data[oldLen .. length]);
|
bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
module tanya.container.string;
|
module tanya.container.string;
|
||||||
|
|
||||||
import std.algorithm.comparison;
|
import std.algorithm.comparison;
|
||||||
|
import std.algorithm.mutation : bringToFront;
|
||||||
import tanya.algorithm.mutation;
|
import tanya.algorithm.mutation;
|
||||||
import tanya.hash.lookup;
|
import tanya.hash.lookup;
|
||||||
import tanya.memory.allocator;
|
import tanya.memory.allocator;
|
||||||
@ -1488,7 +1489,7 @@ struct String
|
|||||||
const after = r.end - this.data;
|
const after = r.end - this.data;
|
||||||
const inserted = insertBack(el);
|
const inserted = insertBack(el);
|
||||||
|
|
||||||
rotate(this.data[after .. oldLength], this.data[oldLength .. length]);
|
bringToFront(this.data[after .. oldLength], this.data[oldLength .. length]);
|
||||||
return inserted;
|
return inserted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,3 +142,23 @@ String indexToName(uint index) @nogc nothrow @trusted
|
|||||||
return String(findNullTerminated(buffer));
|
return String(findNullTerminated(buffer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* $(D_PSYMBOL AddressFamily) specifies a communication domain; this selects
|
||||||
|
* the protocol family which will be used for communication.
|
||||||
|
*/
|
||||||
|
enum AddressFamily : int
|
||||||
|
{
|
||||||
|
unspec = 0, /// Unspecified.
|
||||||
|
local = 1, /// Local to host (pipes and file-domain).
|
||||||
|
unix = local, /// POSIX name for PF_LOCAL.
|
||||||
|
inet = 2, /// IP protocol family.
|
||||||
|
ax25 = 3, /// Amateur Radio AX.25.
|
||||||
|
ipx = 4, /// Novell Internet Protocol.
|
||||||
|
appletalk = 5, /// Appletalk DDP.
|
||||||
|
netrom = 6, /// Amateur radio NetROM.
|
||||||
|
bridge = 7, /// Multiprotocol bridge.
|
||||||
|
atmpvc = 8, /// ATM PVCs.
|
||||||
|
x25 = 9, /// Reserved for X.25 project.
|
||||||
|
inet6 = 10, /// IP version 6.
|
||||||
|
}
|
||||||
|
@ -1324,3 +1324,98 @@ struct Address
|
|||||||
assert(address == Address4.loopback);
|
assert(address == Address4.loopback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service endpoint specified by a version independent IP address and port.
|
||||||
|
*/
|
||||||
|
struct Endpoint
|
||||||
|
{
|
||||||
|
private AddressFamily family = AddressFamily.unspec;
|
||||||
|
private ubyte[ushort.sizeof] service;
|
||||||
|
private Address4 address4; // Unused sin6_flowinfo if IPv6
|
||||||
|
private Address6 address6; // Unused if IPv4
|
||||||
|
|
||||||
|
static assert(Address4.sizeof == 4);
|
||||||
|
|
||||||
|
/// Allows the system to select a free port.
|
||||||
|
enum ushort anyPort = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an endpoint.
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* T = Address type (IPv4 or IPv6).
|
||||||
|
* address = IP address that should be associated with the endpoint.
|
||||||
|
* port = Port number in network byte order.
|
||||||
|
*/
|
||||||
|
this(T)(T address, const ushort port = anyPort)
|
||||||
|
if (is(T == Address) || is(T == Address4) || is(T == Address6))
|
||||||
|
{
|
||||||
|
this.address = address;
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns: Port number in network byte order.
|
||||||
|
*/
|
||||||
|
@property inout(ushort) port() inout const @nogc nothrow pure @safe
|
||||||
|
{
|
||||||
|
return this.service[].toHostOrder!ushort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Params:
|
||||||
|
* port = Port number in network byte order.
|
||||||
|
*/
|
||||||
|
@property void port(const ushort port) @nogc nothrow pure @safe
|
||||||
|
{
|
||||||
|
NetworkOrder!(ushort.sizeof)(port).copy(this.service[]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns: IP address associated with the endpoint.
|
||||||
|
*/
|
||||||
|
@property inout(Address) address() inout @nogc nothrow pure @safe
|
||||||
|
{
|
||||||
|
if (this.family == AddressFamily.inet)
|
||||||
|
{
|
||||||
|
return Address(this.address4);
|
||||||
|
}
|
||||||
|
else if (this.family == AddressFamily.inet6)
|
||||||
|
{
|
||||||
|
return Address(this.address6);
|
||||||
|
}
|
||||||
|
return Address.init;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Params:
|
||||||
|
* address = IP address associated with the endpoint.
|
||||||
|
*/
|
||||||
|
@property void address(Address address) @nogc nothrow pure @safe
|
||||||
|
{
|
||||||
|
if (address.isV4())
|
||||||
|
{
|
||||||
|
this.address = address.toV4();
|
||||||
|
}
|
||||||
|
else if (address.isV6())
|
||||||
|
{
|
||||||
|
this.address = address.toV6();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ditto
|
||||||
|
@property void address(Address4 address) @nogc nothrow pure @safe
|
||||||
|
{
|
||||||
|
this.family = AddressFamily.inet;
|
||||||
|
this.address4 = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ditto
|
||||||
|
@property void address(Address6 address) @nogc nothrow pure @safe
|
||||||
|
{
|
||||||
|
this.family = AddressFamily.inet6;
|
||||||
|
this.address4 = Address4(0);
|
||||||
|
this.address6 = address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
/**
|
/**
|
||||||
* Network programming.
|
* Network programming.
|
||||||
*
|
*
|
||||||
* Copyright: Eugene Wissner 2017-2020.
|
* Copyright: Eugene Wissner 2017-2022.
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||||
* Mozilla Public License, v. 2.0).
|
* Mozilla Public License, v. 2.0).
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Network programming.
|
|
||||||
*
|
|
||||||
* Copyright: Eugene Wissner 2016-2020.
|
|
||||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
|
||||||
* Mozilla Public License, v. 2.0).
|
|
||||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
|
||||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/network/package.d,
|
|
||||||
* tanya/network/package.d)
|
|
||||||
*/
|
|
||||||
module tanya.network;
|
|
||||||
|
|
||||||
public import tanya.network.socket;
|
|
File diff suppressed because it is too large
Load Diff
@ -95,38 +95,3 @@ import tanya.test.stub;
|
|||||||
NonCopyable[] nonCopyable;
|
NonCopyable[] nonCopyable;
|
||||||
initializeAll(nonCopyable);
|
initializeAll(nonCopyable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@nogc nothrow pure @safe unittest
|
|
||||||
{
|
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
|
|
||||||
const int[5] expected = [1, 2, 3, 4, 5];
|
|
||||||
int[5] actual = [4, 5, 1, 2, 3];
|
|
||||||
|
|
||||||
rotate(actual[0 .. 2], actual[2 .. $]);
|
|
||||||
assert(equal(actual[], expected[]));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Doesn't cause an infinite loop if back is shorter than the front
|
|
||||||
@nogc nothrow pure @safe unittest
|
|
||||||
{
|
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
|
|
||||||
const int[5] expected = [1, 2, 3, 4, 5];
|
|
||||||
int[5] actual = [3, 4, 5, 1, 2];
|
|
||||||
|
|
||||||
rotate(actual[0 .. 3], actual[3 .. $]);
|
|
||||||
assert(equal(actual[], expected[]));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Doesn't call .front on an empty front
|
|
||||||
@nogc nothrow pure @safe unittest
|
|
||||||
{
|
|
||||||
import std.algorithm.comparison : equal;
|
|
||||||
|
|
||||||
const int[2] expected = [2, 8];
|
|
||||||
int[2] actual = expected;
|
|
||||||
|
|
||||||
rotate(actual[0 .. 0], actual[]);
|
|
||||||
assert(equal(actual[], expected[]));
|
|
||||||
}
|
|
||||||
|
@ -1,97 +0,0 @@
|
|||||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
||||||
module tanya.async.tests.loop;
|
|
||||||
|
|
||||||
import core.time;
|
|
||||||
import tanya.async.loop;
|
|
||||||
import tanya.async.watcher;
|
|
||||||
import tanya.memory.allocator;
|
|
||||||
|
|
||||||
private final class DummyWatcher : Watcher
|
|
||||||
{
|
|
||||||
bool invoked;
|
|
||||||
|
|
||||||
override void invoke() @nogc
|
|
||||||
{
|
|
||||||
this.invoked = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final class TestLoop : Loop
|
|
||||||
{
|
|
||||||
override protected bool reify(SocketWatcher watcher,
|
|
||||||
EventMask oldEvents,
|
|
||||||
EventMask events) @nogc
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
override protected void poll() @nogc
|
|
||||||
{
|
|
||||||
assert(!this.done);
|
|
||||||
unloop();
|
|
||||||
}
|
|
||||||
|
|
||||||
override protected @property uint maxEvents()
|
|
||||||
const pure nothrow @safe @nogc
|
|
||||||
{
|
|
||||||
return 64U;
|
|
||||||
}
|
|
||||||
|
|
||||||
@nogc @system unittest
|
|
||||||
{
|
|
||||||
auto loop = defaultAllocator.make!TestLoop;
|
|
||||||
assert(loop.blockTime == 1.dur!"minutes");
|
|
||||||
|
|
||||||
loop.blockTime = 2.dur!"minutes";
|
|
||||||
assert(loop.blockTime == 2.dur!"minutes");
|
|
||||||
|
|
||||||
defaultAllocator.dispose(loop);
|
|
||||||
}
|
|
||||||
|
|
||||||
@nogc @system unittest
|
|
||||||
{
|
|
||||||
auto loop = defaultAllocator.make!TestLoop;
|
|
||||||
assert(loop.done);
|
|
||||||
|
|
||||||
loop.run();
|
|
||||||
assert(loop.done);
|
|
||||||
|
|
||||||
defaultAllocator.dispose(loop);
|
|
||||||
}
|
|
||||||
|
|
||||||
@nogc @system unittest
|
|
||||||
{
|
|
||||||
auto loop = defaultAllocator.make!TestLoop;
|
|
||||||
auto watcher = defaultAllocator.make!DummyWatcher;
|
|
||||||
loop.pendings.insertBack(watcher);
|
|
||||||
|
|
||||||
assert(!watcher.invoked);
|
|
||||||
loop.run();
|
|
||||||
assert(watcher.invoked);
|
|
||||||
|
|
||||||
defaultAllocator.dispose(loop);
|
|
||||||
defaultAllocator.dispose(watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@nogc @system unittest
|
|
||||||
{
|
|
||||||
auto loop = defaultAllocator.make!TestLoop;
|
|
||||||
assert(loop.maxEvents == 64);
|
|
||||||
|
|
||||||
defaultAllocator.dispose(loop);
|
|
||||||
}
|
|
||||||
|
|
||||||
@nogc @system unittest
|
|
||||||
{
|
|
||||||
auto oldLoop = defaultLoop;
|
|
||||||
auto loop = defaultAllocator.make!TestLoop;
|
|
||||||
|
|
||||||
defaultLoop = loop;
|
|
||||||
assert(defaultLoop is loop);
|
|
||||||
|
|
||||||
defaultLoop = oldLoop;
|
|
||||||
defaultAllocator.dispose(loop);
|
|
||||||
}
|
|
@ -258,7 +258,7 @@ unittest
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Typeid
|
// Typeid
|
||||||
nothrow pure @safe unittest
|
nothrow @safe unittest
|
||||||
{
|
{
|
||||||
assert(format!"{}"(typeid(int[])) == "int[]");
|
assert(format!"{}"(typeid(int[])) == "int[]");
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user