//게임 시작 public void StartGame(DataPacket packet) { Console.WriteLine(packet.client.RemoteEndPoint.ToString() + "게임 시작"); string id = loginUser[packet.client]; int roomNum = userState[id].state; if (roomNum < 0) { Console.WriteLine("방에 입장해있지 않습니다."); return; } roomManager.Room[roomNum].GameStart(); byte result = (byte)Result.Success; ResultData resultData = new ResultData(result); ResultPacket resultPacket = new ResultPacket(resultData); bool setHost = false; for (int i = 0; i < RoomManager.maxPlayerNum; i++) { resultPacket.SetPacketId((int)ServerPacketId.StartGame); byte[] msg = CreatePacket(resultPacket); if (roomManager.Room[roomNum].Socket[i] != null) { Console.WriteLine(i + "번 유저 : " + roomManager.Room[roomNum].Socket[i].RemoteEndPoint.ToString()); packet = new DataPacket(msg, roomManager.Room[roomNum].Socket[i]); lock (sendLock) { sendMsgs.Enqueue(packet); } if (!setHost) { setHost = true; resultPacket.SetPacketId((int)ServerPacketId.SetHost); msg = CreatePacket(resultPacket); packet = new DataPacket(msg, roomManager.Room[roomNum].Socket[i]); lock (sendLock) { sendMsgs.Enqueue(packet); } } } } }
//대기방 -> 캐릭터 선택 public void ReturnToSelect(DataPacket packet) { Console.WriteLine(packet.client.RemoteEndPoint.ToString() + "캐릭터 선택창으로 돌아가기"); string id = loginUser[packet.client]; Result result = Result.Fail; if (userState.ContainsKey(id)) { userState[id].characterId = -1; result = Result.Success; } else { Console.WriteLine("DataHandler::ReturnToSelect.ContainsKey 에러"); result = Result.Fail; } ResultData resultData = new ResultData((byte)result); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ServerPacketId.ReturnToSelectResult); byte[] msg = CreatePacket(resultPacket); packet = new DataPacket(msg, packet.client); lock (sendLock) { sendMsgs.Enqueue(packet); } }
/* * public ServerPacketId ReLogin(DataPacket packet) * { * Console.WriteLine(tcpPacket.client.RemoteEndPoint.ToString() + " 재로그인요청"); * * AccountPacket accountPacket = new AccountPacket(data); * AccountData accountData = accountPacket.GetData(); * * Console.WriteLine("아이디 : " + accountData.Id); * * try * { * if (database.AccountData.Contains(accountData.Id)) * { * if (!loginUser.ContainsValue(accountData.Id)) * { * msg[0] = (byte)Result.Success; * Console.WriteLine("로그인 성공"); * loginUser.Add(tcpPacket.client, accountData.Id); * } * else * { * Console.WriteLine("현재 접속중인 아이디입니다."); * * if (CompareIP(GetSocket(accountData.Id).RemoteEndPoint.ToString(), tcpPacket.client.RemoteEndPoint.ToString())) * { * loginUser.Remove(GetSocket(accountData.Id)); * Console.WriteLine("현재 접속중 해제"); * } * msg[0] = (byte)Result.Fail; * } * } * } * catch * { * Console.WriteLine("DataHandler::ReLogin.ContainsValue 에러"); * msg[0] = (byte)Result.Fail; * } * * return ServerPacketId.LoginResult; * } */ public void Logout(DataPacket packet) { Console.WriteLine(packet.client.RemoteEndPoint.ToString() + " 로그아웃요청"); Result result = Result.Fail; if (RemoveUserData(packet.client)) { result = Result.Success; } else { result = Result.Fail; } ResultData resultData = new ResultData((byte)result); ResultPacket resultDataPacket = new ResultPacket(resultData); resultDataPacket.SetPacketId((int)ServerPacketId.LogoutResult); byte[] msg = CreatePacket(resultDataPacket); packet = new DataPacket(msg, packet.client); lock (sendLock) { sendMsgs.Enqueue(packet); } }
//게임 종료 -> Server public void GameClose() { Debug.Log("게임 종료"); ResultData resultData = new ResultData(); ResultPacket resultDataPacket = new ResultPacket(resultData); resultDataPacket.SetPacketId((int)ClientPacketId.GameClose); byte[] msg = CreatePacket(resultDataPacket); try { tcpSock.Send(msg, 0, msg.Length, SocketFlags.None); } catch { Debug.Log("GameClose.Send 에러"); } try { tcpSock.Close(); udpSock.Close(); } catch { Debug.Log("이미 소켓이 닫혀있습니다."); } }
//방 퇴장 public void ExitRoom(DataPacket packet) { Console.WriteLine(packet.client.RemoteEndPoint.ToString() + "방 퇴장"); ExitRoomPacket exitRoomPacket = new ExitRoomPacket(packet.msg); ExitRoomData exitRoomData = exitRoomPacket.GetData(); string id = loginUser[packet.client]; int characterId = userState[id].characterId; Result result = Result.Fail; int roomNum = -1; bool empty = false; Console.WriteLine(userState[id].state); try { empty = roomManager.ExitRoom(userState[id].state, packet.client); roomNum = userState[id].state; userState[id].state = -1; result = Result.Success; } catch { Console.WriteLine("DataHandler::ExitRoom.DeletePlayer 에러"); result = Result.Fail; return; } ResultData resultData = new ResultData((byte)result); ResultPacket resultDataPacket = new ResultPacket(resultData); resultDataPacket.SetPacketId((int)ServerPacketId.ExitRoomNumber); byte[] msg = CreatePacket(resultDataPacket); packet = new DataPacket(msg, packet.client); lock (sendLock) { sendMsgs.Enqueue(packet); } Console.WriteLine(roomNum); if (roomNum != -1 && !empty) { for (int playerIndex = 0; playerIndex < RoomManager.maxPlayerNum; playerIndex++) { if (roomManager.Room[exitRoomData.RoomNum].Socket[playerIndex] != null) { packet = new DataPacket(new byte[0], roomManager.Room[exitRoomData.RoomNum].Socket[playerIndex]); RequestRoomUserData(packet); } } } }
//연결 확인 답장 -> Server public void ServerConnectionAnswer() { ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ClientPacketId.ServerConnectionAnswer); DataPacket packet = new DataPacket(CreatePacket(resultPacket), null); sendMsgs.Enqueue(packet); }
//Udp 답신 -> Client public void UdpAnswer(EndPoint newEndPoint, int udpId) { Debug.Log(newEndPoint.ToString() + " Udp 답신 보냄 Id : " + udpId); ResultData resultData = new ResultData(new byte()); ResultPacket resultDataPacket = new ResultPacket(resultData); resultDataPacket.SetPacketId((int)P2PPacketId.UdpAnswer); DataPacket packet = new DataPacket(CreateUdpPacket(resultDataPacket, udpId), newEndPoint); sendMsgs.Enqueue(packet); }
//연결 완료 -> Server public void LoadingComplete() { Debug.Log("Udp 연결 완료"); ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ClientPacketId.LoadingComplete); DataPacket packet = new DataPacket(CreatePacket(resultPacket), null); sendMsgs.Enqueue(packet); }
//UDP 연결 요청 -> Server public void RequestUdpConnection() { Debug.Log("Udp 연결 요청"); ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ClientPacketId.RequestUdpConnection); DataPacket packet = new DataPacket(CreatePacket(resultPacket), null); sendMsgs.Enqueue(packet); }
//선택 창으로 돌아가기 -> Server public void ReturnToSelect() { Debug.Log("선택 창으로 돌아가기"); ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ClientPacketId.ReturnToSelect); DataPacket packet = new DataPacket(CreatePacket(resultPacket), null); sendMsgs.Enqueue(packet); }
//게임 시작 -> Server public void StartGame() { Debug.Log("게임 시작"); ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ClientPacketId.StartGame); DataPacket packet = new DataPacket(CreatePacket(resultPacket), null); sendMsgs.Enqueue(packet); }
//캐릭터 정보 요청 -> Server public void RequestCharacterStatus() { Debug.Log("캐릭터 정보 요청"); ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ClientPacketId.RequestCharacterStatus); DataPacket packet = new DataPacket(CreatePacket(resultPacket), null); sendMsgs.Enqueue(packet); }
//방 목록 요청 -> Server public void RequestRoomList() { Debug.Log("방 목록 요청"); ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ClientPacketId.RequestRoomList); DataPacket packet = new DataPacket(CreatePacket(resultPacket), null); sendMsgs.Enqueue(packet); }
//로그아웃 -> Server public void Logout() { Debug.Log("로그아웃"); ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ClientPacketId.Logout); DataPacket packet = new DataPacket(CreatePacket(resultPacket), null); sendMsgs.Enqueue(packet); }
//던전 몬스터 소환 데이터 요청 -> Server public void RequestMonsterSpawnList() { Debug.Log("몬스터 소환 데이터 요청"); byte result = (byte)UIManager.Instance.RoomUIManager.DungeonId; ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ClientPacketId.RequestMonsterSpawnList); DataPacket packet = new DataPacket(CreatePacket(resultPacket), null); sendMsgs.Enqueue(packet); }
//연결 체크 public void ServerConnectionCheck(DataPacket packet) { ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ServerPacketId.ServerConnectionCheck); byte[] msg = CreatePacket(resultPacket); packet = new DataPacket(msg, packet.client); lock (sendLock) { sendMsgs.Enqueue(packet); } }
//캐릭터 삭제 public void DeleteCharacter(DataPacket packet) { Console.WriteLine(packet.client.RemoteEndPoint.ToString() + "캐릭터 삭제"); DeleteCharacterPacket deleteCharacterPacket = new DeleteCharacterPacket(packet.msg); DeleteCharacterData deleteCharacterData = deleteCharacterPacket.GetData(); string id = ""; try { id = loginUser[packet.client]; } catch { Console.WriteLine("Datahandler::DeleteCharacter.loginUser 에러"); } UserData userData = database.GetUserData(id); Result result = Result.Fail; try { userData.DeleteHero(deleteCharacterData.Index); result = Result.Success; } catch { Console.WriteLine("DataHandler::DeleteCharacter.DeleteHero에러"); result = Result.Fail; } database.FileSave(id + ".data", userData); ResultData resultData = new ResultData((byte)result); ResultPacket resultDataPacket = new ResultPacket(resultData); resultDataPacket.SetPacketId((int)ServerPacketId.DeleteChracterResult); byte[] msg = CreatePacket(resultDataPacket); packet = new DataPacket(msg, packet.client); lock (sendLock) { sendMsgs.Enqueue(packet); } }
public void DeleteAccount(DataPacket packet) { Console.WriteLine(packet.client.RemoteEndPoint.ToString() + " 탈퇴요청"); AccountPacket accountPacket = new AccountPacket(packet.msg); AccountData accountData = accountPacket.GetData(); Console.WriteLine("아이디 : " + accountData.Id + "패스워드 : " + accountData.Id); Result result = Result.Fail; try { if (database.DeleteAccountData(accountData.Id, accountData.Password) == Result.Success) { result = Result.Success; Console.WriteLine("탈퇴 성공"); } else { result = Result.Fail; Console.WriteLine("탈퇴 실패"); } } catch { Console.WriteLine("DataHandler::DeleteAccount.RemovePlayerData 에러"); result = Result.Fail; } ResultData resultData = new ResultData((byte)result); ResultPacket resultDataPacket = new ResultPacket(resultData); resultDataPacket.SetPacketId((int)ServerPacketId.DeleteAccountResult); byte[] msg = CreatePacket(resultDataPacket); packet = new DataPacket(msg, packet.client); lock (sendLock) { sendMsgs.Enqueue(packet); } }
//캐릭터 생성 public void CreateCharacter(DataPacket packet) { Console.WriteLine(packet.client.RemoteEndPoint.ToString() + "캐릭터 생성"); CreateCharacterPacket createCharacterPacket = new CreateCharacterPacket(packet.msg); CreateCharacterData createCharacterData = createCharacterPacket.GetData(); Console.WriteLine("이름 : " + createCharacterData.HName); Console.WriteLine("직업 : " + createCharacterData.HClass); Console.WriteLine("성별 : " + createCharacterData.Gender); string id = loginUser[packet.client]; UserData userData = database.GetUserData(id); Result result = Result.Fail; try { userData.CreateHero(createCharacterData); database.FileSave(id + ".data", userData); result = Result.Success; } catch { Console.WriteLine("DataHandler::CreateCharacter.CreateHero 에러"); result = Result.Fail; } ResultData resultData = new ResultData((byte)result); ResultPacket resultDataPacket = new ResultPacket(resultData); resultDataPacket.SetPacketId((int)ServerPacketId.CreateCharacterResult); byte[] msg = CreatePacket(resultDataPacket); packet = new DataPacket(msg, packet.client); lock (sendLock) { sendMsgs.Enqueue(packet); } }
//UDP 연결 확인 요청 -> Client public void RequestConnectionCheck(EndPoint endPoint) { Debug.Log(endPoint.ToString() + " 연결 체크 요청"); int index = NetworkManager.Instance.GetUserIndex(endPoint); ResultData resultData = new ResultData(new byte()); ResultPacket resultDataPacket = new ResultPacket(resultData); resultDataPacket.SetPacketId((int)P2PPacketId.RequestConnectionCheck); DataPacket packet = new DataPacket(CreateUdpPacket(resultDataPacket, udpId[index]), endPoint); sendMsgs.Enqueue(packet); SendData sendData = new SendData(udpId[index], endPoint, packet.msg); NetworkManager.Instance.ReSendManager.AddReSendData(sendData, index); udpId[index]++; }
//UDP 연결 완료 public void LoadingComplete(DataPacket packet) { Console.WriteLine(packet.client.RemoteEndPoint.ToString() + "UDP 연결 완료"); string id = loginUser[packet.client]; int roomNum = userState[id].state; int playerNum = roomManager.Room[roomNum].FindPlayerWithSocket(packet.client); roomManager.Room[roomNum].Ready[playerNum] = true; for (int i = 0; i < roomManager.Room[roomNum].PlayerNum; i++) { if (!roomManager.Room[roomNum].Ready[i]) { return; } } Console.WriteLine(roomManager.Room[roomNum] + "번 방 게임 시작"); ResultData resultData = new ResultData(); ResultPacket resultPacket = new ResultPacket(resultData); resultPacket.SetPacketId((int)ServerPacketId.StartDungeon); for (int i = 0; i < RoomManager.maxPlayerNum; i++) { if (roomManager.Room[roomNum].Socket[i] != null) { packet = new DataPacket(CreatePacket(resultPacket), roomManager.Room[roomNum].Socket[i]); lock (sendLock) { sendMsgs.Enqueue(packet); } } } }
public void Login(DataPacket packet) { Console.WriteLine(packet.client.RemoteEndPoint.ToString() + " 로그인"); AccountPacket accountPacket = new AccountPacket(packet.msg); AccountData accountData = accountPacket.GetData(); Console.WriteLine("아이디 : " + accountData.Id + "비밀번호 : " + accountData.Password); Result result = Result.Fail; try { if (database.AccountData.Contains(accountData.Id)) { if (((AccountData)database.AccountData[accountData.Id]).Password == accountData.Password) { List <string> id = new List <string>(loginUser.Values); if (!loginUser.ContainsValue(accountData.Id)) { result = Result.Success; Console.WriteLine("로그인 성공"); loginUser.Add(packet.client, accountData.Id); userState.Add(accountData.Id, new UserState(accountData.Id, -1)); database.AddUserData(accountData.Id); } else { Console.WriteLine("현재 접속중인 아이디입니다."); if (CompareIP(GetSocket(accountData.Id).RemoteEndPoint.ToString(), packet.client.RemoteEndPoint.ToString())) { loginUser.Remove(GetSocket(accountData.Id)); try { if (userState.ContainsKey(accountData.Id)) { if (userState[accountData.Id].state >= 0) { roomManager.ExitRoom(userState[accountData.Id].state, packet.client); } userState.Remove(accountData.Id); } } catch { Console.WriteLine("DataHandler::GameClose.ContainsKey - roomManager 에러"); Console.WriteLine("방에 입장하지 않았습니다."); } database.DeleteUserData(accountData.Id); Console.WriteLine("현재 접속중 해제"); } result = Result.Fail; } } else { Console.WriteLine("패스워드가 맞지 않습니다."); result = Result.Fail; } } else { Console.WriteLine("존재하지 않는 아이디입니다."); result = Result.Fail; } } catch (Exception e) { Console.WriteLine("DataHandler::Login.ContainsValue 에러" + e.Message); result = Result.Fail; } ResultData resultData = new ResultData((byte)result); ResultPacket resultDataPacket = new ResultPacket(resultData); resultDataPacket.SetPacketId((int)ServerPacketId.LoginResult); byte[] msg = CreatePacket(resultDataPacket); packet = new DataPacket(msg, packet.client); lock (sendLock) { sendMsgs.Enqueue(packet); } }