Пример #1
0
        private void Receive()
        {
            while (isStart)
            {
                try
                {
                    var     bytes = udpClient.Receive(ref remote);
                    Message msg   = Common.Deserialize <Message>(bytes);
                    if (msg == null)
                    {
                        continue;
                    }
                    switch (msg.type)
                    {
                    case MessageType.PING:
                    {
                        if (server.EqualsValue(remote))
                        {
                            Interlocked.Exchange(ref pingNum, 0);
                        }
                    }
                    break;

                    case MessageType.LOGIN:
                    {
                        if (server.EqualsValue(remote))
                        {
                            currentUser.id = new Guid(msg.content);
                            Console.WriteLine("用户Id已更新:" + currentUser.id);
                        }
                    }
                    break;

                    case MessageType.LIST:
                    {
                        if (server.EqualsValue(remote))
                        {
                            dcUser = Common.Deserialize <Dictionary <Guid, User> >(msg.content);
                            OnUpdateUser(new UserList {
                                    dictinary = dcUser
                                });
                        }
                    }
                    break;

                    case MessageType.P2PSOMEONECALLYOU:
                    {
                        if (server.EqualsValue(remote))
                        {
                            if (dcUser.ContainsKey(msg.userId))
                            {
                                bytes = Common.Serialize(new Message {
                                        type = MessageType.P2PACCEPT, userId = currentUser.id
                                    });
                                udpClient.Send(bytes, bytes.Length, dcUser[msg.userId].address);
                                Console.WriteLine("P2PSOMEONECALLYOU收到来自服务器P2P请求申请,来自用户{0}[{1}]", dcUser[msg.userId].userName, dcUser[msg.userId].address);
                            }
                        }
                    }
                    break;

                    case MessageType.MSG:
                    {
                        if (dcUser.ContainsKey(msg.userId) && dcUser[msg.userId].address.EqualsValue(remote))
                        {
                            bytes = Common.Serialize(new Message {
                                    type = MessageType.ACK, userId = currentUser.id
                                });
                            udpClient.Send(bytes, bytes.Length, dcUser[msg.userId].address);
                            OnReceiveMessage(new TextMessage {
                                    userName = dcUser[msg.userId].userName, content = Encoding.UTF8.GetString(msg.content), address = remote
                                });
                        }
                    }
                    break;

                    case MessageType.ACK:
                    {
                        if (dcUser.ContainsKey(msg.userId) && dcUser[msg.userId].address.EqualsValue(remote))
                        {
                            if (dcAck.ContainsKey(msg.userId) && dcAck[msg.userId].ack == 0)
                            {
                                dcAck[msg.userId].ack = 1;
                            }
                            Console.WriteLine("ACK收到来自用户:{0}[{1}]的消息已送达回执", dcUser[msg.userId].userName, dcUser[msg.userId].address);
                        }
                    }
                    break;

                    case MessageType.P2PACCEPT:
                    {
                        Console.WriteLine("P2PACCEPT收到来自p2p申请用户的回执,来自用户{0}[{1}]", dcUser[msg.userId].userName, dcUser[msg.userId].address);
                        //收到这个消息代表P2P打洞已建立,如果有缓存消息发送出去
                        if (dcAck[msg.userId].buffer != null)
                        {
                            udpClient.Send(dcAck[msg.userId].buffer, dcAck[msg.userId].buffer.Length, dcUser[msg.userId].address);
                            dcAck[msg.userId].buffer = null;
                            Console.WriteLine("发送缓存消息");
                        }
                    }
                    break;
                    }
                }
                catch (SocketException ex)
                {
                    Console.WriteLine(ex.Message);
                    Thread.Sleep(1000);
                    continue;
                }
            }
        }
Пример #2
0
        private void Receive()
        {
            while (isStart)
            {
                var     bytes = udpClient.Receive(ref remote);
                Message msg   = Common.Deserialize <Message>(bytes);
                if (msg == null)
                {
                    continue;
                }
                lock (lockobject)
                {
                    switch (msg.type)
                    {
                    case MessageType.PING:
                    {
                        if (dcUser.ContainsKey(msg.userId))
                        {
                            bytes = Common.Serialize(new Message {
                                    type = MessageType.PING
                                });
                            udpClient.Send(bytes, bytes.Length, dcUser[msg.userId].address);
                            dcPingNum[msg.userId] = 1;
                            if (!remote.EqualsValue(dcUser[msg.userId].address))
                            {
                                //外网端口可能发生变化如果长时间没有连接的话,但是一直发送ping的时候发现端口没有变化
                                Console.WriteLine("地址发生变化,用户{0}[{1}],真实地址:{2}", dcUser[msg.userId].userName, dcUser[msg.userId].address, remote);
                                dcUser[msg.userId].address = remote;
                            }
                        }
                    }
                    break;

                    case MessageType.LOGIN:
                    {
                        var user = new User {
                            id = Guid.NewGuid(), userName = Encoding.UTF8.GetString(msg.content), address = remote
                        };
                        dcUser.Add(user.id, user);
                        dcPingNum.Add(user.id, 1);
                        bytes = Common.Serialize(new Message {
                                type = MessageType.LOGIN, content = user.id.ToByteArray()
                            });
                        udpClient.Send(bytes, bytes.Length, user.address);
                        Console.WriteLine("已发送login到{0}[{1}],guid:{2}", user.userName, user.address, user.id);
                        SendUserList();
                    }
                    break;

                    case MessageType.LIST:
                    {
                        if (dcUser.ContainsKey(msg.userId))
                        {
                            var content = Common.Serialize(dcUser);
                            bytes = Common.Serialize(new Message {
                                    type = MessageType.LIST, content = content
                                });
                            udpClient.Send(bytes, bytes.Length, remote);
                            Console.WriteLine("已发送list到{0}[{1}],真实发送地址:{2}", dcUser[msg.userId].userName, dcUser[msg.userId].address, remote);
                        }
                    }
                    break;

                    case MessageType.P2PREQUEST:
                    {
                        if (dcUser.ContainsKey(msg.userId))
                        {
                            var guid = Guid.Parse(Encoding.UTF8.GetString(msg.content));
                            if (dcUser.ContainsKey(guid))
                            {
                                bytes = Common.Serialize(new Message {
                                        type = MessageType.P2PSOMEONECALLYOU, userId = msg.userId
                                    });
                                udpClient.Send(bytes, bytes.Length, dcUser[guid].address);
                                Console.WriteLine("已发送P2P请求到{0}[{1}],发送人:{2}[{3}],发送人真实地址:{4}", dcUser[guid].userName, dcUser[guid].address, dcUser[msg.userId].userName, dcUser[msg.userId].address, remote);
                            }
                        }
                    }
                    break;

                    case MessageType.EXIT:
                    {
                        if (dcUser.ContainsKey(msg.userId))
                        {
                            Console.WriteLine("收到用户{0}[{1}]的离线通知,真实地址{2}", dcUser[msg.userId].userName, dcUser[msg.userId].address, remote);
                            dcUser.Remove(msg.userId);
                            dcPingNum.Remove(msg.userId);
                            SendUserList();
                        }
                    }
                    break;
                    }
                }
            }
        }