On Tue, Feb 15, 2011 at 4:13 PM, lurker <[email protected]> wrote:
>> What platform and version of DMD? There was a bug in the Socket
>> implementation on Windows recently where the WinSock data was
> either
>> initialized or deinitialized multiple times. I don't remember the
>> details, but I remember having "weird crashes."
>
> Yes, Windows + dmd 2.51.

I looked it up, and that bug was fixed in 2.50, so that's not the problem.
>
>> Yes, I've done this in the past.
>
> Great. At least is posible. Do you have an example available
> somewhere? It would really help

Actually, I lied, I was getting sockets and spawning new threads with
them as arguments.
However, the attached source compiles and runs on Linux (although it
doesn't do any sort of cleanup when it exits, so I can get errors on
the bind call that the address is in use).
Disclaimer:
I'm not sure whether casting to and away from shared like I do is
actually safe, so hopefully someone more knowledgeable can chime in on
that. From a type standpoint, as long as ownership of the socket is
passed between threads cleanly and only one thread can access the
socket at a time, nothing odd should happen, but I'm not sure how/if
TLS and the actual details of shared could break this. I'll see what I
can dig out as far as that goes.
module dsocktest;
import std.stdio;
import std.socket;
import std.concurrency;

void main() {
	auto sock = new TcpSocket();
	sock.bind(new InternetAddress(5122));
	spawn(&listen, cast(shared)sock, thisTid);
	while(true) {
		receive((shared(Socket) s) {
			auto sock = cast(Socket)s;
			writeln("got socket!");
			sock.send("You Win!\n");
			sock.close();
		});
	}
}

void listen(TcpSocket s, Tid dest) {
	s.listen(5);
	while(true) {
		auto sock = cast(shared)s.accept;
		writeln("got connection!");
		send(dest, sock);
	}
}

Reply via email to