blob: 535c623d078596133cc67627b541dd9db916b3ab [file] [log] [blame]
Roger Meier87e49802011-04-19 19:47:03 +00001using System;
2using Thrift;
3using Thrift.Server;
4using Thrift.Transport;
5using Thrift.Protocol;
6using ZMQ;
7using System.IO;
8
9using System.Collections.Generic;
10
11namespace 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