Mercurial > projects > dreactor
diff dreactor/core/ConnectionHandler.d @ 3:e3dbc9208822
basic tests working
author | rick@minifunk |
---|---|
date | Tue, 08 Jul 2008 11:21:09 -0400 |
parents | d3374d553986 |
children | f875a1f278b8 |
line wrap: on
line diff
--- a/dreactor/core/ConnectionHandler.d Thu Jun 12 23:12:17 2008 -0400 +++ b/dreactor/core/ConnectionHandler.d Tue Jul 08 11:21:09 2008 -0400 @@ -1,24 +1,27 @@ module dreactor.core.ConnectionHandler; import tango.io.selector.model.ISelector; -import tango.net.Socket.Address; import tango.util.collection.CircularSeq; +import tango.net.Socket; +public import tango.core.Exception; +import dreactor.transport.AsyncSocketConduit; -import dreactor.transport.AsyncSocketConduit; +import tango.util.log.Log; +import tango.util.log.Config; alias bool delegate(ConnectionHandler) RegisterD; -alias bool delegate(ConnectionHandler, RegisterD) IncomingHandlerD; -alias bool delegate(ConnectionHandler, RegisterD) OutgoingHandlerD; +alias int delegate(ConnectionHandler) IncomingHandlerD; +alias int delegate(ConnectionHandler) OutgoingHandlerD; alias int delegate(ConnectionHandler, RegisterD) ErrorHandlerD; -alias bool delegate(ConnectionHandler, RegisterD) DisconnectHandlerD; -alias int delegate(Conduit, RegisterD) ConnectHandlerD; +alias int delegate(ConnectionHandler) DisconnectHandlerD; +alias int delegate(Conduit, RegisterD) ConnectHandlerD; -alias bool function(ConnectionHandler, RegisterD) IncomingHandlerF; -alias bool function(ConnectionHandler, RegisterD) OutgoingHandlerF; +alias int function(ConnectionHandler) IncomingHandlerF; +alias int function(ConnectionHandler) OutgoingHandlerF; alias int function(ConnectionHandler, RegisterD) ErrorHandlerF; -alias bool function(ConnectionHandler, RegisterD) DisconnectHandlerF; -alias int function(Conduit, RegisterD) ConnectHandlerF; +alias int function(ConnectionHandler) DisconnectHandlerF; +alias int function(Conduit, RegisterD) ConnectHandlerF; /****************************************************************************** @@ -27,23 +30,24 @@ These can be populated passed to a SelectLoop directly by the end user, or may be managed by a chosen Protocol. ******************************************************************************/ -class ConnectionHandler() +class ConnectionHandler { public - enum { init, connected, listening, idle, closing }; + enum State { init, connected, listening, idle, closing }; /************************************************************************** - Standard Ctor, takes a transport + Standard Ctor, takes a transport_ **************************************************************************/ this (Conduit trans, bool listener = false) { - transport = trans; + transport_ = trans; ibuf_len = 0; - obuf_len = 0; i_offset = 0; o_offset = 0; + out_buffers = new CircularSeq!(char[]); + log = Log.lookup("dreactor.core.ConnectionHandler"); } /********************************************************************** @@ -117,47 +121,51 @@ Handlers to be called by the SelectLoop when events occur **********************************************************************/ - void handleIncoming(Conduit cond) + int handleIncoming() { - if (!inD is null) - return inD(cond); - else if (!inF is null) - return inF(cond); + if (inD !is null) + return inD(this); + else if (inF !is null) + return inF(this); + else + throw new Exception("no Incoming handler set"); } - void handleOutgoing(Conduit cond) + int handleOutgoing() { - if (!outD is null) - return outD(cond); - else if (!outF is null) - return outF(cond); + if (outD !is null) + return outD(this); + else if (outF !is null) + return outF(this); + else + throw new Exception("no Outgoing handler set"); } - int handleError(Conduit cond) + int handleError(RegisterD reg) { - if (!errD is null) - return errD(cond); - else if (!errF is null) - return errF(cond); + if (errD !is null) + return errD(this, reg); + else if (errF !is null) + return errF(this, reg); } - int handleDisconnect(Conduit cond) + int handleDisconnect() { - if (!disD is null) - return disD(addr); - else if (!disF is null) - return disF(addr); + if (disD !is null) + return disD(this); + else if (disF !is null) + return disF(this); } - int handleConnection(Address addr) + int handleConnection(Conduit cond, RegisterD reg ) { - if (!conD is null) + if (conD !is null) { - return conD(addr); + return conD(cond, reg); } - else if (!conF is null) + else if (conF !is null) { - return conF(addr); + return conF(cond, reg); } } @@ -176,7 +184,7 @@ SelectLoop. If it returns false, it was probably already registered. **************************************************************************/ - synchronized void appendOutBuffer(char[] outbuf) + synchronized bool appendOutBuffer(char[] outbuf) { out_buffers.append(outbuf); out_buffers_len++; @@ -242,12 +250,15 @@ **************************************************************************/ int listen(IPv4Address addr) { - transport.bind().listen(); - state = listening; - setConnectionHandler() + (cast(AsyncSocketConduit)transport_).bind(addr).listen(); + state_ = State.listening; + return 0; } - + Conduit transport() + { + return transport_; + } /************************************************************************** Configuration functions @@ -263,14 +274,16 @@ } void addEvent(Event e) { + log.trace("events_ before: {}", events_); events_ |= e; + log.trace("events_ after: {}", events_); } void remEvent(Event e) { - events_ ^= e; + events_ &= ~e; } - State getState() {return state;} + State getState() {return state_;} /* connection handlers are left out of this because @@ -302,12 +315,12 @@ { hand = freelist; freelist = hand.next; - hand.transport = tran; + hand.transport_ = tran; } else hand = new ConnectionHandler(tran); - if (!other is null) + if (!(other is null)) { hand.setHandlers(other); } @@ -317,20 +330,22 @@ static synchronized void Delete(ConnectionHandler hand) { hand.next = freelist; - freelist = hand.init(); + freelist = hand.initialize(); } private - - char[SZ] in_buffer; + + char[] in_buffer; CircularSeq!(char[]) out_buffers; + int out_buffers_len; int ibuf_len; int i_offset; int o_offset; + Logger log; - package Conduit transport; - State state; - Event events; + package Conduit transport_; + State state_; + Event events_; IncomingHandlerD inD; OutgoingHandlerD outD; ErrorHandlerD errD; @@ -350,15 +365,14 @@ Copy ctor, creates a new ConnectionHandler using the settings of an existing handler. **************************************************************************/ - void init() + ConnectionHandler initialize() { - transport = null; - state = State.init; + transport_ = null; + state_ = State.init; ibuf_len = 0; - obuf_len = 0; i_offset = 0; o_offset = 0; - out_buffer = null; + out_buffers.clear(); inD = null; outD = null; errD = null; @@ -369,6 +383,7 @@ errF = null; disF = null; conF = null; + return this; } }