2016-10-07 10:40:10 +00:00
|
|
|
module eventcore.socket;
|
|
|
|
|
|
|
|
import eventcore.core : eventDriver;
|
|
|
|
import eventcore.driver;
|
2018-06-06 10:41:20 +00:00
|
|
|
import core.time: Duration;
|
2016-10-07 10:40:10 +00:00
|
|
|
import std.exception : enforce;
|
|
|
|
import std.socket : Address;
|
|
|
|
|
|
|
|
|
2016-10-07 17:40:01 +00:00
|
|
|
StreamSocket connectStream(alias callback)(scope Address peer_address)
|
2016-10-07 10:40:10 +00:00
|
|
|
@safe {
|
2016-10-07 17:40:01 +00:00
|
|
|
void cb(StreamSocketFD fd, ConnectStatus status) @safe nothrow {
|
|
|
|
if (fd != StreamSocketFD.invalid) eventDriver.sockets.addRef(fd);
|
|
|
|
callback(StreamSocket(fd), status);
|
|
|
|
if (fd != StreamSocketFD.invalid) eventDriver.sockets.releaseRef(fd);
|
|
|
|
}
|
|
|
|
|
2016-12-19 21:16:34 +00:00
|
|
|
auto fd = eventDriver.sockets.connectStream(peer_address, null, &cb);
|
2016-10-07 17:40:01 +00:00
|
|
|
enforce(fd != StreamSocketFD.invalid, "Failed to create socket.");
|
|
|
|
eventDriver.sockets.addRef(fd);
|
2016-10-07 10:40:10 +00:00
|
|
|
return StreamSocket(fd);
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:40:01 +00:00
|
|
|
StreamListenSocket listenStream(scope Address bind_address)
|
2016-10-07 10:40:10 +00:00
|
|
|
@safe {
|
2016-10-07 17:40:01 +00:00
|
|
|
auto fd = eventDriver.sockets.listenStream(bind_address, null);
|
|
|
|
enforce(fd != StreamListenSocketFD.invalid, "Failed to create socket.");
|
2016-10-07 10:40:10 +00:00
|
|
|
return StreamListenSocket(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
DatagramSocket createDatagramSocket(scope Address bind_address, scope Address target_address = null)
|
|
|
|
@safe {
|
|
|
|
auto fd = eventDriver.sockets.createDatagramSocket(bind_address, target_address);
|
2016-10-07 17:40:01 +00:00
|
|
|
enforce(fd != DatagramSocketFD.invalid, "Failed to create socket.");
|
2016-10-07 10:40:10 +00:00
|
|
|
return DatagramSocket(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StreamSocket {
|
|
|
|
@safe: nothrow:
|
|
|
|
|
|
|
|
private StreamSocketFD m_fd;
|
|
|
|
|
|
|
|
private this(StreamSocketFD fd)
|
|
|
|
{
|
|
|
|
m_fd = fd;
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:40:01 +00:00
|
|
|
this(this) { if (m_fd != StreamSocketFD.invalid) eventDriver.sockets.addRef(m_fd); }
|
|
|
|
~this() { if (m_fd != StreamSocketFD.invalid) eventDriver.sockets.releaseRef(m_fd); }
|
2016-10-07 10:40:10 +00:00
|
|
|
|
|
|
|
@property ConnectionState state() { return eventDriver.sockets.getConnectionState(m_fd); }
|
|
|
|
@property void tcpNoDelay(bool enable) { eventDriver.sockets.setTCPNoDelay(m_fd, enable); }
|
2018-06-06 10:41:20 +00:00
|
|
|
void setKeepAlive(bool enable) { eventDriver.sockets.setKeepAlive(m_fd, enable); }
|
|
|
|
void setKeepAliveParams(Duration idle, Duration interval, int probeCount = 5) {
|
|
|
|
eventDriver.sockets.setKeepAliveParams(m_fd, idle, interval, probeCount);
|
|
|
|
}
|
|
|
|
void setUserTimeout(Duration timeout) { eventDriver.sockets.setUserTimeout(m_fd, timeout); }
|
2016-10-07 17:40:01 +00:00
|
|
|
}
|
2016-10-07 10:40:10 +00:00
|
|
|
|
2016-10-07 17:40:01 +00:00
|
|
|
void read(alias callback)(ref StreamSocket socket, ubyte[] buffer, IOMode mode)
|
|
|
|
{
|
|
|
|
void cb(StreamSocketFD, IOStatus status, size_t nbytes) @safe nothrow {
|
|
|
|
callback(status, nbytes);
|
|
|
|
}
|
|
|
|
eventDriver.sockets.read(socket.m_fd, buffer, mode, &cb);
|
|
|
|
}
|
2020-03-17 13:23:17 +00:00
|
|
|
void cancelRead(ref StreamSocket socket) @safe nothrow { eventDriver.sockets.cancelRead(socket.m_fd); }
|
2016-10-07 17:40:01 +00:00
|
|
|
void waitForData(alias callback)(ref StreamSocket socket)
|
|
|
|
{
|
|
|
|
void cb(StreamSocketFD, IOStatus status, size_t nbytes) @safe nothrow {
|
|
|
|
callback(status, nbytes);
|
|
|
|
}
|
|
|
|
eventDriver.sockets.waitForData(socket.m_fd, &cb);
|
2016-10-07 10:40:10 +00:00
|
|
|
}
|
2016-10-07 17:40:01 +00:00
|
|
|
void write(alias callback)(ref StreamSocket socket, const(ubyte)[] buffer, IOMode mode)
|
|
|
|
{
|
|
|
|
void cb(StreamSocketFD, IOStatus status, size_t nbytes) @safe nothrow {
|
|
|
|
callback(status, nbytes);
|
|
|
|
}
|
|
|
|
eventDriver.sockets.write(socket.m_fd, buffer, mode, &cb);
|
|
|
|
}
|
2020-03-17 13:23:17 +00:00
|
|
|
void cancelWrite(ref StreamSocket socket) @safe nothrow { eventDriver.sockets.cancelWrite(socket.m_fd); }
|
|
|
|
void shutdown(ref StreamSocket socket, bool shut_read = true, bool shut_write = true)
|
|
|
|
@safe nothrow {
|
|
|
|
eventDriver.sockets.shutdown(socket.m_fd, shut_read, shut_write);
|
|
|
|
}
|
2016-10-07 17:40:01 +00:00
|
|
|
|
2016-10-07 10:40:10 +00:00
|
|
|
|
|
|
|
struct StreamListenSocket {
|
|
|
|
@safe: nothrow:
|
|
|
|
|
|
|
|
private StreamListenSocketFD m_fd;
|
|
|
|
|
|
|
|
private this(StreamListenSocketFD fd)
|
|
|
|
{
|
|
|
|
m_fd = fd;
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:40:01 +00:00
|
|
|
this(this) { if (m_fd != StreamListenSocketFD.invalid) eventDriver.sockets.addRef(m_fd); }
|
|
|
|
~this() { if (m_fd != StreamListenSocketFD.invalid) eventDriver.sockets.releaseRef(m_fd); }
|
|
|
|
}
|
2016-10-07 10:40:10 +00:00
|
|
|
|
2016-10-07 17:40:01 +00:00
|
|
|
void waitForConnections(alias callback)(ref StreamListenSocket socket)
|
|
|
|
{
|
2017-01-15 20:59:15 +00:00
|
|
|
void cb(StreamListenSocketFD, StreamSocketFD sock, scope RefAddress addr) @safe nothrow {
|
2016-10-07 17:40:01 +00:00
|
|
|
auto ss = StreamSocket(sock);
|
2017-01-15 20:59:15 +00:00
|
|
|
callback(ss, addr);
|
2016-10-07 10:40:10 +00:00
|
|
|
}
|
2016-10-07 17:40:01 +00:00
|
|
|
eventDriver.sockets.waitForConnections(socket.m_fd, &cb);
|
2016-10-07 10:40:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:40:01 +00:00
|
|
|
|
2016-10-07 10:40:10 +00:00
|
|
|
struct DatagramSocket {
|
|
|
|
@safe: nothrow:
|
|
|
|
|
|
|
|
private DatagramSocketFD m_fd;
|
|
|
|
|
|
|
|
private this(DatagramSocketFD fd)
|
|
|
|
{
|
|
|
|
m_fd = fd;
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:40:01 +00:00
|
|
|
this(this) { if (m_fd != DatagramSocketFD.invalid) eventDriver.sockets.addRef(m_fd); }
|
|
|
|
~this() { if (m_fd != DatagramSocketFD.invalid) eventDriver.sockets.releaseRef(m_fd); }
|
2018-02-22 01:08:09 +00:00
|
|
|
|
|
|
|
@property void broadcast(bool enable) { eventDriver.sockets.setBroadcast(m_fd, enable); }
|
2016-10-07 10:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void receive(alias callback)(ref DatagramSocket socket, ubyte[] buffer, IOMode mode) {
|
2017-01-15 20:59:15 +00:00
|
|
|
void cb(DatagramSocketFD fd, IOStatus status, size_t bytes_written, scope RefAddress address) @safe nothrow {
|
2016-10-07 10:40:10 +00:00
|
|
|
callback(status, bytes_written, address);
|
|
|
|
}
|
|
|
|
eventDriver.sockets.receive(socket.m_fd, buffer, mode, &cb);
|
|
|
|
}
|
|
|
|
void cancelReceive(ref DatagramSocket socket) { eventDriver.sockets.cancelReceive(socket.m_fd); }
|
|
|
|
void send(alias callback)(ref DatagramSocket socket, const(ubyte)[] buffer, IOMode mode, Address target_address = null) {
|
2017-01-15 20:59:15 +00:00
|
|
|
void cb(DatagramSocketFD fd, IOStatus status, size_t bytes_written, scope RefAddress) @safe nothrow {
|
2016-10-07 10:40:10 +00:00
|
|
|
callback(status, bytes_written);
|
|
|
|
}
|
2016-12-19 21:16:34 +00:00
|
|
|
eventDriver.sockets.send(socket.m_fd, buffer, mode, target_address, &cb);
|
2016-10-07 10:40:10 +00:00
|
|
|
}
|
|
|
|
void cancelSend(ref DatagramSocket socket) { eventDriver.sockets.cancelSend(socket.m_fd); }
|