Thread overview
How to use eventcore write an echo server?
March 12

I use eventcore latest version write an echo server for test.

some error of build.

my D code:


import eventcore.core;
import std.functional : toDelegate;
import std.socket : InternetAddress;
import std.exception : enforce;
import core.time : Duration;
import std.stdio : writeln;

void main()
{
	auto addr = new InternetAddress("127.0.0.1", 1111);
	auto listener = eventDriver.sockets.listenStream(addr, toDelegate(&onClientConnect));
	enforce(listener != StreamListenSocketFD.invalid, "Failed to listen for connections.");

	writeln("Listening for requests on port 1111...");
	while (eventDriver.core.waiterCount)
		eventDriver.core.processEvents(Duration.max);
}

void onClientConnect(StreamListenSocketFD listener, StreamSocketFD client, scope RefAddress)
{
	Connection connection = new Connection(client);

	// Send welcome message to client
	connection.write("Welcome to use my echo server.");
}

class Connection
{
	StreamSocketFD client;

	ubyte[1024] buf = void;

	this(StreamSocketFD client)
	{
		this.client = client;

		eventDriver.sockets.read(client, buf, IOMode.once, &onRead);
	}

	void write(ubyte[] data)
	{
		eventDriver.sockets.write(client, data, IOMode.all, &onWriteFinished);
	}

	void onWriteFinished(StreamSocketFD fd, IOStatus status, size_t len)
	{
		writeln("Send size: ", len);
	}

	void onRead(StreamSocketFD, IOStatus status, size_t bytes_read)
	{
		if (status != IOStatus.ok) {
			writeln("Client disconnect");
			eventDriver.sockets.shutdown(client, true, true);
			eventDriver.sockets.releaseRef(client);
			return;
		}

		this.write(buf[0..bytes_read]);
		
		eventDriver.sockets.read(client, buf, IOMode.once, &onRead);
	}
}

error code:

dub build
    Starting Performing "debug" build using ldc2 for aarch64, arm_hardfloat.
  Up-to-date taggedalgebraic 0.11.22: target for configuration [library] is up to date.
  Up-to-date eventcore 0.9.28: target for configuration [cfrunloop] is up to date.
    Building eventcoredemo ~master: building configuration [application]
source/main.d(12,50): Error: none of the overloads of `listenStream` are callable using argument types `(InternetAddress, void delegate(StreamListenSocketFD a0, StreamSocketFD a1, scope RefAddress a2) @system)`
../../../.dub/packages/eventcore/0.9.28/eventcore/source/eventcore/driver.d(213,23):        Candidates are: `eventcore.driver.EventDriverSockets.listenStream(scope Address bind_address, StreamListenOptions options, void delegate(StreamListenSocketFD, StreamSocketFD, scope RefAddress remote_address) nothrow @safe on_accept)`
../../../.dub/packages/eventcore/0.9.28/eventcore/source/eventcore/driver.d(215,29):                        `eventcore.driver.EventDriverSockets.listenStream(scope Address bind_address, void delegate(StreamListenSocketFD, StreamSocketFD, scope RefAddress remote_address) nothrow @safe on_accept)`
../../../.dub/packages/eventcore/0.9.28/eventcore/source/eventcore/drivers/posix/sockets.d(233,38):                        `eventcore.drivers.posix.sockets.PosixEventDriverSockets!(CFRunLoopEventLoop).PosixEventDriverSockets.listenStream(scope Address address, StreamListenOptions options, void delegate(StreamListenSocketFD, StreamSocketFD, scope RefAddress remote_address) nothrow @safe on_accept)`
source/main.d(25,18): Error: function `main.Connection.write(ubyte[] data)` is not callable using argument types `(string)`
source/main.d(25,18):        cannot pass argument `"Welcome to use my echo server."` of type `string` to parameter `ubyte[] data`
source/main.d(38,27): Error: function `eventcore.drivers.posix.sockets.PosixEventDriverSockets!(CFRunLoopEventLoop).PosixEventDriverSockets.read(StreamSocketFD socket, ubyte[] buffer, IOMode mode, void delegate(StreamSocketFD, IOStatus, ulong) nothrow @safe on_read_finish)` is not callable using argument types `(StreamSocketFD, ubyte[1024], IOMode, void delegate(StreamSocketFD, IOStatus status, ulong bytes_read))`
source/main.d(38,27):        cannot pass argument `&this.onRead` of type `void delegate(StreamSocketFD, IOStatus status, ulong bytes_read)` to parameter `void delegate(StreamSocketFD, IOStatus, ulong) nothrow @safe on_read_finish`
source/main.d(43,28): Error: function `eventcore.drivers.posix.sockets.PosixEventDriverSockets!(CFRunLoopEventLoop).PosixEventDriverSockets.write(StreamSocketFD socket, const(ubyte)[] buffer, IOMode mode, void delegate(StreamSocketFD, IOStatus, ulong) nothrow @safe on_write_finish)` is not callable using argument types `(StreamSocketFD, ubyte[], IOMode, void delegate(StreamSocketFD fd, IOStatus status, ulong len))`
source/main.d(43,28):        cannot pass argument `&this.onWriteFinished` of type `void delegate(StreamSocketFD fd, IOStatus status, ulong len)` to parameter `void delegate(StreamSocketFD, IOStatus, ulong) nothrow @safe on_write_finish`
source/main.d(62,27): Error: function `eventcore.drivers.posix.sockets.PosixEventDriverSockets!(CFRunLoopEventLoop).PosixEventDriverSockets.read(StreamSocketFD socket, ubyte[] buffer, IOMode mode, void delegate(StreamSocketFD, IOStatus, ulong) nothrow @safe on_read_finish)` is not callable using argument types `(StreamSocketFD, ubyte[1024], IOMode, void delegate(StreamSocketFD __param_0, IOStatus status, ulong bytes_read))`
source/main.d(62,27):        cannot pass argument `&this.onRead` of type `void delegate(StreamSocketFD __param_0, IOStatus status, ulong bytes_read)` to parameter `void delegate(StreamSocketFD, IOStatus, ulong) nothrow @safe on_read_finish`
Error ldc2 failed with exit code 1.

