private IEnumerator BeginSendPacket <T>(ENetworkMessage networkMessage, T packet, uint timeoutMessage, byte[] msgIDBytes) where T : global::ProtoBuf.IExtensible { string msgID = BitConverter.ToString(msgIDBytes); lock (_msgIDDict) { _msgIDDict.Add(BitConverter.ToString(msgIDBytes), packet); } Log4U.LogInfo("Send : " + networkMessage + " msgID : " + msgID); DoBeginSendPacket <T>(networkMessage, packet, msgIDBytes); yield return(new WaitForSeconds(REQ_TIME_OUT)); lock (_msgIDDict) { if (_msgIDDict.ContainsKey(msgID)) { RemoveMsgID(msgID); NetworkMessageParam param = new NetworkMessageParam { msgID = msgID, req = packet, }; MessageDispatcher.GetInstance().DispatchMessage(timeoutMessage, param); DialogManager.GetInstance().CreateSingleButtonDialog("Send Packet Type : " + networkMessage + " msgID : " + msgID + " timeout "); } } }
/// <summary> /// 协议格式: /// SIZE : 4 | TYPE : 4 | MsgID : 4 | PACKET : dynamic /// </summary> /// <typeparam name="T">向服务器发送的packet的类型</typeparam> /// <param name="networkMessage">向服务器发送的请求的类型</param> /// <param name="packet">向服务器发送的packet</param> private void DoBeginSendPacket <T>(ENetworkMessage networkMessage, T packet, byte[] msgID) where T : global::ProtoBuf.IExtensible { try { byte[] sendBuffer = new byte[_socket.SendBufferSize]; MemoryStream streamForProto = new MemoryStream(); Serializer.Serialize <T>(streamForProto, packet); int bufferSize = HEAD_SIZE * HEAD_NUM + (int)streamForProto.Length; byte[] bufferSizeBytes = MiniConverter.IntToBytes(bufferSize); byte[] networkMessageBytes = MiniConverter.IntToBytes((int)networkMessage); Array.Copy(bufferSizeBytes, 0, sendBuffer, HEAD_SIZE * 0, HEAD_SIZE); Array.Copy(networkMessageBytes, 0, sendBuffer, HEAD_SIZE * 1, HEAD_SIZE); Array.Copy(msgID, 0, sendBuffer, HEAD_SIZE * 2, HEAD_SIZE); Array.Copy(streamForProto.ToArray(), 0, sendBuffer, HEAD_SIZE * HEAD_NUM, streamForProto.Length); lock (_socket) { if (_socket != null && _socket.Connected) { _socket.BeginSend(sendBuffer, 0, bufferSize, SocketFlags.None, new AsyncCallback(EndSendPacket), null); } } streamForProto.Dispose(); } catch (ObjectDisposedException) { Log4U.LogInfo("Send Closed"); } catch (Exception ex) { Log4U.LogError(ex.Message); } }
/// <summary> /// 向服务器请求软件和游戏白名单 /// </summary> /// <param name="ipadress">服务器IP</param> /// <param name="port">服务器端口</param> /// <returns>软件和游戏白名单</returns> public static GetExeInfoRsp GetExe(string ipadress, int port) { GetExeInfoReq builder = new GetExeInfoReq(); builder.requestType = GetExeInfoReq.RequestType.ALL; try { MemoryStream ms = new MemoryStream(); Serializer.Serialize <GetExeInfoReq>(ms, builder); byte[] data = ms.ToArray(); IPAddress ip = IPAddress.Parse(ipadress); Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Connect(new IPEndPoint(ip, port)); log.InfoFormat("连接服务器{0}成功", ip); byte[] result = new byte[4096]; byte[] byteArray = PackMessage((int)ENetworkMessage.GET_EXE_INFO_REQ, data); serverSocket.Send(byteArray); log.InfoFormat("向服务器{0}发送请求软件白名单消息成功", ip); int count = 5; while (count-- > 0) { int receiveNumber = serverSocket.Receive(result); int size = BytesToInt(result, 0); ENetworkMessage type = (ENetworkMessage)BytesToInt(result, 4); if (type == ENetworkMessage.GET_EXE_INFO_RSP) { byte[] objBytes = new byte[size - GetMessageObjectStartIndex()]; for (int i = 0; i < objBytes.Length; i++) { objBytes[i] = result[GetMessageObjectStartIndex() + i]; } MemoryStream ms1 = new MemoryStream(objBytes); GetExeInfoRsp response = Serializer.Deserialize <GetExeInfoRsp>(ms1); log.Info("服务器返回状态码Response : " + response.resultCode); try { serverSocket.Close(); } catch (Exception e) { } return(response); } } return(null); } catch (IOException e) { log.Info("向服务器请求软件白名单异常"); return(null); } }
public string SendPacket <T>(ENetworkMessage networkMessage, T packet, uint timeoutMessage = (uint)EModelMessage.REQ_TIMEOUT) where T : global::ProtoBuf.IExtensible { byte[] msgIDBytes = BitConverter.GetBytes(UnityEngine.Random.value); if (timeoutMessage == (uint)EModelMessage.REQ_TIMEOUT) { DialogManager.GetInstance().ShowLoadingDialog(); } StartCoroutine(BeginSendPacket <T>(networkMessage, packet, timeoutMessage, msgIDBytes)); return(BitConverter.ToString(msgIDBytes)); }
private void DoBeginSendPacket(ENetworkMessage networkMessage, byte[] msgID) { try { byte[] sendBuffer = new byte[HEAD_SIZE * HEAD_NUM]; byte[] bufferSizeBytes = MiniConverter.IntToBytes(HEAD_SIZE * HEAD_NUM); byte[] networkMessageBytes = MiniConverter.IntToBytes((int)networkMessage); Array.Copy(bufferSizeBytes, 0, sendBuffer, HEAD_SIZE * 0, HEAD_SIZE); Array.Copy(networkMessageBytes, 0, sendBuffer, HEAD_SIZE * 1, HEAD_SIZE); Array.Copy(msgID, 0, sendBuffer, HEAD_SIZE * 2, HEAD_SIZE); lock (_socket) { _socket.BeginSend(sendBuffer, 0, HEAD_SIZE * HEAD_NUM, SocketFlags.None, new AsyncCallback(EndSendPacket), null); } } catch (Exception ex) { Log4U.LogError(ex.Message); } }
public static IExtensible UnPack(ENetworkMessage networkMessage, int startIndex, int length, byte[] buffer) { IExtensible packet = null; try { using (MemoryStream streamForProto = new MemoryStream(buffer, startIndex, length)) { switch (networkMessage) { case ENetworkMessage.KEEP_ALIVE_SYNC: packet = Serializer.Deserialize<KeepAliveSyncPacket>(streamForProto); break; case ENetworkMessage.REGISTER_RSP: packet = Serializer.Deserialize<RegisterRsp>(streamForProto); break; case ENetworkMessage.LOGIN_RSP: packet = Serializer.Deserialize<LoginRsp>(streamForProto); break; case ENetworkMessage.GET_USERINFO_RSP: packet = Serializer.Deserialize<GetUserInfoRsp>(streamForProto); break; case ENetworkMessage.PERSONALSETTINGS_RSP: packet = Serializer.Deserialize<PersonalSettingsRsp>(streamForProto); break; case ENetworkMessage.LOGOUT_RSP: packet = Serializer.Deserialize<LogoutRsp>(streamForProto); break; case ENetworkMessage.ADD_FRIEND_RSP: packet = Serializer.Deserialize<AddFriendRsp>(streamForProto); break; case ENetworkMessage.DELETE_FRIEND_RSP: packet = Serializer.Deserialize<DeleteFriendRsp>(streamForProto); break; case ENetworkMessage.OFFLINE_SYNC: packet = Serializer.Deserialize<OffLineSync>(streamForProto); break; case ENetworkMessage.GET_PERSONALINFO_RSP: packet = Serializer.Deserialize<GetPersonalInfoRsp>(streamForProto); break; case ENetworkMessage.CHANGE_FRIEND_SYNC: packet = Serializer.Deserialize<ChangeFriendSync>(streamForProto); break; case ENetworkMessage.SEND_CHAT_RSP: packet = Serializer.Deserialize<SendChatRsp>(streamForProto); break; case ENetworkMessage.RECEIVE_CHAT_SYNC: packet = Serializer.Deserialize<ReceiveChatSync>(streamForProto); break; case ENetworkMessage.CREATE_GROUP_CHAT_RSP: packet = Serializer.Deserialize<CreateGroupChatRsp>(streamForProto); break; case ENetworkMessage.CHANGE_GROUP_SYNC: packet = Serializer.Deserialize<ChangeGroupSync>(streamForProto); break; case ENetworkMessage.CHANGE_GROUP_RSP: packet = Serializer.Deserialize<ChangeGroupRsp>(streamForProto); break; case ENetworkMessage.GET_GROUP_INFO_RSP: packet = Serializer.Deserialize<GetGroupInfoRsp>(streamForProto); break; default: Log4U.LogInfo("No Such Packet, packet type is " + networkMessage); break; } } } catch (System.Exception ex) { Log4U.LogError(ex.Message + "\n " + ex.StackTrace + "\n" + ex.Source); } return packet; }
private void EndReceivePacket(IAsyncResult ar) { int bytesRead = -1; try { if (IsConncted) { lock (_socket) { bytesRead = _socket.EndReceive(ar); } } if (bytesRead == -1) { CloseConnection(); return; } } catch (ObjectDisposedException) { Log4U.LogInfo("Receive Closed"); } catch (Exception ex) { Log4U.LogError(ex.Message + "\n " + ex.StackTrace + "\n" + ex.Source); } // Begin Read // int position = 0; while (position < bytesRead) { int bufferSize = MiniConverter.BytesToInt(_receiveBuffer, position + HEAD_SIZE * 0); ENetworkMessage networkMessage = (ENetworkMessage)MiniConverter.BytesToInt(_receiveBuffer, position + HEAD_SIZE * 1); byte[] msgIDBytes = new byte[HEAD_SIZE]; for (int i = 0; i < HEAD_SIZE; i++) { msgIDBytes[i] = _receiveBuffer[position + HEAD_SIZE * 2 + i]; } string msgID = BitConverter.ToString(msgIDBytes); if (networkMessage != ENetworkMessage.KEEP_ALIVE_SYNC) { Log4U.LogInfo("networkMessage : " + networkMessage, "msgID : " + msgID, "bufferSize : " + bufferSize); } if (position + bufferSize > bytesRead) { Log4U.LogError("Error receive packet, packet is too long : " + bufferSize); break; } IExtensible rspPacket = UnPackTool.UnPack(networkMessage, position + HEAD_SIZE * HEAD_NUM, bufferSize - HEAD_NUM * HEAD_SIZE, _receiveBuffer); if (rspPacket == null) { continue; } MessageArgs args = new MessageArgs { iMessageType = (uint)networkMessage, kParam = rspPacket, }; NetworkMessageParam networkParam = new NetworkMessageParam { rsp = rspPacket, msgID = msgID, }; lock (_msgIDDict) { if (_msgIDDict.ContainsKey(msgID)) { networkParam.req = _msgIDDict[msgID]; } if (_needReqMessageType.Contains(networkMessage)) { args.kParam = networkParam; } if (_forcePushMessageType.Contains(networkMessage) || _msgIDDict.ContainsKey(msgID)) { MessageDispatcher.GetInstance().DispatchMessageAsync(args.iMessageType, args.kParam); } if (_msgIDDict.ContainsKey(msgID)) { RemoveMsgID(msgID); } } //if (_forcePushMessageType.Contains(networkMessage)) //{ // DoBeginSendPacket(networkMessage, msgIDBytes); //} position += bufferSize; } Array.Clear(_receiveBuffer, 0, _socket.ReceiveBufferSize); BeginReceivePacket(); }
public static IExtensible UnPack(ENetworkMessage networkMessage, int startIndex, int length, byte[] buffer) { IExtensible packet = null; try { using (MemoryStream streamForProto = new MemoryStream(buffer, startIndex, length)) { switch (networkMessage) { case ENetworkMessage.KEEP_ALIVE_SYNC: packet = Serializer.Deserialize <KeepAliveSync>(streamForProto); break; case ENetworkMessage.LOGIN_REQ: packet = Serializer.Deserialize <LoginReq>(streamForProto); break; case ENetworkMessage.LOGIN_RSP: packet = Serializer.Deserialize <LoginRsp>(streamForProto); break; case ENetworkMessage.GET_USERINFO_REQ: packet = Serializer.Deserialize <GetUserinfoReq>(streamForProto); break; case ENetworkMessage.GET_USERINFO_RSP: packet = Serializer.Deserialize <GetUserinfoRsp>(streamForProto); break; case ENetworkMessage.LOGOUT_REQ: packet = Serializer.Deserialize <LogoutReq>(streamForProto); break; case ENetworkMessage.LOGOUT_RSP: packet = Serializer.Deserialize <LogoutRsp>(streamForProto); break; case ENetworkMessage.OFFLINE_SYNC: packet = Serializer.Deserialize <OfflineSync>(streamForProto); break; case ENetworkMessage.GET_QUESTION_INFO_REQ: packet = Serializer.Deserialize <GetQuestionInfoReq>(streamForProto); break; case ENetworkMessage.GET_QUESTION_INFO_RSP: packet = Serializer.Deserialize <GetQuestionInfoRsp>(streamForProto); break; default: Log4U.LogInfo("No Such Packet, packet type is " + networkMessage); break; } } } catch (System.Exception ex) { Log4U.LogError(ex.Message + "\n " + ex.StackTrace + "\n" + ex.Source); } return(packet); }
public static IExtensible UnPack(ENetworkMessage networkMessage, int startIndex, int length, byte[] buffer) { IExtensible packet = null; try { using (MemoryStream streamForProto = new MemoryStream(buffer, startIndex, length)) { switch (networkMessage) { case ENetworkMessage.KEEP_ALIVE_SYNC: packet = Serializer.Deserialize <KeepAliveSyncPacket>(streamForProto); break; case ENetworkMessage.REGISTER_RSP: packet = Serializer.Deserialize <RegisterRsp>(streamForProto); break; case ENetworkMessage.LOGIN_RSP: packet = Serializer.Deserialize <LoginRsp>(streamForProto); break; case ENetworkMessage.GET_USERINFO_RSP: packet = Serializer.Deserialize <GetUserInfoRsp>(streamForProto); break; case ENetworkMessage.PERSONALSETTINGS_RSP: packet = Serializer.Deserialize <PersonalSettingsRsp>(streamForProto); break; case ENetworkMessage.LOGOUT_RSP: packet = Serializer.Deserialize <LogoutRsp>(streamForProto); break; case ENetworkMessage.ADD_FRIEND_RSP: packet = Serializer.Deserialize <AddFriendRsp>(streamForProto); break; case ENetworkMessage.DELETE_FRIEND_RSP: packet = Serializer.Deserialize <DeleteFriendRsp>(streamForProto); break; case ENetworkMessage.OFFLINE_SYNC: packet = Serializer.Deserialize <OffLineSync>(streamForProto); break; case ENetworkMessage.GET_PERSONALINFO_RSP: packet = Serializer.Deserialize <GetPersonalInfoRsp>(streamForProto); break; case ENetworkMessage.CHANGE_FRIEND_SYNC: packet = Serializer.Deserialize <ChangeFriendSync>(streamForProto); break; case ENetworkMessage.SEND_CHAT_RSP: packet = Serializer.Deserialize <SendChatRsp>(streamForProto); break; case ENetworkMessage.RECEIVE_CHAT_SYNC: packet = Serializer.Deserialize <ReceiveChatSync>(streamForProto); break; case ENetworkMessage.CREATE_GROUP_CHAT_RSP: packet = Serializer.Deserialize <CreateGroupChatRsp>(streamForProto); break; case ENetworkMessage.CHANGE_GROUP_SYNC: packet = Serializer.Deserialize <ChangeGroupSync>(streamForProto); break; case ENetworkMessage.CHANGE_GROUP_RSP: packet = Serializer.Deserialize <ChangeGroupRsp>(streamForProto); break; case ENetworkMessage.GET_GROUP_INFO_RSP: packet = Serializer.Deserialize <GetGroupInfoRsp>(streamForProto); break; default: Log4U.LogInfo("No Such Packet, packet type is " + networkMessage); break; } } } catch (System.Exception ex) { Log4U.LogError(ex.Message + "\n " + ex.StackTrace + "\n" + ex.Source); } return(packet); }