예제 #1
0
 new public byte[] Receive(ref IPEndPoint remoteHost)
 {
     byte[] data = base.Receive(ref remoteHost);
     System.Console.WriteLine(System.DateTime.Now.ToString() + "<<< " + remoteHost);
     System.Console.WriteLine(FormatterHelper.Deserialize(data));
     System.Console.WriteLine();
     return(data);
 }
예제 #2
0
        public async Task Send(byte[] data, int bytes, IPEndPoint remoteHost)
        {
            var client = new UdpSocketClient();
            await client.SendToAsync(data, remoteHost.Address, remoteHost.Port);

            System.Console.WriteLine(System.DateTime.Now.ToString() + ">>> " + remoteHost);
            System.Console.WriteLine(FormatterHelper.Deserialize(data).ToString());
            System.Console.WriteLine();
        }
예제 #3
0
        new public int Send(byte[] data, int bytes, IPEndPoint remoteHost)
        {
            int ret = base.Send(data, bytes, remoteHost);

            System.Console.WriteLine(System.DateTime.Now.ToString() + ">>> " + remoteHost);
            System.Console.WriteLine(FormatterHelper.Deserialize(data));
            System.Console.WriteLine();
            return(ret);
        }
예제 #4
0
 public void PaserCommand(string cmdstring)
 {
     cmdstring = cmdstring.Trim();
     string[] args = cmdstring.Split(new char[] { ' ' });
     if (args.Length > 0)
     {
         if (string.Compare(args[0], "exit", true) == 0)
         {
             LogoutMessage lgoutMsg = new LogoutMessage(myName);
             byte[]        buffer   = FormatterHelper.Serialize(lgoutMsg);
             client.Send(buffer, buffer.Length, hostPoint);
             // do clear something here
             Dispose();
             System.Environment.Exit(0);
         }
         else if (string.Compare(args[0], "send", true) == 0)
         {
             if (args.Length > 2)
             {
                 string toUserName = args[1];
                 string message    = "";
                 for (int i = 2; i < args.Length; i++)
                 {
                     if (args[i] == "")
                     {
                         message += " ";
                     }
                     else
                     {
                         message += args[i];
                     }
                 }
                 if (this.SendMessageTo(toUserName, message))
                 {
                     Console.WriteLine("Send OK!");
                 }
                 else
                 {
                     Console.WriteLine("Send to " + toUserName + " Failed!");
                 }
             }
         }
         else if (string.Compare(args[0], "getu", true) == 0)
         {
             ListUserMessage getUserMsg = new ListUserMessage(myName);
             byte[]          buffer     = FormatterHelper.Serialize(getUserMsg);
             client.Send(buffer, buffer.Length, hostPoint);
         }
         else
         {
             Console.WriteLine("Unknown command {0}", cmdstring);
         }
     }
 }
예제 #5
0
        private async void Run()
        {
            byte[] buffer;
            while (true)
            {
                buffer = await client.Receive(remotePoint);

                object msgObj  = FormatterHelper.Deserialize(buffer);
                Type   msgType = msgObj.GetType();
                if (msgType == typeof(ListUserResponseMessage))
                {
                    // 转换消息
                    ListUserResponseMessage usersMsg = (ListUserResponseMessage)msgObj;
                    // 更新用户列表
                    userList.Clear();
                    foreach (User user in usersMsg.UserList)
                    {
                        userList.Add(user);
                    }
                    this.DisplayUsers(userList);
                }
                else if (msgType == typeof(PingMessage))
                {
                    // 转换消息
                    PingMessage purchReqMsg = (PingMessage)msgObj;
                    // 发送打洞消息到远程主机
                    TrashMessage trashMsg = new TrashMessage();
                    buffer = FormatterHelper.Serialize(trashMsg);
                    client.Send(buffer, buffer.Length, purchReqMsg.RemotePoint);
                }
                else if (msgType == typeof(P2PTextMessage))
                {
                    // 转换消息
                    P2PTextMessage workMsg = (P2PTextMessage)msgObj;
                    Console.WriteLine("Receive a message: {0}", workMsg.Message);
                    // 发送应答消息
                    P2PAckMessage ackMsg = new P2PAckMessage();
                    buffer = FormatterHelper.Serialize(ackMsg);
                    client.Send(buffer, buffer.Length, remotePoint);
                }
                else if (msgType == typeof(P2PAckMessage))
                {
                    this.ReceivedACK = true;
                }
                else if (msgType == typeof(TrashMessage))
                {
                    Console.WriteLine("Recieve a trash message");
                }
                Thread.Sleep(100);
            }
        }
예제 #6
0
        public async Task <byte[]> Receive(IPEndPoint remoteHost)
        {
            var receiver = new UdpSocketReceiver();

            byte[] data = new byte[1];

            receiver.MessageReceived += (sender, args) =>
            {
                // get the remote endpoint details and convert the received data into a string
                data = args.ByteData;
            };
            await receiver.StartListeningAsync(remoteHost.Port);

            System.Console.WriteLine(System.DateTime.Now.ToString() + "<<< " + remoteHost);
            System.Console.WriteLine(FormatterHelper.Deserialize(data).ToString());
            System.Console.WriteLine();
            return(data);
        }
