コード例 #1
0
ファイル: BaseListener.cs プロジェクト: ezrover/clrzmq2
 protected abstract byte[] GetReplyFor(byte[] request, ZSocket socket);
コード例 #2
0
        private void Worker()
        {
            try
            {
                var config = GetConfig();

                socket = ContextAccessor.SocketFactory(SocketType.REP);

                socket.Bind(config.Transport, config.Ip, config.Port);

                Logger.InfoFormat("Binding {0} on {1}:{2}", GetType().Name, config.Ip, config.Port);

                AcceptAndHandleMessage(socket);
            }
            catch (System.Exception e)
            {
                Logger.Fatal("Error on " + GetType().Name + " background thread", e);
            }
            finally
            {
                CloseSocket();
            }
        }
コード例 #3
0
        protected void AcceptAndHandleMessage(ZSocket zSocket)
        {
            try
            {
                while (!disposed)
                {
                    var watch = new Stopwatch();

                    if (Logger.IsDebugEnabled)
                        watch.Start();

                    try
                    {
                        var bytes = zSocket.Recv(ZSocket.InfiniteTimeout);

                        byte[] reply = null;

                        receivedCounter.Increment();

                        try
                        {
                            reply = bytes == null ? new byte[0] : GetReplyFor(bytes, zSocket);
                        }
                        catch (Exception e)
                        {
                            Logger.Error("Error getting reply.", e);
                        }
                        finally
                        {
                            zSocket.Send(reply ?? new byte[0]);

                            sentCounter.Increment();
                        }
                    }
                    finally
                    {
                        watch.Stop();

                        timerReplyCounter.IncrementBy(watch.ElapsedTicks);
                        baseReplyCounter.Increment();

                        if (Logger.IsDebugEnabled)
                            Logger.Debug("Listener Recv Took: " + watch.ElapsedMilliseconds);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Fatal("Error on working thread", e);
            }
        }
コード例 #4
0
        private void CloseSocket()
        {
            try
            {
                if (socket == null) return;

                socket.Dispose();
                socket = null;
            }
            catch (Exception e)
            {
                Logger.Warn("Error closing socket.", e);
            }
        }