示例#1
0
 private void pong(Client tc, TCPClientMessage msg)
 {
     /* This is async method */
     tc.SetLastTimestamp(DateTimeOffset.Now.ToUnixTimeSeconds());
     tc.SetLatency(
         DateTimeOffset.Now.ToUnixTimeMilliseconds() - msg.RemoteTime
         );
 }
示例#2
0
        private void SendActual()
        {
            TCPClientMessage sendPacket = new TCPClientMessage();

            sendPacket.RemoteTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            sendPacket.Cmd        = TCPClientCmd.Actual;
            this.sendQueue.Enqueue(sendPacket);
        }
示例#3
0
        private void ServerThread()
        {
            while (true)
            {
                Thread.Sleep(1);

                if (server.Pending())
                {
                    TcpClient tc = server.AcceptTcpClient();
                    server.Start();
                    clients.Add(new Client(tc));

                    this.controller.RefreshList();
                }


                try
                {
                    foreach (Client c in this.clients)
                    {
                        try
                        {
                            NetworkStream stream = c.GetSocket().GetStream();
                            if (stream.CanRead && stream.DataAvailable)
                            {
                                IFormatter       formatter  = new BinaryFormatter();
                                TCPClientMessage recvPacket = new TCPClientMessage();
                                recvPacket = (TCPClientMessage)formatter.Deserialize(stream);

                                if (recvPacket.Cmd == TCPClientCmd.PONG)
                                {
                                    this.pong(c, recvPacket);
                                }
                                else if (recvPacket.Cmd == TCPClientCmd.Actual)
                                {
                                    this.AuthClient(c);
                                }
                            }
                        }
                        catch (SocketException) {
                            CloseClient(c);
                        }
                    }
                } catch (Exception e)
                {
                    Debug.Print(e.Message);
                }
            }
        }
示例#4
0
        private void MainThread()
        {
            while (true)
            {
                Thread.Sleep(1);
                // Receiving
                NetworkStream stream = mainTc.GetStream();

                if (stream.CanRead && stream.DataAvailable)
                {
                    IFormatter       formatter  = new BinaryFormatter();
                    TCPServerMessage recvPacket = new TCPServerMessage();
                    recvPacket = (TCPServerMessage)formatter.Deserialize(stream);

                    if (recvPacket.Cmd == TCPServerCmd.PING)
                    {
                        TCPClientMessage sendPacket = new TCPClientMessage(recvPacket);
                        sendPacket.Cmd = TCPClientCmd.PONG;
                        formatter.Serialize(stream, sendPacket);
                    }
                    else if (recvPacket.Cmd == TCPServerCmd.Message)
                    {
                        string tmp_data = recvPacket.Param;
                        Debug.Print(tmp_data);
                        var json = JObject.Parse(tmp_data);

                        controller.ShowMessage(json["content"].ToString());
                    }
                }
                {
                    // Sending
                    if (this.sendQueue.Count == 0)
                    {
                        continue;
                    }
                    TCPClientMessage task = this.sendQueue.Dequeue();

                    IFormatter formatter = new BinaryFormatter();
                    task.RemoteTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
                    formatter.Serialize(stream, task);
                }
            }
        }