I am trying to understand concurrent/parallel programming with D but i just don't get how
i should usesome of the concepts.

This is the code i am using to tying out stuff.

public class TCPListener {
    ubyte[] _messageBuffer;
    Socket _server;
    Socket _client;

    // define server in constructor
    this(string address, ushort port) {
        _server = new TcpSocket();
_server.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, true);
    }

    // starts accept() in a new thread
    void startlistening(){...}

    // accepts connection and assigns client
    // after that start receiving in new thread and keep accepting
    void accept() {...}

    // receives data and adds to buffer
    // emits message if lineend in buffer
    void receiving() {...}

// gets the client and sends the data (prefferably in its own task/thread)
    // emits signal when completed
    void send(string data) {...}

}

And I would like to use it in the following matter:

int main()
{
    auto o1 = new SpecialisedTCPListener();

    //do other stuff
}

class SpecialisedTCPListener{
    TCPListener _listener;

    this(){
        _listener= new TCPListener("127.0.0.1", 10000);
        _listener.connect(&MessageReceived);
        _listener.connect(&SendCallback);
        _listener.startListening();
    }

    public void MessageReceived(string message){
        auto answer = doSomeThings(message);
        // send the answer
        _listener.Send(answer);
    }

    // did it send correctly?
    public void SendCallback(CallBackData e){...}
}


When i tried this approach it did not work. Accepting and receiving worked normally but sending was impossible because _client was thread local und would return a nullpointer.

How do i pass fields to the different threads?

I tried using spawn to start the threads but that only works with functions and not with class methods.
What would be better ways to do something like this?



Reply via email to