/// <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); }
/// <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); }
/// <summary> /// 发送消息给服务器 /// </summary> /// <param name="message"></param> /// <param name="ip"></param> /// <returns></returns> public bool SendMessage(string message, IPEndPoint ip) { Socket socket = SocketRegister.Get(UDPPrimaryKey.UDPServerSocket); if (socket == null) { return(false); } return(UDPSocketServer.SendMessage(socket, message, ip)); }
/// <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)); }
/// <summary> /// 发送消息给服务器 /// </summary> /// <param name="message"></param> /// <returns></returns> public bool SendMessage(string message) { Socket socket = SocketRegister.Get(SocketClient); if (socket == null) { return(false); } return(UDPSocketServer.SendMessage(socket, message, this.ServerIPPoint)); }
/// <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); } } }