Exemplo n.º 1
0
        /// <summary>
        /// 发送消息给服务器
        /// </summary>
        /// <param name="ksModel"></param>
        /// <returns></returns>
        public bool SendMessage <T>(KevSocketModel <T> ksModel)
        {
            Socket socket = SocketRegister.Get(SocketClient);

            if (socket == null)
            {
                return(false);
            }

            bool isSuccess = UDPSocketServer.SendMessage(socket, ksModel, this.ServerIPPoint);

            if (isSuccess)
            {
                Console.WriteLine("SendMessage" + ksModel.MessageId + "::" + ksModel.MessageType);

                KevMessageBox.Enqueue(ksModel.MessageId, new KevMessageBoxModel
                {
                    IP          = ServerIPPoint,
                    SocketModel = ksModel,
                    TimeOut     = DateTime.Now.AddSeconds(this.TimeOutSeconds)
                });
            }

            return(isSuccess);
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 发送消息给服务器
        /// </summary>
        /// <param name="ksModel"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool SendMessage <T>(KevSocketModel <T> ksModel, IPEndPoint ip)
        {
            Socket socket = SocketRegister.Get(UDPPrimaryKey.UDPServerSocket);

            if (socket == null)
            {
                return(false);
            }

            return(UDPSocketServer.SendMessage(socket, ksModel, ip));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="ksModel"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool SendMessage(Socket socket, KevSocketModel ksModel, IPEndPoint ip)
        {
            if (socket == null)
            {
                return(false);
            }

            if (ksModel == null)
            {
                return(false);
            }

            string message = JsonHelper.GetJson(ksModel);

            //KevMessageBox.Enqueue(ksModel.MessageId, new KevMessageBoxModel { SocketModel = ksModel, IP = ip, TimeOut = DateTime.Now });

            return(SendMessage(socket, message, ip));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="ksModel"></param>
        /// <param name="uModel"></param>
        /// <returns></returns>
        internal int HandleMessage(KevSocketModel ksModel, UDPModel uModel)
        {
            int result_code = 200;

            do
            {
                if (ksModel == null)
                {
                    result_code = 401;
                    break;
                }

                IUDPServerDelegate del = GetDelegate(ksModel.MessageType);
                if (del == null)
                {
                    result_code = 402;
                    break;
                }

                result_code = del.HandleRequestMessage(uModel);
            } while (false);

            return(result_code);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 处理响应消息
        /// </summary>
        /// <param name="ksModel"></param>
        /// <param name="uModel"></param>
        /// <returns></returns>
        public int HandleResponseMessage(KevSocketModel ksModel, UDPModel uModel)
        {
            int result_code = ResponseCode.Success;

            do
            {
                if (ksModel == null)
                {
                    result_code = ResponseCode.NotFindKevSocketModel;
                    break;
                }

                IUDPClientDelegate del = GetDelegate(ksModel.MessageType);
                if (del == null)
                {
                    result_code = ResponseCode.NotRegisteredType;
                    break;
                }

                result_code = del.HandleResponseMessage(uModel);
            } while (false);

            return(result_code);
        }
Exemplo n.º 7
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);
                }
            }
        }