Exemplo n.º 1
0
        /// <summary>
        /// 获取单例
        /// </summary>
        /// <returns></returns>
        public static UDPClientRouteHelper GetInstanse()
        {
            if (_helper == null)
            {
                _helper = new UDPClientRouteHelper();
            }

            return(_helper);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 处理超时消息
        /// </summary>
        /// <param name="state"></param>
        private void HandleTimeOutMessage(object state)
        {
            KevMessageBoxModel ksbModel = state as KevMessageBoxModel;

            if (ksbModel == null)
            {
                return;
            }

            UDPClientRouteHelper.GetInstanse().HandleTimeOutMessage(ksbModel);
        }
Exemplo n.º 3
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);
                }
            }
        }