Asynchronous Sockets in D

Ok, this is my first question here about stack overflow, so it gets along with it if I don't ask properly.

Basically I am trying to encode some asynchronous sockets using std.socket, but I'm not sure if I understood the concept correctly. I only ever worked with asynchronous sockets in C #, and in D it was at a much lower level. I researched a lot and searched for a lot of code, documentation, etc. For both D and C / C ++, to understand, however, I'm not sure if I understand the concept correctly and if any of you have examples. I tried looking at splat, but it is very outdated and vibe seems too complicated just for a simple asynchronous shell.

If I understand correctly, there is no poll () function in std.socket, so you will need to use a SocketSet with one socket in select () to correctly poll the status of the socket?

Thus, basically, as I will deal with sockets, this is a poll to get the read status of the socket, and if it succeeds (value> 0), then I can call the receive () method, which will return 0 to disable else received value, but I must continue to do this until the expected bytes are received.

Of course, the socket is set to unblocked!

Is it correct?

Here is the code I've done so far.

void HANDLE_READ()
{
    while (true)
    {
        synchronized
        {
            auto events = cast(AsyncObject[int])ASYNC_EVENTS_READ;
            foreach (asyncObject; events)
            {
                int poll = pollRecv(asyncObject.socket.m_socket);
                switch (poll)
                {
                    case 0:
                    {
                        throw new SocketException("The socket had a time out!");
                        continue;
                    }
                    default:
                    {
                        if (poll <= -1)
                        {
                            throw new SocketException("The socket was interrupted!");
                            continue;
                        }                    

                        int recvGetSize = (asyncObject.socket.m_readBuffer.length - asyncObject.socket.readSize);
                        ubyte[] recvBuffer = new ubyte[recvGetSize];
                        int recv = asyncObject.socket.m_socket.receive(recvBuffer);

                        if (recv == 0)
                        {
                            removeAsyncObject(asyncObject.event_id, true);
                            asyncObject.socket.disconnect();
                            continue;
                        }

                        asyncObject.socket.m_readBuffer ~= recvBuffer;                      
                        asyncObject.socket.readSize += recv;

                        if (asyncObject.socket.readSize == asyncObject.socket.expectedReadSize)
                        {
                            removeAsyncObject(asyncObject.event_id, true);
                            asyncObject.event(asyncObject.socket);
                        }
                        break;
                    }
                }
            }
        }
    }
}
+4
source share
1 answer

, , , ,

. , select, , , . , , , .

SocketSet , . , "" . , "" . "".

select ( ) , , SocketSet , , . : , .

D- : ae.net.asockets.

+1

Source: https://habr.com/ru/post/1543689/


All Articles