/// <summary> /// 匹配成功对手后返回对手信息 /// </summary> /// <param name="e">E.</param> private void _RaiseMatchOpponentResponse(ReceivedProtoEventArgs e) { if (e.errorCode == ErrorCode.SUCCESS) { Room_MatchOpponent_Response s = _Deserialize <Room_MatchOpponent_Response> (e.bytes); PVPGlobal.roomInfo = new RoomInfo(s.room_info); foreach (PVPProtobuf.AppUserInfo appUserInfo in s.appuser_infos) { if (appUserInfo.uid == PVPGlobal.userInfo.uid) { PVPGlobal.localAppUserInfo.FillAppUserInfo(appUserInfo); } else { if (PVPGlobal.appUserInfos.ContainsKey(appUserInfo.uid)) { PVPGlobal.appUserInfos [appUserInfo.uid].FillAppUserInfo(appUserInfo); } else { PVPGlobal.appUserInfos [appUserInfo.uid] = new AppUserInfo(appUserInfo); } } } } if (ICM.handlerRegister.RoomMatchOpponentResponseHandler != null) { ICM.handlerRegister.RoomMatchOpponentResponseHandler(e.errorCode); } }
private void _RaiseCheckTokenMessageResponse(ReceivedProtoEventArgs arg) { Debug.LogError("_RaiseCheckTokenMessageEvent"); this._RaiseUnlockSocketEvent(); if (arg.errorCode == 0 && arg.bytes != null && arg.bytes.Length > 0) { Debug.LogError(arg.bytes.Length); PVPProtobuf.Socket_CheckToken_Response s = _Deserialize <Socket_CheckToken_Response> (arg.bytes); if (s.user_info != null) { PVPGlobal.localAppUserInfo = new LocalAppUserInfo(s.user_info); } if (s.lobby_info != null) { PVPGlobal.lobbyInfo = new LobbyInfo(s.lobby_info); } else { PVPGlobal.lobbyInfo = null; } if (s.room_info != null) { PVPGlobal.roomInfo = new RoomInfo(s.room_info); } else { PVPGlobal.roomInfo = null; } } if (ICM.handlerRegister != null && ICM.handlerRegister.socketCheckTokenEventHandler != null) { ICM.handlerRegister.socketCheckTokenEventHandler(arg.errorCode); } }
public bool SendData <T>(UInt16 request_id, int messageTypeId, T t, bool is_token = false) where T : global::ProtoBuf.IExtensible { if (!this.isConnected) { return(false); } byte[] b = this._Serialize <T> (t); byte[] bf = new byte[10 + b.Length]; Array.Copy(this._UInt162ByteArray(request_id).CheckBigEndian(), bf, 2); Array.Copy(this._Int2ByteArray(messageTypeId).CheckBigEndian(), 0, bf, 2, 4); Array.Copy(this._Int2ByteArray(b.Length).CheckBigEndian(), 0, bf, 6, 4); Array.Copy(b, 0, bf, 10, b.Length); ReceivedProtoEventArgs item = new ReceivedProtoEventArgs(); item.messageTypeId = messageTypeId; item.bytes = bf; if (is_token) { this._socketWriteQueue.Enqueue(item); } else { _writeQueue.Enqueue(item); } Debug.Log("messageTypeId = " + messageTypeId + ", send in queue"); return(true); }
private void _OnNetworkError() { ReceivedProtoEventArgs arg = new ReceivedProtoEventArgs(); arg.errorCode = ErrorCode.SUCCESS; arg.messageTypeId = MessageTypeId.Network_Offline_Notice; this._readQueue.Enqueue(arg); }
private void _RaiseRoomUpdateMemberCustomDataResponseEvent(ReceivedProtoEventArgs e) { int errorCode = e.errorCode; if (ICM.handlerRegister != null && ICM.handlerRegister.roomMemberCustomDataResponseHandler != null) { ICM.handlerRegister.roomMemberCustomDataResponseHandler(errorCode); } }
/// <summary> /// 匹配到对手 /// </summary> /// <param name="e">E.</param> private void _RaiseRoomCancelMatchOpponentResponse(ReceivedProtoEventArgs e) { if (e.errorCode == ErrorCode.SUCCESS && e.bytes != null && e.bytes.Length > 0) { PVPProtobuf.Room_MatchOpponent_Response r = _Deserialize <PVPProtobuf.Room_MatchOpponent_Response> (e.bytes); } if (ICM.handlerRegister != null && ICM.handlerRegister.roomCancelMatchOpponentResonseHandler != null) { ICM.handlerRegister.roomCancelMatchOpponentResonseHandler(e.errorCode); } }
/// <summary> /// Raises the leave room event. /// </summary> /// <param name="e">E.</param> private void _RaiseLeaveRoomResponse(ReceivedProtoEventArgs e) { if (e.errorCode == ErrorCode.SUCCESS) { PVPGlobal.roomInfo = null; } if (ICM.handlerRegister != null && ICM.handlerRegister.leaveRoomResponseHandler != null) { ICM.handlerRegister.leaveRoomResponseHandler(e.errorCode); } }
private void _RaiseRoomNewMessageEvent(ReceivedProtoEventArgs arg) { if (arg.errorCode == 0 && arg.bytes != null) { PVPProtobuf.Room_NewMessage_Broadcast new_message = _Deserialize <Room_NewMessage_Broadcast> (arg.bytes); if (ICM.handlerRegister != null && ICM.handlerRegister.roomNewMessageEventHandler != null) { RoomNewMessage m = new RoomNewMessage(new_message); ICM.handlerRegister.roomNewMessageEventHandler(arg.errorCode, m); } } }
/// <summary> /// 进入指定大厅响应 /// </summary> private void _RaiseEnterLobbyResponse(ReceivedProtoEventArgs arg) { // int count = 0; if (arg.errorCode == 0 && arg.bytes != null) { Lobby_EnterLobby_Response lobby_EnterLobby_Response = _Deserialize <Lobby_EnterLobby_Response> (arg.bytes); PVPGlobal.lobbyInfo = new LobbyInfo(lobby_EnterLobby_Response.lobby_id, lobby_EnterLobby_Response.name); // count = lobby_EnterLobby_Response.room_count; } if (ICM.handlerRegister != null && ICM.handlerRegister.enterLobbyResponseHandler != null) { ICM.handlerRegister.enterLobbyResponseHandler(arg.errorCode); } }
private void _RaiseOtherMemberLeaveRoomBroadcast(ReceivedProtoEventArgs arg) { List <uint> member_uids = new List <uint> (); if (arg.errorCode == ErrorCode.SUCCESS) { Room_OtherMemberLeaveRoom_Response s = _Deserialize <Room_OtherMemberLeaveRoom_Response> (arg.bytes); member_uids.AddRange(s.member_ids); PVPGlobal.roomInfo = null; } if (ICM.handlerRegister != null && ICM.handlerRegister.otherMemberLeaveRoomEventHandler != null) { ICM.handlerRegister.otherMemberLeaveRoomEventHandler(arg.errorCode, member_uids); } }
private void _RaiseUpdateRoomCustomDataBroadcast(ReceivedProtoEventArgs e) { NewCustomData d = null; if (e.errorCode == ErrorCode.SUCCESS) { Room_UpdateRoomCustomData_Broadcast u = _Deserialize <Room_UpdateRoomCustomData_Broadcast> (e.bytes); d = new NewCustomData(u.member_uid, u.custom_data_number, u.updated_data, u.deleted_data); PVPGlobal.roomInfo.OnUpdateCustomData(d); } if (ICM.handlerRegister != null && ICM.handlerRegister.updateRoomCustomDataBroadcastHandler != null) { ICM.handlerRegister.updateRoomCustomDataBroadcastHandler(e.errorCode, d); } }
public void ReadHeader() { try { // Begin receiving the data from the remote device. this._tcpClient.Client.BeginReceive( this._buffer, 0, this._headerSize, SocketFlags.None, ar => { int bytesRead = this._tcpClient.Client.EndReceive(ar); if (bytesRead == this._headerSize) { UInt16 requestId = BitConverter.ToUInt16(this._buffer.SubArray(0, 2).CheckBigEndian(), 0); int errorCode = BitConverter.ToInt32(this._buffer.SubArray(2, 4).CheckBigEndian(), 0); int messageTypeId = BitConverter.ToInt32(this._buffer.SubArray(6, 4).CheckBigEndian(), 0); int protoLength = BitConverter.ToInt32(this._buffer.SubArray(10, 4).CheckBigEndian(), 0); ReceivedProtoEventArgs r = new ReceivedProtoEventArgs(); r.bytes = null; r.messageTypeId = messageTypeId; r.errorCode = errorCode; r.requestId = requestId; if (r.messageTypeId == MessageTypeId.Socket_CheckToken_Response && r.errorCode == ErrorCode.SUCCESS) { this._state = SocketClientState.CheckTokened; } if (protoLength == 0) { if (receiveProtoEventHandler != null) { receiveProtoEventHandler(r); } ReadHeader(); } else { ReadBody(protoLength, r); } } }, null); } catch (Exception e) { this._tcpClient.Close(); Debug.LogError(e.Message); Debug.LogError(e.StackTrace); } }
private void _RaiseAppUserUpdateInfoResponse(ReceivedProtoEventArgs e) { UpdateAppUserInfoResult info = null; if (e.bytes != null && e.bytes.Length > 0) { Appuser_UpdateInfo_Response r = _Deserialize <Appuser_UpdateInfo_Response> (e.bytes); info = new UpdateAppUserInfoResult(e.errorCode, r); PVPGlobal.localAppUserInfo.OnUpdateInfo(info); } if (ICM.handlerRegister != null && ICM.handlerRegister.appUserUpdateInfoResponseHandler != null) { ICM.handlerRegister.appUserUpdateInfoResponseHandler(e.errorCode, info); } }
private void _RaiseOtherLoseConnectionBroadcast(ReceivedProtoEventArgs e) { uint other_uid = 0; if (e.errorCode == ErrorCode.SUCCESS) { PVPGlobal.roomInfo = null; Room_OtherLoseConnection_Broadcast client = _Deserialize <Room_OtherLoseConnection_Broadcast> (e.bytes); other_uid = client.uid; PVPGlobal.roomInfo = null; } if (ICM.handlerRegister != null && ICM.handlerRegister.otherLoseConnectionBroadcastHandler != null) { ICM.handlerRegister.otherLoseConnectionBroadcastHandler(e.errorCode, other_uid); } }
public bool SendData(UInt16 requestId, int messageTypeId) { if (!this.isConnected) { return(false); } byte[] bf = new byte[10];//+ b.Length]; Array.Copy(this._UInt162ByteArray(requestId).CheckBigEndian(), bf, 2); Array.Copy(this._Int2ByteArray(messageTypeId).CheckBigEndian(), 0, bf, 2, 4); Array.Copy(this._Int2ByteArray(0).CheckBigEndian(), 0, bf, 6, 4); ReceivedProtoEventArgs item = new ReceivedProtoEventArgs(); item.messageTypeId = messageTypeId; item.bytes = bf; _writeQueue.Enqueue(item); return(true); }
private void _RaiseEnterRoomResponse(ReceivedProtoEventArgs arg) { if (arg.errorCode == 0 && arg.bytes != null) { PVPProtobuf.Room_EnteredRoom_Response enterRoomMessage = _Deserialize <Room_EnteredRoom_Response> (arg.bytes); if (enterRoomMessage.room_info != null) { PVPGlobal.roomInfo = new PVPSdk.RoomInfo(enterRoomMessage.room_info); } } if (ICM.handlerRegister != null && ICM.handlerRegister.enterRoomResponseHandler != null) { ICM.handlerRegister.enterRoomResponseHandler(arg.errorCode); } }
public void doSendData() { while (this._keepConnecting && this.isConnected) { if (_socketWriteQueue.Count == 0 && _writeQueue.Count == 0) { Thread.Sleep(15); continue; } try { Stream stream = this._tcpClient.GetStream(); while (_socketWriteQueue.Count > 0) { ReceivedProtoEventArgs item = _socketWriteQueue.Dequeue(); // Debug.LogError(Time.); Debug.Log(string.Format("_socketWriteQueue data {0}", item.bytes.Length)); stream.Write(item.bytes, 0, item.bytes.Length); stream.Flush(); } if (this.state == SocketClientState.CheckTokened) { while (_writeQueue.Count > 0) { ReceivedProtoEventArgs item = _writeQueue.Dequeue(); if (PVPGlobal.isDebug) { Debug.Log(string.Format("send data {0}", item.bytes.Length)); } stream.Write(item.bytes, 0, item.bytes.Length); stream.Flush(); } } } catch (Exception ex) { Debug.LogError(ex.Message); Debug.LogError(ex.StackTrace); this._tcpClient.Close(); } } }
public bool ConnectServer() { if (!PVPGlobal.isLogined) { return(false); } if (_socketClient == null) { _socketClient = new SocketClient(); _socketClient.receiveProtoEventHandler += new ReceivedProtoEventHandler(_OnReceivedBytes); _socketClient.networkErrorHandler += new NetworkErrorEventHandler(_OnNetworkError); _socketClient.startConnectEventHandler += new StartConnectEventHandler(delegate() { ReceivedProtoEventArgs arg = new ReceivedProtoEventArgs(); arg.errorCode = ErrorCode.SUCCESS; arg.messageTypeId = MessageTypeId.Network_Lock_Notice; this._readQueue.Enqueue(arg); }); } // 连接服务器 _socketClient.Connect(this.ip, this.port, this.token); return(true); }
private void _RaiseAppUserUpdateCustomDataResponse(ReceivedProtoEventArgs e) { NewCustomData meta = null; if (e.bytes != null && e.bytes.Length > 0) { AppUser_UpdateCustomData_Response r = _Deserialize <AppUser_UpdateCustomData_Response> (e.bytes); if (e.errorCode == ErrorCode.SUCCESS) { meta = new NewCustomData(PVPGlobal.userInfo.uid, r.custom_data_number, r.updated_data, r.deleted_data); PVPGlobal.localAppUserInfo.OnUpdateCustomData(r.custom_data_number, r.updated_data, r.deleted_data); } else { meta = new NewCustomData(PVPGlobal.userInfo.uid, r.custom_data_number, r.check_data, r.check_data_not_exist); PVPGlobal.localAppUserInfo.OnUpdateCustomData(r.custom_data_number, r.check_data, r.check_data_not_exist); } } if (ICM.handlerRegister != null && ICM.handlerRegister.appUserUpdateCustomDataResponseHandler != null) { ICM.handlerRegister.appUserUpdateCustomDataResponseHandler(e.errorCode, meta); } }
public void ReadBody(int proto_length, ReceivedProtoEventArgs arg) { try { if (this._bufferSize < proto_length) { this._bufferSize = proto_length + _headerSize; this._buffer = new byte[this._bufferSize]; // 填充一下协议id } // Begin receiving the data from the remote device. this._tcpClient.Client.BeginReceive( this._buffer, this._headerSize, proto_length, SocketFlags.None, ar => { // Read data from the remote device. int bytesRead = this._tcpClient.Client.EndReceive(ar); Debug.Log("ReadBody bytesRead = " + bytesRead); if (bytesRead == proto_length) { if (receiveProtoEventHandler != null) { arg.bytes = this._buffer.SubArray(this._headerSize, proto_length); receiveProtoEventHandler(arg); } } ReadHeader(); }, null); } catch (Exception e) { this._tcpClient.Close(); Debug.LogError(e.Message); Debug.LogError(e.StackTrace); } }
private void _RaiseLobbyGetLobbyListResponse(ReceivedProtoEventArgs arg) { int errorCode = arg.errorCode; List <LobbyInfo> l = null; if (errorCode == ErrorCode.SUCCESS) { l = new List <LobbyInfo> (); using (MemoryStream memoryStream = new MemoryStream(arg.bytes)) { memoryStream.Position = 0; Lobby_GetLobbyList_Response lobby_GetLobbyList_Response = Serializer.Deserialize <Lobby_GetLobbyList_Response> (memoryStream); for (int i = 0; i < lobby_GetLobbyList_Response.lobby_list.Count; i++) { l.Add(new LobbyInfo(lobby_GetLobbyList_Response.lobby_list [i])); } } } if (ICM.handlerRegister != null && ICM.handlerRegister.lobbyGetLobbyListEventHandler != null) { ICM.handlerRegister.lobbyGetLobbyListEventHandler(errorCode, l); } }
private void _RaiseUpdateRoomCustomDataResponse(ReceivedProtoEventArgs e) { NewCustomData d = null; if (e.bytes != null && e.bytes.Length > 0) { Room_UpdateRoomCustomData_Response u = _Deserialize <Room_UpdateRoomCustomData_Response> (e.bytes); if (e.errorCode == ErrorCode.SUCCESS) { //d = new RoomCustomData (); d = new NewCustomData(PVPGlobal.userInfo.uid, u.custom_data_number, u.updated_data, u.deleted_data); } else { d = new NewCustomData(PVPGlobal.userInfo.uid, u.custom_data_number, u.check_data, u.check_data_not_exist); } PVPGlobal.roomInfo.OnUpdateCustomData(d); } PVPGlobal.roomInfo.OnUpdateCustomData(d); if (ICM.handlerRegister != null && ICM.handlerRegister.updateRoomCustomDataBroadcastHandler != null) { ICM.handlerRegister.updateRoomCustomDataBroadcastHandler(e.errorCode, d); } }
private void _RaiseRoomUpdateMemberCustomDataBroadcastEvent(ReceivedProtoEventArgs e) { int errorCode = e.errorCode; MemberCustomData customData = null; if (e.bytes != null && e.bytes.Length > 0) { Room_UpdateMemberCustomData_Broadcast b = _Deserialize <Room_UpdateMemberCustomData_Broadcast> (e.bytes); customData = new MemberCustomData(b); if (errorCode == ErrorCode.SUCCESS) { if (PVPGlobal.roomInfo.member_infos.ContainsKey(customData.member_uid)) { PVPGlobal.roomInfo.member_infos [customData.member_uid].OnUpdateCustomData(customData); } } } if (ICM.handlerRegister != null && ICM.handlerRegister.roomMemberCustomDataBroadcastHandler != null) { ICM.handlerRegister.roomMemberCustomDataBroadcastHandler(errorCode, customData); } }
private void _RaiseAppUserGetCustomDataResponse(ReceivedProtoEventArgs e) { List <UInt32> uids = null; if (e.bytes != null && e.bytes.Length > 0) { AppUser_GetUserInfo_Response r = _Deserialize <AppUser_GetUserInfo_Response> (e.bytes); if (e.errorCode == ErrorCode.SUCCESS) { uids = new List <uint> (); foreach (PVPProtobuf.AppUserInfo item in r.user_infos) { uids.Add(item.uid); if (item.uid == PVPGlobal.userInfo.uid) { PVPGlobal.localAppUserInfo.FillAppUserInfo(item); } else { if (PVPGlobal.appUserInfos.ContainsKey(item.uid)) { PVPGlobal.appUserInfos [item.uid].FillAppUserInfo(item); } else { PVPGlobal.appUserInfos [item.uid] = new AppUserInfo(item); } } } } } if (ICM.handlerRegister != null && ICM.handlerRegister.appUserGetUserInfoResponseHandler != null) { ICM.handlerRegister.appUserGetUserInfoResponseHandler(e.errorCode, uids); } }
private void _OnReceivedBytes(ReceivedProtoEventArgs e) { lock (_lock) { this._readQueue.Enqueue(e); } }
/// <summary> /// 路由接收到的数据 /// </summary> /// <returns>The read message.</returns> private IEnumerator _RouteReceivedServerMessage() { while (true) { while (this._readQueue.Count > 0) { ReceivedProtoEventArgs e = this._readQueue.Dequeue(); Debug.LogError(string.Format("e.messageTypeId {0} ", e.messageTypeId)); UInt16 requestId = e.requestId; if (this._timeoutMessageTypeIdList.Contains(e.messageTypeId)) { this._tcpRequestTimeoutChecker.Remove(e.requestId); } switch (e.messageTypeId) { case MessageTypeId.Lobby_GetLobbyList_Response: this._RaiseLobbyGetLobbyListResponse(e); break; case MessageTypeId.Lobby_EnterLobby_Response: this._RaiseEnterLobbyResponse(e); break; case MessageTypeId.Room_EnterRoom_Response: this._RaiseEnterRoomResponse(e); break; case MessageTypeId.Socket_CheckToken_Response: this._RaiseCheckTokenMessageResponse(e); break; case MessageTypeId.Room_OtherMemberEnterRoom_Response: this._RaiseOtherMemberEnterRoomMessageResponse(e); break; case MessageTypeId.Room_OtherMemberLeaveRoom_Broadcast: this._RaiseOtherMemberLeaveRoomBroadcast(e); break; case MessageTypeId.Room_UpdateRoomCustomData_Response: this._RaiseUpdateRoomCustomDataResponse(e); break; case MessageTypeId.Room_UpdateRoomCustomData_Broadcast: this._RaiseUpdateRoomCustomDataBroadcast(e); break; case MessageTypeId.Room_LeaveRoom_Response: this._RaiseLeaveRoomResponse(e); break; case MessageTypeId.Room_MatchOpponent_Response: this._RaiseMatchOpponentResponse(e); break; case MessageTypeId.Room_OtherLoseConnection_Broadcast: this._RaiseOtherLoseConnectionBroadcast(e); break; case MessageTypeId.Network_Offline_Notice: this._RaiseNetworkErrorEvent(); break; case MessageTypeId.Network_Lock_Notice: this._RaiseLockSocketEvent(); break; case MessageTypeId.Network_UnLock_Notice: this._RaiseUnlockSocketEvent(); break; case MessageTypeId.Room_UpdateMemberCustomData_Response: this._RaiseRoomUpdateMemberCustomDataResponseEvent(e); break; case MessageTypeId.Room_UpdateMemberCustomData_Broadcast: this._RaiseRoomUpdateMemberCustomDataBroadcastEvent(e); break; case MessageTypeId.Room_CancelMatchOpponent_Response: this._RaiseRoomCancelMatchOpponentResponse(e); break; case MessageTypeId.AppUser_UpdateCustomData_Response: this._RaiseAppUserUpdateCustomDataResponse(e); break; case MessageTypeId.AppUser_UpdateInfo_Response: this._RaiseAppUserUpdateInfoResponse(e); break; default: Debug.LogWarning(String.Format("do nothing {0}", e.messageTypeId)); break; } } //返回一帧 yield return(0); } }
private void _RaiseOtherMemberEnterRoomMessageResponse(ReceivedProtoEventArgs arg) { }