4
|
1 module dreactor.protocol.Raw;
|
|
2
|
|
3 import tango.io.Conduit;
|
|
4 import tango.io.selector.model.ISelector;
|
|
5 import dreactor.core.AsyncConduit;
|
|
6 import dreactor.core.SelectLoop;
|
|
7 import dreactor.core.ConnectionHandler;
|
|
8 import tango.util.collection.CircularSeq;
|
|
9 import tango.util.log.Log;
|
|
10 import tango.util.log.Configurator;
|
|
11
|
|
12 Logger log = Log.getLogger("dreactor.core.SelectLoop");
|
|
13
|
|
14 /******************************************************************************
|
|
15
|
|
16 Basic TCP server or client routines for sending raw data.
|
|
17
|
|
18 ******************************************************************************/
|
|
19 class RawListener
|
|
20 {
|
|
21 public
|
|
22
|
|
23 this(ConnectionHandler mgr, SelectLoop sel)
|
|
24 {
|
|
25 manager = mgr;
|
|
26 mgr.events(Event.Read);
|
|
27 sel.addConnection(mgr);
|
|
28 select = sel;
|
|
29 children = CircularSeq!(ConnectionHandler);
|
|
30 Configurator();
|
|
31 }
|
|
32
|
|
33 int accept(Conduit cond)
|
|
34 {
|
|
35 AsyncConduit newcond = new AsyncConduit;
|
|
36 cond.socket().accept(newcond.socket);
|
|
37 ConnectionHandler h = ConnectionHandler.New(manager);
|
|
38 mgr.events(Event.Read);
|
|
39 select.addConnection(mgr);
|
|
40 children.append(mgr);
|
|
41 }
|
|
42
|
|
43 bool broadcast(char[] outbuf)
|
|
44 {
|
|
45 foreach(ConnectionHandler h; children)
|
|
46 {
|
|
47 if (h.appendBuffer(outbuf))
|
|
48 {
|
|
49 h.addEvent(Event.Write);
|
|
50 select.addConnection(h);
|
|
51 }
|
|
52 }
|
|
53 }
|
|
54
|
|
55 void close()
|
|
56 {
|
|
57
|
|
58 }
|
|
59
|
|
60 /**************************************************************************
|
|
61
|
|
62 send
|
|
63 OutgoingHandlerD
|
|
64 To be registered as the response to socket writable event.
|
|
65 Sends data, returns amount sent. Unregisters Handler for sending
|
|
66 if there is no more data left to send.
|
|
67
|
|
68 ***************************************************************************/
|
|
69 int send(ConnectionHandler h, RegisterD reg)
|
|
70 {
|
|
71 char[] outbuf = h.nextBuffer();
|
|
72 if (!outbuf is null)
|
|
73 {
|
|
74 int sent = h.transport.write(outbuf);
|
|
75 if (sent > 0)
|
|
76 {
|
|
77 if (! h.addOffset(sent))
|
|
78 {
|
|
79 h.removeEvent(Event.write);
|
|
80 reg(h);
|
|
81 }
|
|
82 }
|
|
83 else if (sent == EOF)
|
|
84 {
|
|
85 // EAGAIN ? probably shouldn't have happened.
|
|
86 }
|
|
87 else
|
|
88 {
|
|
89 log.error("Socket send return ERR");
|
|
90 }
|
|
91 return sent;
|
|
92 }
|
|
93 return 0;
|
|
94 }
|
|
95
|
|
96 /**************************************************************************
|
|
97
|
|
98 receive
|
|
99 IncomingHandlerD
|
|
100 Default incoming data handler. Should be replaced with something useful.
|
|
101
|
|
102 **************************************************************************/
|
|
103 int receive(ConnectionHandler h, RegisterD reg)
|
|
104 {
|
|
105 char inbuf[8192];
|
|
106 auto format = Log.format;
|
|
107 if(h.transport.read(inbuf) > 0)
|
|
108 log.info(format("Received Buffer: {}", inbuf));
|
|
109 }
|
|
110
|
|
111 private
|
|
112 ConnectionHandler manager;
|
|
113 SelectLoop select;
|
|
114 CircularSeq!(ConnectionHandler) children;
|
|
115 }
|
|
116
|
|
117 class RawClient
|
|
118 {
|
|
119 public
|
|
120 this(ConnectionHandler mgr, SelectLoop sel)
|
|
121 {
|
|
122 manager = mgr;
|
|
123 mgr.events(Event.Read);
|
|
124 sel.addConnection(mgr);
|
|
125 select = sel;
|
|
126 }
|
|
127
|
|
128
|
|
129
|
|
130 private
|
|
131 ConnectionHandler manager;
|
|
132 SelectLoop select;
|
|
133 }
|