当前位置: 首页>>代码示例>>C#>>正文


C# ZmqSocket.Dispose方法代码示例

本文整理汇总了C#中ZmqSocket.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# ZmqSocket.Dispose方法的具体用法?C# ZmqSocket.Dispose怎么用?C# ZmqSocket.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ZmqSocket的用法示例。


在下文中一共展示了ZmqSocket.Dispose方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Connect

        public void Connect()
        {
            _socket = _context.CreateSocket(SocketType.PUSH);
            _socket.SendHighWatermark = _options.SendHighWaterMark;
            _socket.TcpKeepalive = TcpKeepaliveBehaviour.Enable;
            _socket.TcpKeepaliveIdle = 30;
            _socket.TcpKeepaliveIntvl = 3;
            _socket.SetPeerId(_peerId);

            try
            {
                _socket.Connect(EndPoint);
            }
            catch
            {
                _socket.Dispose();
                throw;
            }

            _logger.InfoFormat("Socket connected, Peer: {0}, EndPoint: {1}", _peerId, EndPoint);
            _isConnected = true;
        }
开发者ID:MarouenK,项目名称:Zebus,代码行数:22,代码来源:ZmqOutboundSocket.cs

示例2: ClientThread

        private void ClientThread()
        {
            try
            {

                status = 1;
                context = ZmqContext.Create();
                socket = context.CreateSocket(SocketType.DEALER);
                window.SetProgress(5);
                window.SetStatus("Contacting server...");
                window.Log("Attempting connection to server...");
                window.Log("   --> " + Settings.Default.ServerAddress);
                bool connected = false;
                {
                    byte[] identity = new byte[15];
                    new Random().NextBytes(identity);
                    socket.Identity = identity;
                    var helloMessage = new[] { (byte)MessageIdentifier.Init };
                    int attempt = 1;
                    var endpoint = Settings.Default.ServerAddress;
                    socket.Connect(endpoint);
                    socket.Send(helloMessage);
                    while (!connected)
                    {
                        var message = socket.ReceiveMessage(TimeSpan.FromSeconds(5));
                        if (message.TotalSize == 0)
                        {
                            window.Log("(" + attempt + ") Failed to connect, trying again in 5 seconds...");
                        }
                        else
                        {
                            window.Log("Received response, verifying...");

                            //check command value
                            if (message.First.Buffer[0] == (byte)MessageIdentifier.SetIdentity)
                            {
                                assignedIdentity = message.First.Buffer.Skip(1).ToArray();
                                window.Log("Received a new " + assignedIdentity.Length + " byte identity.");
                                socket.Identity = assignedIdentity;
                                socket.Disconnect(endpoint);
                                var finalSocket = context.CreateSocket(SocketType.DEALER);
                                socket.Dispose();
                                socket = finalSocket;
                                socket.Identity = assignedIdentity;
                                socket.TcpKeepalive = TcpKeepaliveBehaviour.Enable;
                                socket.Connect(endpoint);
                                socket.Send(new byte[] { (byte)MessageIdentifier.SetIdentity });
                                break;
                            }
                            if (message.First.Buffer[0] == (byte)MessageIdentifier.InvalidIdentity)
                            {
                                window.Log("Server responded with invalid identity. Trying again.");
                            }
                        }
                        attempt++;
                        Thread.Sleep(5000);
                    }
                    window.Log("Connected to master server after " + attempt + " attempts.");
                    window.SetProgress(20);
                    window.SetStatus("Synchronizing encryption...");

                }
                //We are connected!
                window.Log("Waiting for encryption sequence...");
                ZmqMessage msg = socket.ReceiveMessage();
                if (msg.First.Buffer[0] != (byte)MessageIdentifier.BeginEncryption)
                {
                    LogUnexpectedMessage(msg.First.Buffer);
                    window.Log("Crucial unexpected result, disconnecting.");
                    socket.Send(new byte[] { (byte)MessageIdentifier.Disconnect });
                    status = 0;
                    socket.Dispose();
                    context.Dispose();
                    return;
                }

                window.Log("Sending encryption key...");
                byte[] encryptMsg = new byte[1 + keyHash.Length];
                encryptMsg[0] = (byte)MessageIdentifier.BeginEncryption;
                keyHash.CopyTo(encryptMsg, 1);
                socket.Send(encryptMsg);

                window.Log("Waiting for response...");

                msg = socket.ReceiveMessage();

                if (msg.First.Buffer[0] != (byte)MessageIdentifier.ConfirmEncryption)
                {
                    window.Log("Invalid encryption key. Exiting...");
                    status = 0;
                    socket.Send(BuildMessage(MessageIdentifier.Disconnect, null, false));
                    return;
                }

                string salt = Encoding.UTF8.GetString(DecryptMessage(msg.First.Buffer.Skip(1).ToArray()));

                heartbeat.Start();

                window.Log("Connected, encrypted, beginning login.");
                window.SetStatus("Logging in...");
//.........这里部分代码省略.........
开发者ID:paralin,项目名称:MatrixServer,代码行数:101,代码来源:ClientManager.cs

示例3: ContractServer

        /// <summary>
        /// The main loop. Runs on its own thread. Accepts requests on the REP socket, gets results from the InstrumentManager,
        /// and sends them back right away.
        /// </summary>
        private void ContractServer()
        {
            var timeout = new TimeSpan(100000);
            _socket = _context.CreateSocket(SocketType.REP);
            _socket.Bind("tcp://*:" + _socketPort);
            var ms = new MemoryStream();
            List<Instrument> instruments;

            while (_runServer)
            {
                string request = _socket.Receive(Encoding.UTF8, timeout);
                if (request == null) continue;

                //if the request is for a search, receive the instrument w/ the search parameters and pass it to the searcher
                if (request == "SEARCH" && _socket.ReceiveMore)
                {
                    int size;
                    byte[] buffer = _socket.Receive(null, timeout, out size);
                    var searchInstrument = MyUtils.ProtoBufDeserialize<Instrument>(buffer, ms);

                    Log(LogLevel.Info, string.Format("Instruments Server: Received search request: {0}",
                        searchInstrument));

                    try
                    {
                        instruments = _instrumentManager.FindInstruments(null, searchInstrument);
                    }
                    catch (Exception ex)
                    {
                        Log(LogLevel.Error, string.Format("Instruments Server: Instrument search error: {0}",
                            ex.Message));
                        instruments = new List<Instrument>();
                    }
                }
                else if (request == "ALL") //if the request is for all the instruments, we don't need to receive anything else
                {
                    Log(LogLevel.Info, "Instruments Server: received request for list of all instruments.");
                    instruments = _instrumentManager.FindInstruments();
                }
                else if (request == "ADD") //request to add instrument
                {
                    int size;
                    byte[] buffer = _socket.Receive(null, timeout, out size);
                    var instrument = MyUtils.ProtoBufDeserialize<Instrument>(buffer, ms);

                    bool addResult;
                    try
                    {
                        addResult = InstrumentManager.AddInstrument(instrument);
                    }
                    catch (Exception ex)
                    {
                        addResult = false;
                        Log(LogLevel.Error, string.Format("Instruments Server: Instrument addition error: {0}",
                            ex.Message));
                    }
                    _socket.Send(addResult ? "SUCCESS" : "FAILURE", Encoding.UTF8);

                    continue;
                }
                else //no request = loop again
                {
                    continue;
                }

                byte[] uncompressed = MyUtils.ProtoBufSerialize(instruments, ms);//serialize the list of instruments
                ms.Read(uncompressed, 0, (int)ms.Length); //get the uncompressed data
                byte[] result = LZ4Codec.Encode(uncompressed, 0, (int)ms.Length); //compress it

                //before we send the result we must send the length of the uncompressed array, because it's needed for decompression
                _socket.SendMore(BitConverter.GetBytes(uncompressed.Length));

                //then finally send the results
                _socket.Send(result);
            }

            _socket.Dispose();
        }
开发者ID:KeithNel,项目名称:qdms,代码行数:82,代码来源:InstrumentsServer.cs

示例4: processMessages

        /// <summary>
        /// Processes the incoming message queue.
        /// </summary><remarks>
        /// Runs on a worker thread.
        /// </remarks>
        private void processMessages()
        {
            // We create a socket to receive messages from the
            // Python server...
            m_receiveSocket = m_context.CreateSocket(SocketType.SUB);
            m_receiveSocket.Connect("tcp://localhost:12345");
            m_receiveSocket.SubscribeAll();

            TimeSpan timeout = new TimeSpan(0, 0, 0, 0, 1);
            while (m_stopThread == false)
            {
                // We see if there is a message...
                int bytesReceived = m_receiveSocket.Receive(m_buffer, timeout);
                if (bytesReceived == -1)
                {
                    // There was no message in the queue...
                    continue;
                }

                // We got a message, so we decode it...
                switch (m_buffer[0])
                {
                    case 0:
                        // The 'Hello' message from the server...
                        handleHelloMessage();
                        break;

                    case 1:
                        // The start-of-tournament message...
                        decodeStartOfTournamentMessage(bytesReceived);
                        break;

                    case 2:
                        // The start-of-game message...
                        Utils.raiseEvent(StartOfGameEvent, this, null);
                        break;

                    case 3:
                        // The player-info message...
                        decodePlayerInfoMessage(bytesReceived);
                        break;

                    case 4:
                        // The board-update message...
                        decodeBoardUpdateMessage(bytesReceived);
                        break;

                    default:
                        // An unknown message (or maybe the "Hello" message after
                        // we connect. We ignore this...
                        break;
                }
            }

            // We close the socket before exiting the thread...
            m_receiveSocket.Linger = new TimeSpan(0);
            m_receiveSocket.Dispose();
        }
开发者ID:kkanagal,项目名称:monopyly,代码行数:63,代码来源:MessagingClient.cs


注:本文中的ZmqSocket.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。