Beispiel #1
0
        static void Main(string[] args)
        {
            UDPServerRouteHelper.GetInstanse().SetDelegate(new LoginSocketDelegate());
            UDPServerRouteHelper.GetInstanse().SetDelegate(new GetUserInfoSocketDelegate());
            UDPServerRouteHelper.GetInstanse().SetDelegate(new GetUserFriendListSocketDelegate());
            UDPServerRouteHelper.GetInstanse().SetDelegate(new ChatTextMessageSocketDelegate());

            UDPServer udpServer = new UDPServer();

            udpServer.BindIPPoint = new IPEndPoint(IPAddress.Any, 7788);

            KevRegister.Add(UDPPrimaryKey.UDPServer, udpServer);

            udpServer.Start();

            while (true)
            {
                Console.WriteLine("Command:");
                bool isContinue = true;
                switch (Console.ReadLine().ToUpper())
                {
                case "EXIT":
                    isContinue = false;
                    break;

                default:
                    break;
                }
                if (!isContinue)
                {
                    break;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="state"></param>
        private void HandleMessage(object state)
        {
            UDPModel uModel = state as UDPModel;

            if (uModel == null)
            {
                return;
            }

            string message = Encoding.UTF8.GetString(uModel.Body, 0, uModel.Length);

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            Socket socket = SocketRegister.Get(SocketClient);

            if (socket == null)
            {
                return;
            }

            if (message == Handshake2)
            {
                UDPSocketServer.SendMessage(socket, Handshake3, uModel.IPPoint);
            }

            KevSocketModel ksModel = JsonHelper.ParseFromJson <KevSocketModel>(message);

            if (ksModel == null)
            {
                return;
            }

            Console.WriteLine("HandleMessage" + ksModel.MessageId + "::" + ksModel.MessageType);

            if (ksModel.NetworkType == NetworkType.Request)
            {
                long clientId = KevRegister.Get <long>(UDPPrimaryKey.Client_ThisDeviceId);

                //非指向本机请求一律抛弃
                if (ksModel.ReceiveDeviceId != clientId)
                {
                    return;
                }

                int responseCode = UDPClientRouteHelper.GetInstanse().HandleRequestMessage(ksModel, uModel);
                if (responseCode == ResponseCode.NoResponse)
                {
                    return;
                }

                ksModel.NetworkType  = NetworkType.Response;
                ksModel.ResponseCode = responseCode;

                //做出相应

                UDPSocketServer.SendMessage(socket, ksModel, uModel.IPPoint);
            }

            if (ksModel.NetworkType == NetworkType.Response)
            {
                //从消息盒子中移除数据
                KevMessageBoxModel kmbModel = KevMessageBox.GetMessage(ksModel.MessageId);
                if (kmbModel != null)
                {
                    UDPClientRouteHelper.GetInstanse().HandleResponseMessage(ksModel, uModel);
                }
            }
        }