public void sendMessage(PokerTHMessage msg, Socket s) throws Exception { int size = msg.getSerializedSize(); byte[] header = new byte[4]; for (int i = 0; i < 4; i++) { header[i] = (new Integer(size >>> 24)).byteValue(); size <<= 8; } s.getOutputStream().write(header); s.getOutputStream().write(msg.toByteArray()); }
public PokerTHMessage receiveMessage(Socket s) throws Exception { byte[] header = new byte[4]; s.getInputStream().read(header); int size = 0; for (int i = 0; i < 4; i++) { size <<= 8; size |= (int) header[i]; } byte[] data = new byte[size]; s.getInputStream().read(data); return PokerTHMessage.parseFrom(data); }
void failOnErrorMessage(PokerTHMessage msg) { if (msg.hasLobbyMessage() && msg.getLobbyMessage().hasErrorMessage()) { ErrorMessage error = msg.getLobbyMessage().getErrorMessage(); fail("Received lobby error: " + error.getErrorReason().toString()); } else if (msg.hasGameMessage() && msg.getGameMessage().hasGameManagementMessage() && msg.getGameMessage().getGameManagementMessage().hasErrorMessage()) { ErrorMessage error = msg.getGameMessage().getGameManagementMessage().getErrorMessage(); fail("Received game error: " + error.getErrorReason().toString()); } }
public PokerTHMessage rejoinGameRequestMsg(int gameId, boolean autoLeave) { RejoinGameMessage rejoinRequest = RejoinGameMessage.newBuilder().setGameId(gameId).setAutoLeave(autoLeave).build(); LobbyMessage lobby = LobbyMessage.newBuilder() .setMessageType(LobbyMessageType.Type_RejoinGameMessage) .setRejoinGameMessage(rejoinRequest) .build(); PokerTHMessage msg = PokerTHMessage.newBuilder() .setMessageType(PokerTHMessageType.Type_LobbyMessage) .setLobbyMessage(lobby) .build(); return msg; }
public int guestInit(Socket s) throws Exception { int playerId = 0; PokerTHMessage msg = receiveMessage(s); assertTrue(msg.hasAnnounceMessage()); AnnounceMessage.Version requestedVersion = AnnounceMessage.Version.newBuilder() .setMajorVersion(PROTOCOL_VERSION_MAJOR) .setMinorVersion(PROTOCOL_VERSION_MINOR) .build(); AuthClientRequestMessage init = AuthClientRequestMessage.newBuilder() .setBuildId(0) .setLogin(AuthClientRequestMessage.LoginType.guestLogin) .setRequestedVersion(requestedVersion) .setNickName(GuestUser) .build(); AuthMessage auth = AuthMessage.newBuilder() .setMessageType(AuthMessageType.Type_AuthClientRequestMessage) .setAuthClientRequestMessage(init) .build(); msg = PokerTHMessage.newBuilder() .setMessageType(PokerTHMessageType.Type_AuthMessage) .setAuthMessage(auth) .build(); sendMessage(msg, s); msg = receiveMessage(s); if (msg.hasLobbyMessage() && msg.getMessageType() == PokerTHMessageType.Type_LobbyMessage && msg.getLobbyMessage().hasInitDoneMessage() && msg.getLobbyMessage().getMessageType() == LobbyMessageType.Type_InitDoneMessage) { InitDoneMessage initDone = msg.getLobbyMessage().getInitDoneMessage(); assertTrue(initDone.getYourPlayerId() != 0L); assertTrue(!initDone.hasYourAvatarHash()); playerId = initDone.getYourPlayerId(); } else { failOnErrorMessage(msg); fail("Invalid message."); } return playerId; }
public PokerTHMessage leaveGameRequestMsg(int gameId) { LeaveGameRequestMessage leaveRequest = LeaveGameRequestMessage.newBuilder().build(); GameManagementMessage gameManagement = GameManagementMessage.newBuilder() .setMessageType(GameManagementMessageType.Type_LeaveGameRequestMessage) .setLeaveGameRequestMessage(leaveRequest) .build(); GameMessage game = GameMessage.newBuilder() .setMessageType(GameMessageType.Type_GameManagementMessage) .setGameManagementMessage(gameManagement) .setGameId(gameId) .build(); PokerTHMessage msg = PokerTHMessage.newBuilder() .setMessageType(PokerTHMessageType.Type_GameMessage) .setGameMessage(game) .build(); return msg; }
public PokerTHMessage joinGameRequestMsg( int gameId, String password, boolean autoLeave, boolean spectateOnly) { JoinGameMessage.Builder joinBuilder = JoinGameMessage.newBuilder(); joinBuilder.setGameId(gameId); joinBuilder.setAutoLeave(autoLeave); joinBuilder.setSpectateOnly(spectateOnly); if (!password.isEmpty()) { joinBuilder.setPassword(password); } JoinGameMessage joinExisting = joinBuilder.build(); LobbyMessage lobby = LobbyMessage.newBuilder() .setMessageType(LobbyMessageType.Type_JoinGameMessage) .setJoinGameMessage(joinExisting) .build(); PokerTHMessage msg = PokerTHMessage.newBuilder() .setMessageType(PokerTHMessageType.Type_LobbyMessage) .setLobbyMessage(lobby) .build(); return msg; }
public PokerTHMessage createGameRequestMsg( NetGameInfo gameInfo, String password, boolean autoLeave) { CreateGameMessage.Builder createBuilder = CreateGameMessage.newBuilder(); createBuilder.setRequestId(1); createBuilder.setGameInfo(gameInfo); createBuilder.setAutoLeave(autoLeave); if (!password.isEmpty()) { createBuilder.setPassword(password); } CreateGameMessage createNew = createBuilder.build(); LobbyMessage lobby = LobbyMessage.newBuilder() .setMessageType(LobbyMessageType.Type_CreateGameMessage) .setCreateGameMessage(createNew) .build(); PokerTHMessage msg = PokerTHMessage.newBuilder() .setMessageType(PokerTHMessageType.Type_LobbyMessage) .setLobbyMessage(lobby) .build(); return msg; }
public int userInit(Socket s, String user, String password, byte[] avatarData, Guid lastSessionId) throws Exception { int playerId = 0; PokerTHMessage msg = receiveMessage(s); AnnounceMessage announce = msg.getAnnounceMessage(); assertTrue(announce.getServerType() == ServerType.serverTypeInternetAuth); ScramSha1 scramAuth = new ScramSha1(); // Send challenge. AnnounceMessage.Version requestedVersion = AnnounceMessage.Version.newBuilder() .setMajorVersion(PROTOCOL_VERSION_MAJOR) .setMinorVersion(PROTOCOL_VERSION_MINOR) .build(); AuthClientRequestMessage.Builder initBuilder = AuthClientRequestMessage.newBuilder() .setBuildId(0) .setLogin(AuthClientRequestMessage.LoginType.authenticatedLogin) .setRequestedVersion(requestedVersion) .setClientUserData(ByteString.copyFromUtf8(scramAuth.executeStep1(user))); if (avatarData != null) { initBuilder.setAvatarHash(ByteString.copyFrom(avatarData)); } if (lastSessionId != null && lastSessionId.value != null) { initBuilder.setMyLastSessionId(ByteString.copyFrom(lastSessionId.value)); } AuthClientRequestMessage init = initBuilder.build(); AuthMessage authRequest = AuthMessage.newBuilder() .setMessageType(AuthMessageType.Type_AuthClientRequestMessage) .setAuthClientRequestMessage(init) .build(); msg = PokerTHMessage.newBuilder() .setMessageType(PokerTHMessageType.Type_AuthMessage) .setAuthMessage(authRequest) .build(); sendMessage(msg, s); msg = receiveMessage(s); if (msg.hasAuthMessage() && msg.getMessageType() == PokerTHMessageType.Type_AuthMessage && msg.getAuthMessage().hasAuthServerChallengeMessage() && msg.getAuthMessage().getMessageType() == AuthMessageType.Type_AuthServerChallengeMessage) { String serverFirstMessage = new String( msg.getAuthMessage() .getAuthServerChallengeMessage() .getServerChallenge() .toStringUtf8()); AuthClientResponseMessage authClient = AuthClientResponseMessage.newBuilder() .setClientResponse( ByteString.copyFromUtf8(scramAuth.executeStep2(password, serverFirstMessage))) .build(); AuthMessage authResponse = AuthMessage.newBuilder() .setMessageType(AuthMessageType.Type_AuthClientResponseMessage) .setAuthClientResponseMessage(authClient) .build(); msg = PokerTHMessage.newBuilder() .setMessageType(PokerTHMessageType.Type_AuthMessage) .setAuthMessage(authResponse) .build(); sendMessage(msg, s); } failOnErrorMessage(msg); msg = receiveMessage(s); failOnErrorMessage(msg); msg = receiveMessage(s); if (msg.hasLobbyMessage() && msg.getMessageType() == PokerTHMessageType.Type_LobbyMessage && msg.getLobbyMessage().hasInitDoneMessage() && msg.getLobbyMessage().getMessageType() == LobbyMessageType.Type_InitDoneMessage) { InitDoneMessage initDone = msg.getLobbyMessage().getInitDoneMessage(); assertTrue(initDone.getYourPlayerId() != 0L); assertTrue(!initDone.hasYourAvatarHash()); playerId = initDone.getYourPlayerId(); if (lastSessionId != null) { lastSessionId.value = initDone.getYourSessionId().toByteArray(); } if (initDone.hasRejoinGameId()) { lastRejoinGameId = initDone.getRejoinGameId(); } else { lastRejoinGameId = 0; } } else { failOnErrorMessage(msg); fail("Invalid message."); } return playerId; }