FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Socket client/server model - is this the DFL way?

 
Post new topic   Reply to topic     Forum Index -> DFL
View previous topic :: View next topic  
Author Message
Portis



Joined: 26 Sep 2005
Posts: 6

PostPosted: Wed Dec 14, 2005 4:30 pm    Post subject: Socket client/server model - is this the DFL way? Reply with quote

Hi,

I've got a working framework for a TCP/IP server that accepts messages from multiple clients, and I'm just wondering if I'm using the DFL socket events correctly.

Basically, it works like this.

1) Set up a standard D TcpSocket to act as a listener, bind, listen and initialise a SocketSet to handle the connections (there's nothing DFL specific in this code)
2) When the server 'form' starts up, do a 'socket.registerEvent' and monitor the 'ACCEPT' event on the listener socket
3) When the ACCEPT event is triggered, add the 'incoming' socket to the SocketSet and use a different 'socket.registerEvent' call to monitor the 'incoming' socket .. I call it the client socket
4) This second 'regsiterEvent' method will catch events like READ that will trigger when the client sends messages
5) When the client is closed, the server cleans up the client socket
It works just fine, I had about 30 clients (simple DFL forms) sending messages in semi-random intervals (one message every 1 to 3 seconds) and getting replies from the server.

It came about largely through trial and error after reviewing the DFL 'rps.d' example. Smile

I'll paste the framework code here, just in case I'm not doing it the DFL way or someone could spot something that I could do better.

Thanks.

Code:

class server_form: Form
{
  Socket listener;
  SocketSet clients;
   
  this()
  {
    begin_listening();
    // method that does the normal bind and listen calls for the
    // TcpSocket 'listener' and also initialises the 'clients' SocketSet
    // with pleny of slots free
   
    // DFL form code goes here
   
    try
    {
      dfl.socket.registerEvent(listener, EventType.ACCEPT |
        EventType.CONNECT | EventType.CLOSE, &server_net_events);
    }
    catch
    {  /* nothing at the moment */  }
   
  }
 
  void server_net_events(Socket event_sock, EventType event_type,
    int event_err)
  {
    switch (event_type)
    {

      case EventType.ACCEPT:
        Socket accepted_sock = listener.accept();
        clients.add(accepted_sock);
        dfl.socket.registerEvent(accepted_sock, EventType.CONNECT |
          EventType.CLOSE | EventType.READ | EventType.WRITE,
          &client_net_events);
        break;

      case EventType.CONNECT:
        // whatever
        break;

      case EventType.CLOSE:
        // whatever
        break;
    }
  }
 
  void client_net_events(Socket event_sock, EventType event_type,
    int event_err)
  {
    switch (event_type)
    {
      case EventType.CONNECT:
        // whatever
        break;

      case EventType.CLOSE:
        dfl.socket.unregisterEvent(event_sock);
        clients.remove(event_sock);
        break;

      case EventType.READ:
        char[1024] sock_read_buffer;
        int num_bytes_read = event_sock.receive(sock_read_buffer);
        if (num_bytes_read > 0)
        {
          char[] bytes_read = sock_read_buffer[0 .. num_bytes_read];
          // do something with the message
        }
        break;

      case EventType.WRITE:
        // whatever
        break;
    }
  }
}

_________________
- Brendon -
Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Wed Dec 14, 2005 4:55 pm    Post subject: Re: Socket client/server model - is this the DFL way? Reply with quote

Portis wrote:
Hi,

I've got a working framework for a TCP/IP server that accepts messages from multiple clients, and I'm just wondering if I'm using the DFL socket events correctly.
...

Looks OK, except you don't need to use SocketSet with dfl.socket.registerEvent(), SocketSet is only needed with Socket.select().

Here's something you might be interested in, dfl.socket.SocketQueue, which manages buffering send/recieve data for you. Each client should get its own SocketQueue, and you call SocketQueue.readEvent() in the EventType.READ and SocketQueue.writeEvent() in the EventType.WRITE, then you send and receive from the SocketQueue instead of the Socket.

- Chris
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> DFL All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group