예제 #7
0
        /// <summary>
        /// 这是主要的函数:发送一个消息给某个用户(C)
        /// 流程:直接向某个用户的外网IP发送消息,如果此前没有联系过
        /// 那么此消息将无法发送,发送端等待超时。
        /// 超时后,发送端将发送一个请求信息到服务端,要求服务端发送
        /// 给客户C一个请求,请求C给本机发送打洞消息
        /// 以上流程将重复MAXRETRY次
        /// </summary>
        /// <param name="toUserName">对方用户名</param>
        /// <param name="message">待发送的消息</param>
        /// <returns></returns>
        private bool SendMessageTo(string toUserName, string message)
        {
            User toUser = userList.Find(toUserName);

            if (toUser == null)
            {
                return(false);
            }
            for (int i = 0; i < MAXRETRY; i++)
            {
                P2PTextMessage workMsg = new P2PTextMessage(message);
                byte[]         buffer  = FormatterHelper.Serialize(workMsg);
                client.Send(buffer, buffer.Length, toUser.NetPoint);

                // 等待接收线程将标记修改
                for (int j = 0; j < 10; j++)
                {
                    if (this.ReceivedACK)
                    {
                        this.ReceivedACK = false;
                        return(true);
                    }
                    else
                    {
                        Thread.Sleep(300);
                    }
                }

                // 没有接收到目标主机的回应,认为目标主机的端口映射没有
                // 打开,那么发送请求信息给服务器,要服务器告诉目标主机
                // 打开映射端口(UDP打洞)
                TranslateMessage transMsg = new TranslateMessage(myName, toUserName);
                buffer = FormatterHelper.Serialize(transMsg);
                client.Send(buffer, buffer.Length, hostPoint);

                // 等待对方先发送信息
                Thread.Sleep(100);
            }
            return(false);
        }
예제 #8
0
        public void ConnectToServer(string userName, string password)
        {
            myName = userName;

            // 发送登录消息到服务器
            LoginMessage lginMsg = new LoginMessage(userName, password);

            byte[] buffer = FormatterHelper.Serialize(lginMsg);
            client.Send(buffer, buffer.Length, hostPoint);

            // 接受服务器的登录应答消息
            buffer = client.Receive(ref remotePoint);
            ListUserResponseMessage srvResMsg = (ListUserResponseMessage)FormatterHelper.Deserialize(buffer);

            // 更新用户列表
            userList.Clear();
            foreach (User user in srvResMsg.UserList)
            {
                userList.Add(user);
            }

            this.DisplayUsers(userList);
        }
예제 #9
0
        private async void Run()
        {
            byte[] buffer = null;
            while (true)
            {
                byte[] msgBuffer = await server.Receive(remotePoint);

                try
                {
                    object msgObj  = FormatterHelper.Deserialize(msgBuffer);
                    Type   msgType = msgObj.GetType();
                    if (msgType == typeof(LoginMessage))
                    {
                        // 转换接受的消息
                        LoginMessage lginMsg = (LoginMessage)msgObj;
                        Console.WriteLine("{0}: user {1} sign in.", System.DateTime.Now.ToString(), lginMsg.UserName);

                        // 添加用户到列表
                        IPEndPoint userEndPoint = new IPEndPoint(remotePoint.Address, remotePoint.Port);
                        User       user         = new User(lginMsg.UserName, userEndPoint);
                        userList.Add(user);

                        // 发送应答消息
                        ListUserResponseMessage usersMsg = new ListUserResponseMessage(userList);
                        buffer = FormatterHelper.Serialize(usersMsg);
                        server.Send(buffer, buffer.Length, remotePoint);
                        Console.WriteLine("Send:" + usersMsg);
                    }
                    else if (msgType == typeof(LogoutMessage))
                    {
                        // 转换接受的消息
                        LogoutMessage lgoutMsg = (LogoutMessage)msgObj;
                        Console.WriteLine("{0}: {1} sign out", System.DateTime.Now.ToString(), lgoutMsg.UserName);

                        // 从列表中删除用户
                        User lgoutUser = userList.Find(lgoutMsg.UserName);
                        if (lgoutUser != null)
                        {
                            userList.Remove(lgoutUser);
                        }
                    }
                    else if (msgType == typeof(TranslateMessage))
                    {
                        // 转换接受的消息
                        TranslateMessage transMsg = (TranslateMessage)msgObj;
                        Console.WriteLine("{0}(1) wants to p2p {2}", remotePoint.Address.ToString(), transMsg.UserName, transMsg.ToUserName);

                        // 获取目标用户
                        User toUser = userList.Find(transMsg.ToUserName);

                        // 转发Purch Hole请求消息
                        if (toUser == null)
                        {
                            Console.WriteLine("Remote host {0} cannot be found at index server", transMsg.ToUserName);
                        }
                        else
                        {
                            PingMessage transMsg2 = new PingMessage(remotePoint);
                            buffer = FormatterHelper.Serialize(transMsg);
                            server.Send(buffer, buffer.Length, toUser.NetPoint);
                            Console.WriteLine("Send:" + transMsg2);
                        }
                    }
                    else if (msgType == typeof(ListUserMessage))
                    {
                        // 发送当前用户信息到所有登录客户
                        ListUserResponseMessage srvResMsg = new ListUserResponseMessage(userList);
                        buffer = FormatterHelper.Serialize(srvResMsg);
                        foreach (User user in userList.InnerList)
                        {
                            server.Send(buffer, buffer.Length, user.NetPoint);
                            Console.WriteLine("Send:" + srvResMsg);
                        }
                    }
                    Thread.Sleep(500);
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e.Message);
                    System.Console.WriteLine(e.StackTrace);
                }
            }
        }