diff --git a/src/net/common/servergame.cpp b/src/net/common/servergame.cpp index d88e876b..34cde66d 100644 --- a/src/net/common/servergame.cpp +++ b/src/net/common/servergame.cpp @@ -1171,8 +1171,8 @@ ServerGame::AddPlayerToNumJoinsPerPlayer(const std::string &playerName) NumJoinsPerPlayerMap::iterator pos = m_numJoinsPerPlayer.find(playerName); if (pos != m_numJoinsPerPlayer.end()) { - pos->second++; - } else{ + pos->second++; + } else { m_numJoinsPerPlayer[playerName] = 1; } } diff --git a/src/net/common/servergamestate.cpp b/src/net/common/servergamestate.cpp index 14379fb7..ab0b5dcc 100644 --- a/src/net/common/servergamestate.cpp +++ b/src/net/common/servergamestate.cpp @@ -84,7 +84,7 @@ using namespace std; #define SERVER_LOOP_DELAY_MSEC 50 #define SERVER_MAX_NUM_SPECTATORS_PER_GAME 100 -#define GAME_MAX_NUM_JOINS_PER_PLAYER 6 +#define GAME_MAX_NUM_JOINS_PER_PLAYER 6 // Helper functions diff --git a/src/net/common/sessionmanager.cpp b/src/net/common/sessionmanager.cpp index 097c3e69..67599b62 100644 --- a/src/net/common/sessionmanager.cpp +++ b/src/net/common/sessionmanager.cpp @@ -260,9 +260,9 @@ SessionManager::IsGuestAllowedToConnect(const std::string &clientAddress) const int num = 0; while (i != end) { boost::shared_ptr tmpPlayer(i->second->GetPlayerData()); - if(tmpPlayer && tmpPlayer->GetRights() == PLAYER_RIGHTS_GUEST){ + if(tmpPlayer && tmpPlayer->GetRights() == PLAYER_RIGHTS_GUEST) { num++; - if(i->second->GetClientAddr() == clientAddress || num >= SERVER_MAX_GUEST_USERS_LOBBY) { + if(i->second->GetClientAddr() == clientAddress) { // guest has same ip as another guest in lobby or // number of guests in lobby >= SERVER_MAX_GUEST_USERS_LOBBY => not allowed retVal = false; @@ -271,6 +271,10 @@ SessionManager::IsGuestAllowedToConnect(const std::string &clientAddress) const } ++i; } + + // Check if number of players in lobby exceeds max + if (num >= SERVER_MAX_GUEST_USERS_LOBBY) retVal = false; + return retVal; } diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index 40038432..c26486b7 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -36,7 +36,6 @@ #include #include #include -#include #include #include diff --git a/tests/src/de/pokerth/test/GuestLoginThreadedTest.java b/tests/src/de/pokerth/test/GuestLoginThreadedTest.java deleted file mode 100644 index be661a6e..00000000 --- a/tests/src/de/pokerth/test/GuestLoginThreadedTest.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.pokerth.test; - -import org.junit.Test; - -public class GuestLoginThreadedTest extends TestBase { - - @Test - public void testInitThreadedMessage() throws Exception { - guestInitThreaded(); - } -} diff --git a/tests/src/de/pokerth/test/TestBase.java b/tests/src/de/pokerth/test/TestBase.java index d5bb3b79..4e80c327 100644 --- a/tests/src/de/pokerth/test/TestBase.java +++ b/tests/src/de/pokerth/test/TestBase.java @@ -389,243 +389,4 @@ public abstract class TestBase { return cipher.doFinal(ciphertext); } - - /** - * Description: - * - * This test runs several guests users in PokerTH Server. - * Starts N threads thats connects to server "at once": There is a delay between each connection, to avoid error 133 - * (NOTE: I didn't succeed avoid this message. So, it's better to disable "ServerBruteForceProtection" in config.xml) - * Once connected, each thread waits until all other connects to simulate N users at once - * - * When all threads are connected, each thread gets unlocked and close connections, in an orderly way. - * - * - * Running: - * - * Tests accepts JVM property options: - * -Dpthserver.type, type of server: 'S' for dedicated, 'O' for official (default) - * -Dpthserver.passwd, password for dedicated server - * -Dpthserver.numthreads, number of threads (guests users) launched (5 default) - * -Dpthserver.waitfor, time all users remain connected until released. - * Format: XXX[m,s,M] for m(milliseconds), s(seconds), M(minutes). Eg: 1M (1 minute), 45s (45 seconds) - * Default: 1s - * - * For DEDICATED_SERVER Server must be faked to accept guest connections (aren't allowed by default). - * - * Purpose: - * - * Final purpose of this test, is check whether a guest connection isn't permitted - * once the maximum number of guests players allowed is reached - * To achieve that, N (NUM_THREADS) must be SERVER_MAX_GUEST_USERS + 1. That value is defined in ServerLobbyThread - * In that case, test will fail, because it's not supposed to receive that error message. - * Test must be corrected: - * - Last thread launched (or the one that makes SERVER_MAX_GUEST_USERS + 1, and next ones), must receive an error - * - To do that, msg.hasErrorMessage() must be true, and reason (msg.getErrorMessage().getErrorReason()) - * must be ERR_NET_SERVER_FULL (error gameIsFull) - * - * - * TODO: - * - * This test must be modified to adapt to: - * - Java8 (much simple threads) Runnable name = () -> {code} - * - Modern concurrency design: using Executors, Barriers and CountDownLatch - * - Test a guarantee exception when limit is reached. - * - * @author albmed - * - * @return - * @throws Exception - */ - public int guestInitThreaded() throws Exception { - - String serverType = System.getProperty("pthserver.type"); - String serverPasswd = System.getProperty("pthserver.passwd"); - String numThreadsStr = System.getProperty("pthserver.numthreads"); - String sleepForStr = System.getProperty("pthserver.waitfor"); - - // if serverType not provided or wrong, suppose official server - if (serverType == null || !serverType.matches("[OoSs]")) { - serverType = "O"; - serverPasswd = ""; - } - - // if dedicated server passwd is mandatory - if (serverType.equalsIgnoreCase("S") && (serverPasswd == null || serverPasswd.trim().length() == 0)) - fail("Server Passwd needed on Dedicated server"); - - // if numThreads not provided supposed 5 (must match with SERVER_MAX_GUEST_USERS) - int numThreads = 5; - if (numThreadsStr != null && numThreadsStr.matches("[0-9]+")) - numThreads = Integer.valueOf(numThreadsStr).intValue(); - - // waits for, at least 1sec, when all threads are alive and waiting - // can wait longer if, a one want to do a test with a real client. - // Format is: time[unit], where unit is "m" (milli), "s" (seconds) or "M" (minutes) - long sleepFor = 1000L; - if (sleepForStr != null && sleepForStr.matches("([0-9]+)([msM]?)")) { - long units = 1L; - if ((sleepForStr.charAt(sleepForStr.length() - 1) + "").matches("[msM]")) { - switch (sleepForStr.charAt(sleepForStr.length() - 1)) { - case 'm': units = 1L; break; // milliseconds - case 's': units = 1000L; break; // seconds - case 'M': units = 60L*1000L; break; // minutes - default: break; - } - sleepForStr = sleepForStr.substring(0, sleepForStr.length() -1); - } - - long tmp = Long.valueOf(sleepForStr).longValue() * units; - if (tmp > sleepFor) sleepFor = tmp; - } - - class ThreadGuest implements Runnable { - Thread t; - Object lock; - String name; - String type; - Socket s; - String passwd; - - public ThreadGuest(String _name, String _type, String _passwd, Object _lock) { - this.name = _name; - if (_lock == null) lock = new Object(); - this.lock = _lock; - this.type = _type; - this.passwd = _passwd; - } - - public Object getLock() { return this.lock; } - public Thread getThread() { return this.t; } - - public void start() { - if (t == null) { - t = new Thread(this); - t.start(); - } - } - - @Override - public void run() { - int playerId = connect(); - - System.out.println("[" + System.currentTimeMillis() + " - " + name + "]: PlayerId: " + playerId + " connected"); - - // Wait to make all guests users connected at once - synchronized (lock) { - try { - lock.wait(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - System.out.println("[" + System.currentTimeMillis() + " - " + name + "]: PlayerId: " + playerId + " released"); - - // Close conn - if (s != null && !s.isClosed()) try {s.close();} catch (IOException e) {} - } - - public int connect() { - Exception excep = null; - int playerId = -1; - - try { - s = new Socket("localhost", 7234); - PokerTHMessage msg = receiveMessage(s); - assertTrue(msg.hasAnnounceMessage()); - - if (type.equalsIgnoreCase("S")) assertTrue(msg.getAnnounceMessage().getServerType() == ServerType.serverTypeInternetNoAuth); - - AnnounceMessage.Version requestedVersion = AnnounceMessage.Version.newBuilder() - .setMajorVersion(PROTOCOL_VERSION_MAJOR) - .setMinorVersion(PROTOCOL_VERSION_MINOR) - .build(); - - InitMessage init; - if (type.equalsIgnoreCase("S")) { - init = InitMessage.newBuilder() - .setBuildId(0) - .setLogin(InitMessage.LoginType.unauthenticatedLogin) - .setRequestedVersion(requestedVersion) - .setNickName(name).setAuthServerPassword(passwd) - .build(); - } - else { - init = InitMessage.newBuilder() - .setBuildId(0) - .setLogin(InitMessage.LoginType.guestLogin) - .setRequestedVersion(requestedVersion) - .setNickName(name) - .build(); - } - - msg = PokerTHMessage.newBuilder() - .setMessageType(PokerTHMessageType.Type_InitMessage) - .setInitMessage(init) - .build(); - - sendMessage(msg, s); - - msg = receiveMessage(s); - failOnErrorMessage(msg); - - if (msg.hasInitAckMessage() && msg.getMessageType() == PokerTHMessageType.Type_InitAckMessage) { - InitAckMessage initAck = msg.getInitAckMessage(); - assertTrue(initAck.getYourPlayerId() != 0L); - assertTrue(!initAck.hasYourAvatarHash()); - playerId = initAck.getYourPlayerId(); - } - else { - failOnErrorMessage(msg); - fail("Invalid message."); - } - - } - catch (Exception e) { - excep = e; - e.printStackTrace(); - } - finally { - if (excep != null) { - if (s != null) try {s.close();} catch (IOException e) {} - } - } - - return playerId; - } - - } - - ThreadGuest[] threads = new ThreadGuest[numThreads]; - - for (int i = 0; i < threads.length; i++) { - threads[i] = new ThreadGuest("Guest" + String.format("%05d",i), serverType, serverPasswd, new Object()); - threads[i].start(); - - // Must wait over a second, before start next thread, to avoid error 133 (ERR_NET_INIT_BLOCKED) - // Don't know why this don't works. I had to disable ServerBruteForceProtection on server config file. - // So, this can be reduced to a few milliseconds (better not to comment or remove). - Thread.sleep(1500L); - } - - // All threads are running and waiting. - Thread.sleep(sleepFor); - - // Unlock, and let threads die. - for (int i = 0; i < threads.length; i++) { - Object lock = threads[i].getLock(); - synchronized (lock) { - lock.notify(); - } - try { - threads[i].getThread().join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - return -1; - } - }