Roger Meier | 87e4980 | 2011-04-19 19:47:03 +0000 | [diff] [blame] | 1 | using System; |
| 2 | using Thrift; |
| 3 | using Thrift.Server; |
| 4 | using Thrift.Transport; |
| 5 | using Thrift.Protocol; |
| 6 | using ZMQ; |
| 7 | using System.IO; |
| 8 | |
| 9 | using System.Collections.Generic; |
| 10 | |
| 11 | namespace ZmqServer |
| 12 | { |
| 13 | public class TZmqServer |
| 14 | { |
| 15 | Socket _socket ; |
| 16 | TProcessor _processor; |
| 17 | |
| 18 | void debug (string msg) |
| 19 | { |
| 20 | //Uncomment to enable debug |
| 21 | // Console.WriteLine (msg); |
| 22 | } |
| 23 | |
| 24 | public TZmqServer (TProcessor processor, Context ctx, String endpoint, SocketType sockType) |
| 25 | { |
| 26 | new TSimpleServer (processor,null); |
| 27 | _socket = ctx.Socket (sockType); |
| 28 | _socket.Bind (endpoint); |
| 29 | _processor = processor; |
| 30 | } |
| 31 | |
| 32 | public void ServeOne () |
| 33 | { |
| 34 | debug ("Server_ServeOne"); |
| 35 | Byte[] msg = _socket.Recv (); |
| 36 | MemoryStream istream = new MemoryStream (msg); |
| 37 | MemoryStream ostream = new MemoryStream (); |
| 38 | TProtocol tProtocol = new TBinaryProtocol (new TStreamTransport (istream, ostream)); |
| 39 | _processor.Process (tProtocol, tProtocol); |
| 40 | |
| 41 | if (ostream.Length != 0) { |
| 42 | byte[] newBuf = new byte[ostream.Length]; |
| 43 | Array.Copy (ostream.GetBuffer (), newBuf, ostream.Length); |
| 44 | debug (string.Format ("Server_ServeOne sending {0}b", ostream.Length)); |
| 45 | _socket.Send (newBuf); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public void Serve () |
| 50 | { |
| 51 | while (true) |
| 52 | ServeOne (); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |