diff dreactor/protocol/Raw.d @ 0:7a315154bf5e

Initial commit
author rick@minifunk
date Sun, 08 Jun 2008 01:45:38 -0400
parents
children b5c7dc3922c6
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dreactor/protocol/Raw.d	Sun Jun 08 01:45:38 2008 -0400
@@ -0,0 +1,125 @@
+module dreactor.protocol.Raw;
+
+import tango.io.Conduit;
+import tango.io.selector.model.ISelector;
+import dreactor.core.AsyncConduit;
+import dreactor.core.SelectLoop;
+import dreactor.core.ConnectionHandler;
+import tango.util.collection.CircularSeq;
+import tango.util.log.Log;
+import tango.util.log.Configurator;
+
+Logger log = Log.getLogger("dreactor.core.SelectLoop");
+
+/******************************************************************************
+    
+    Basic TCP server or client routines for sending raw data.
+
+******************************************************************************/
+class RawListener
+{
+public
+ 
+    this(ConnectionHandler mgr, SelectLoop sel)
+    {
+        manager = mgr;
+        mgr.events(Event.Read);
+        sel.addConnection(mgr);
+        select = sel;
+        children = CircularSeq!(ConnectionHandler);
+        Configurator();
+    }
+ 
+    int accept(Conduit cond)
+    {
+        AsyncConduit newcond = new AsyncConduit;
+        cond.socket().accept(newcond.socket);
+        ConnectionHandler h = ConnectionHandler.New(manager);
+        mgr.events(Event.Read);
+        select.addConnection(mgr);
+        children.append(mgr);
+    }
+
+    bool broadcast(char[] outbuf)
+    {
+        foreach(ConnectionHandler h; children)
+        {
+            if (h.appendBuffer(outbuf))
+            {
+                h.addEvent(Event.Write);
+                select.addConnection(h);
+            }
+        }
+    }
+ 
+    void close()
+    {
+        
+    }
+
+    /**************************************************************************
+
+        send
+        OutgoingHandlerD 
+        To be registered as the response to socket writable event. 
+        Sends data, returns amount sent. Unregisters Handler for sending
+        if there is no more data left to send. 
+
+    ***************************************************************************/
+    int send(ConnectionHandler h, RegisterD reg)
+    {
+        char[] outbuf = h.nextBuffer();
+        if (!outbuf is null)
+        {
+            int sent = h.transport.write(outbuf);
+            if (sent > 0)
+            {
+                if (! h.addOffset(sent))
+                {
+                    h.removeEvent(Event.write);
+                    reg(h);
+                }
+            }
+            else if (sent == EOF)
+            {
+                // EAGAIN ? probably shouldn't have happened. 
+            }
+            else
+            {
+               log.error("Socket send return ERR"); 
+            }
+            return sent;
+        }
+        return 0;
+    }
+
+    /**************************************************************************
+    
+        receive
+        IncomingHandlerD
+        Default incoming data handler. Should be replaced with something useful.
+
+    **************************************************************************/ 
+    int receive(ConnectionHandler h, RegisterD reg)
+    {
+    }
+
+private
+    ConnectionHandler manager;
+    SelectLoop select;
+    CircularSeq!(ConnectionHandler) children;
+}
+
+class RawClient
+{
+    ConnectionHandler manager;
+    SelectLoop select;
+ 
+    this(ConnectionHandler mgr, SelectLoop sel)
+    {
+        manager = mgr;
+        mgr.events(Event.Read);
+        sel.addConnection(mgr);
+        select = sel;
+    }
+}