I am trying to build something like the asynchronous Sockets from C# .NET but I'm stuck at the accepting phase.

My code is something like this:


public class TCPListener {

    ushort _port;
    string _address;
    bool _active;
    string _lineEnd;
    ubyte[] _messageBuffer;
    Socket _socket;
    Socket _connection;
    int _waitForAnswer = 500;
    bool _keepOpen = true;

    this(string address, ushort port, string lineEnd = "\n") {
        _port = port;
        _address = address;
        _socket = new TcpSocket();
_socket.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
        this.connect(&this.Accepted);
        this.connect(&this.Received);
    }

    void startListening(){
        try{
                _socket.bind(new InternetAddress(_port));
                _socket.listen(3);
        }
        catch (Exception e){
                writeln(e.msg);
        }
        auto _connectionTask = task(&this.Accept);
        _connectionTask.executeInNewThread();
    }

    private void Accept(){
        bool error = false;
        // try to accept connection
        try{
            Socket client = _socket.accept();
            Thread.sleep( dur!("msecs")( 50 ) );
            MessageBuffer buffer;
            auto received = client.receive(buffer.data);
            emit(error);
            //_connection = client;
        }
        catch (SocketAcceptException e){
            writeln("Error while accepting connection: " ~ e.msg);
            error = true;
        }public class TCPListener {

    ushort _port;
    string _address;
    bool _active;
    string _lineEnd;
    ubyte[] _messageBuffer;
    Socket _socket;
    Socket _connection;
    int _waitForAnswer = 500;
    bool _keepOpen = true;

    this(string address, ushort port, string lineEnd = "\n") {
        _port = port;
        _address = address;
        _socket = new TcpSocket();
_socket.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
        this.connect(&this.Accepted);
        this.connect(&this.Received);
    }

    void startListening(){
        try{
                _socket.bind(new InternetAddress(_port));
                _socket.listen(3);
        }
        catch (Exception e){
                writeln(e.msg);
        }
        auto _connectionTask = task(&this.Accept);
        _connectionTask.executeInNewThread();
    }

    private void Accept(){
        bool error = false;
        // try to accept connection
        try{
            Socket client = _socket.accept();
            Thread.sleep( dur!("msecs")( 50 ) );
            MessageBuffer buffer;
            auto received = client.receive(buffer.data);
            emit(error);
            //_connection = client;
        }
        catch (SocketAcceptException e){
            writeln("Error while accepting connection: " ~ e.msg);
            error = true;
        }
        finally{
            emit(error);
        }
    }

    mixin Signal!(bool);

        finally{
            emit(error);
        }
    }

    mixin Signal!(bool);

...
}


The client created by _socket.accept() cannot receive any data.

client.receive() immediately returns 0 like a non-blocking socket. I think it has something to do with the original socket being owned by a different thread.

I am not very familiar with multitasking/multithreading in D so any help would be appreciated.

Reply via email to