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


C# ZmqSocket.Disconnect方法代码示例

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


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

示例1: 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

示例2: Start1

        private static void Start1(Object cancelationToken)
        {
            router = context.CreateSocket(SocketType.REQ);
            router.Connect("tcp://127.0.0.1:5000");
            router.ReceiveReady += new EventHandler<SocketEventArgs>(router_ReceiveReady1);
            router.SendReady += new EventHandler<SocketEventArgs>(router_SendReady);

            Poller poller = new Poller(new[] { router });
            var token = (CancellationToken)cancelationToken;

            while (!token.IsCancellationRequested)
            {
                poller.Poll(TimeSpan.FromMilliseconds(100));
            }
            router.Disconnect("tcp://127.0.0.1:5000");
        }
开发者ID:maslakov,项目名称:zmqtests,代码行数:16,代码来源:Program.cs


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