コード例 #1
0
        private IZmqSocket StartLink(string addr)
        {
            var sock = _zmqCtx.CreateSocket(SocketType.Rep);

            sock.Bind(addr);
            var jss     = new JavaScriptSerializer();
            var polling = new Polling(PollingEvents.RecvReady, sock);
            var watch   = new Stopwatch();

            polling.RecvReady += (socket) =>
            {
                try
                {
                    var    item     = socket.RecvString(Encoding.UTF8);
                    var    dict     = jss.Deserialize <dynamic>(item);
                    string api_name = dict["__api"];
                    var    mt4_id   = 0;
                    if (dict.ContainsKey("mt4UserID"))
                    {
                        mt4_id = Convert.ToInt32(dict["mt4UserID"]);
                    }
                    if (_apiDict.ContainsKey(api_name))
                    {
                        var service    = _apiDict[api_name];
                        var serviceobj = Activator.CreateInstance(service) as IService;
                        using (var server = new ZmqServer(mt4_id, !_attrDict[api_name].DisableMT4))
                        {
                            server.Logger = LogManager.GetLogger("common");
                            if (_attrDict[api_name].ShowRequest)
                            {
                                server.Logger.Info(string.Format("ZMQ,recv request:{0}", item));
                            }
                            watch.Restart();
                            serviceobj.OnRequest(server, dict);
                            if (server.Output != null)
                            {
                                socket.Send(server.Output);
                                watch.Stop();
                                var elsp = watch.ElapsedMilliseconds;
                                if (_attrDict[api_name].ShowResponse)
                                {
                                    server.Logger.Info(string.Format("ZMQ[{0}ms] response:{1}",
                                                                     elsp, server.Output));
                                }
                            }
                            else
                            {
                                socket.Send(string.Empty);
                                watch.Stop();
                                var elsp = watch.ElapsedMilliseconds;
                                if (_attrDict[api_name].ShowResponse)
                                {
                                    server.Logger.Warn(string.Format("ZMQ[{0}ms] response empty,source:{1}",
                                                                     elsp, item));
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    var logger = LogManager.GetLogger("clr_error");
                    logger.Error("处理单个ZMQ请求失败,{0},{1}", e.Message, e.StackTrace);
                    socket.Send(string.Empty);
                }
                finally
                {
                    if (EnableRunning)
                    {
                        ContinuePoll(polling);
                    }
                }
            };
            if (EnableRunning)
            {
                ContinuePoll(polling);
            }
            return(sock);
        }
コード例 #2
0
ファイル: Poll.cs プロジェクト: rebider/MT4Proxy.NET
 public static void PubMessage(string aTopic, string aMessage)
 {
     ZmqServer.PubMessage(aTopic, aMessage);
 }