How to fix it? than you ;)

March 14

On Tuesday, 12 March 2024 at 05:13:26 UTC, zoujiaqing wrote:

>

How to fix it? than you ;)

Try the following:

class Connection
{
	StreamSocketFD client;

	ubyte[1024] buf = void;

        // Add these two lines before the constructor:
        nothrow:
        @safe:

	this(StreamSocketFD client)

Also you will need to either comment out calls to writeln() or surround them in try/catch as writeln() may throw so it can't really be called in nothrow code... For starters just comment all calls to writeln() out to make it compile, and then move from there.

March 26

On Thursday, 14 March 2024 at 18:49:54 UTC, Dejan Lekic wrote:

>

On Tuesday, 12 March 2024 at 05:13:26 UTC, zoujiaqing wrote:

>

How to fix it? than you ;)

Try the following:

class Connection
{
	StreamSocketFD client;

	ubyte[1024] buf = void;

        // Add these two lines before the constructor:
        nothrow:
        @safe:

	this(StreamSocketFD client)

Also you will need to either comment out calls to writeln() or surround them in try/catch as writeln() may throw so it can't really be called in nothrow code... For starters just comment all calls to writeln() out to make it compile, and then move from there.

This is the code I modified that can be compiled and run successfully.

import eventcore.core;
import std.functional : toDelegate;
import std.socket : InternetAddress;
import std.exception : enforce;
import core.time : Duration;
import std.stdio : writeln;

    void main()
    {
    	auto addr = new InternetAddress("127.0.0.1", 1111);
    	auto listener = eventDriver.sockets.listenStream(addr, toDelegate(&onClientConnect));
    	enforce(listener != StreamListenSocketFD.invalid, "Failed to listen for connections.");

    	writeln("Listening for requests on port 1111...");
    	while (eventDriver.core.waiterCount)
    		eventDriver.core.processEvents(Duration.max);
    }


    nothrow @safe: void onClientConnect(StreamListenSocketFD listener, StreamSocketFD client, scope RefAddress)
    {
    	import std.array: appender;
    	import std.algorithm: copy;

    	try {
    		writeln("onClientConnect");
    		Connection connection = new Connection(client);
    		auto w = appender!(ubyte[]);
    		string str = "Welcome to use my echo server.";
    		copy(str, w);
    		connection.write(w[]);
    	} catch (Exception e) {
    		// pass
    	}
    	
    }

    class Connection
    {
    	StreamSocketFD client;

    	ubyte[1024] buf = void;

    	nothrow:
    	@safe:

    	this(StreamSocketFD client)
    	{
    		this.client = client;

    		eventDriver.sockets.read(client, buf, IOMode.once, &onRead);
    	}

    	void write(ubyte[] data)
    	{
    		eventDriver.sockets.write(client, data, IOMode.all, &onWriteFinished);
    	}

    	void onWriteFinished(StreamSocketFD fd, IOStatus status, size_t len)
    	{
    		try {
    			writeln("Send size: ", len);
    		} catch (Exception e) {
    			// pass
    		}
    	}

    	void onRead(StreamSocketFD, IOStatus status, size_t bytes_read)
    	{
    		if (status != IOStatus.ok)
    		{
    			try {
    				writeln("Client disconnect");
    			} catch (Exception e) {
    				// pass
    			}
    			eventDriver.sockets.shutdown(client, true, true);
    			eventDriver.sockets.releaseRef(client);
    			return;
    		}

    		this.write(buf[0 .. bytes_read]);

    		eventDriver.sockets.read(client, buf, IOMode.once, &onRead);
    	}
    }
    ```