Mercurial > projects > dreactor
view asyncdreactor/protocol/RawUdp.d @ 11:5836613d16ac
reorg! reorg!
author | rick@minifunk |
---|---|
date | Tue, 12 Aug 2008 16:59:56 -0400 |
parents | dreactor/protocol/RawUdp.d@f8b01c9f7114 |
children |
line wrap: on
line source
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) { char inbuf[8192]; auto format = Log.format; if(h.transport.read(inbuf) > 0) log.info(format("Received Buffer: {}", inbuf)); } private ConnectionHandler manager; SelectLoop select; CircularSeq!(ConnectionHandler) children; } class RawClient { public this(ConnectionHandler mgr, SelectLoop sel) { manager = mgr; mgr.events(Event.Read); sel.addConnection(mgr); select = sel; } private ConnectionHandler manager; SelectLoop select; }