Пример #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;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="state"></param>
        private void HandleMessage(object state)
        {
            UDPModel uModel = state as UDPModel;

            if (uModel == null)
            {
                return;
            }

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

                Socket socket = SocketRegister.Get(UDPPrimaryKey.UDPServerSocket);
                if (socket == null)
                {
                    break;
                }

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

                //三次握手

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

                if (message == Handshake3)
                {
                    break;
                }

                KevSocketModel ksModel = JsonHelper.ParseFromJson <KevSocketModel>(message);
                if (ksModel == null)
                {
                    break;
                }

                int responseCode = UDPServerRouteHelper.GetInstanse().HandleMessage(ksModel, uModel);
                if (ksModel.NetworkType == NetworkType.Request && responseCode != ResponseCode.NoResponse)
                {
                    ksModel.NetworkType  = NetworkType.Response;
                    ksModel.ResponseCode = responseCode;

                    //做出响应
                    UDPSocketServer.SendMessage(socket, ksModel, uModel.IPPoint);
                }
            } while (false);
        }