RejectedSoftware Forums

Sign up

Trivial listenTCP() don't work for me

Example of an echo server from vibed.org:

import vibe.d;

shared static this()
{
    listenTCP(7000, (conn) { conn.write(conn); });
}

It works for me. But if I am trying to add this ctor to one my project it isn't work: it listens and allows to connect, but delegate isn't executed (checked by assert(false) at first line of it). And after killing of this process it just releases connected socket by disconnecting client (telnet has been used as client).

What could be the problem?

Re: Trivial listenTCP() don't work for me

Am 17.04.2015 um 07:46 schrieb doge:

Example of an echo server from vibed.org:

import vibe.d;

shared static this()
{
     listenTCP(7000, (conn) { conn.write(conn); });
}

It works for me. But if I am trying to add this ctor to one my project it isn't work: it listens and allows to connect, but delegate isn't executed (checked by assert(false) at first line of it). And after killing of this process it just releases connected socket by disconnecting client (telnet has been used as client).

What could be the problem?

Sounds like no event loop may be running. Do you use your own main
function, or call some kind of indefinitely blocking function before
leaving static this?

Re: Trivial listenTCP() don't work for me

On Fri, 17 Apr 2015 10:10:03 +0200, Sönke Ludwig wrote:

Am 17.04.2015 um 07:46 schrieb doge:

Example of an echo server from vibed.org:

import vibe.d;

shared static this()
{
     listenTCP(7000, (conn) { conn.write(conn); });
}

It works for me. But if I am trying to add this ctor to one my project it isn't work: it listens and allows to connect, but delegate isn't executed (checked by assert(false) at first line of it). And after killing of this process it just releases connected socket by disconnecting client (telnet has been used as client).

What could be the problem?

Sounds like no event loop may be running. Do you use your own main

Yes, but also I am practice unittests-only executions ("dub test")

function, or call some kind of indefinitely blocking function before
leaving static this?

Hmm... My shared static this(){} contains only one line, as an example above.

What is "blocking" in this context? Do I understand correctly that listenTCP is launching an independent thread for listening?

Re: Trivial listenTCP() don't work for me

On Fri, 17 Apr 2015 14:43:09 GMT, doge wrote:

On Fri, 17 Apr 2015 10:10:03 +0200, Sönke Ludwig wrote:
(...)

Sounds like no event loop may be running. Do you use your own main

Yes, but also I am practice unittests-only executions ("dub test")

function, or call some kind of indefinitely blocking function before
leaving static this?

Hmm... My shared static this(){} contains only one line, as an example above.

What is "blocking" in this context? Do I understand correctly that listenTCP is launching an independent thread for listening?

No, by default listenTCP will run in the same thread in which it is called (by using the asynchronous event loop). It thus requires runEventLoop to be called eventually, so if you have a custom main, something like return runEventLoop() should be the last line.

Blocking here would mean that some lengthy computation happens (or I/O, a sleep() call or similar) that doesn't allow the event loop to run. All such operations in vibe.d are made in a way that they automatically cause the event loop to continue when they need to wait for something, but others may not be.

If you want to test listenTCP within a unit test, you can do it using this pattern:

unittest {
	// open the listening socket and
	// register the connection handler
	listenTCP(...);

	// run the test asynchronously
	runTask({
		// connect to the listening socket
		// and perform some tests here
		// ...

		// finally stop the event loop to
		// continue with the next unit test
		exitEventLoop();
	});

	// start event processing
	runEventLoop();
}

Re: Trivial listenTCP() don't work for me

On Fri, 17 Apr 2015 16:16:38 GMT, Sönke Ludwig wrote:

On Fri, 17 Apr 2015 14:43:09 GMT, doge wrote:

On Fri, 17 Apr 2015 10:10:03 +0200, Sönke Ludwig wrote:
(...)

Sounds like no event loop may be running. Do you use your own main

Yes, but also I am practice unittests-only executions ("dub test")

function, or call some kind of indefinitely blocking function before
leaving static this?

Hmm... My shared static this(){} contains only one line, as an example above.

What is "blocking" in this context? Do I understand correctly that listenTCP is launching an independent thread for listening?

No, by default listenTCP will run in the same thread in which it is called (by using the asynchronous event loop). It thus requires runEventLoop to be called eventually, so if you have a custom main, something like return runEventLoop() should be the last line.

Blocking here would mean that some lengthy computation happens (or I/O, a sleep() call or similar) that doesn't allow the event loop to run. All such operations in vibe.d are made in a way that they automatically cause the event loop to continue when they need to wait for something, but others may not be.

Yes, then - I am forgot to call runEventLoop(). Thanks!

If you want to test listenTCP within a unit test, you can do it using this pattern:

unittest {
	// open the listening socket and
	// register the connection handler
	listenTCP(...);

	// run the test asynchronously
	runTask({
		// connect to the listening socket
		// and perform some tests here
		// ...

		// finally stop the event loop to
		// continue with the next unit test
		exitEventLoop();
	});

	// start event processing
	runEventLoop();
}

Yes, it did. Now everything is clear! Thanks!

Re: Trivial listenTCP() don't work for me

Yes, it did.

did it*