Manual removal of session

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

ramana
Posts: 20
Joined: 16 Sep 2013, 14:47

Manual removal of session

Postby ramana » 14 Aug 2015, 06:46

Hello ,
We are developing a multiplayer online game with smartfox server version 2.0.
Java api as server side and flash as client side.

the problem we are facing is :

when the client is trying to login with invalid credentials we are validating with a method

"getApi().checkSecurePassword(session, dbPassword, cryptedPass)" in java.

if the credentials are valid we are making him login and joining the user.
But if credentials are not valid we are throwing a login exception.

The problem is after throwing a exception server is removing session from server that is after 10 to 15
secs.

If the user enters valid credentials and logs in , still the client recieves the session removed event.

Please suggest how to handle this situation.

the code we used for this is :

Code: Select all

"    IDBManager dbManager = getParentExtension().getParentZone()
               .getDBManager();
         Connection connection;

         try {
            // Grab a connection from the DBManager connection pool
            connection = dbManager.getConnection();

            // Build a prepared statement
            PreparedStatement stmt = connection
                  .prepareStatement("SELECT password,user_id FROM user WHERE user_id=?");
            stmt.setString(1, userName.trim());
            
            // Execute query
            ResultSet res = stmt.executeQuery();

            // Verify that one record was found
            if (!res.first()) {
               // This is the part that goes to the client
               SFSErrorData errData = new SFSErrorData(
                     SFSErrorCode.LOGIN_BAD_USERNAME);
               errData.addParameter(userName);
               
               
               // This is logged on the server side
               throw new SFSLoginException("Baddd user name: " + userName,
                     errData);
            }

            String dbPword = res.getString("password");

            // Verify the secure password
            if (!getApi()
                  .checkSecurePassword(session, dbPword, cryptedPass)) {
               SFSErrorData data = new SFSErrorData(
                     SFSErrorCode.LOGIN_BAD_PASSWORD);
               data.addParameter(userName);
               
               throw new SFSLoginException("Login failed for user: "
                     + userName, data);
            }

            // Return connection to the DBManager connection pool
            connection.close();
"

thanks.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Manual removal of session

Postby Lapo » 14 Aug 2015, 07:23

( Moved the SFS2X section )

Hi,
can you please check the server logs at the time of login and make sure there aren't any other exceptions going?
It seems like you have some other error stopping the login handler's execution.

Also the code you have posted is incomplete as I don't see the catch(...) block.
Also make sure to use a finally block where you close the connection, otherwise it won't return in the connection pool.
http://docs2x.smartfoxserver.com/Develo ... se-recipes

See the "Handling Exceptions" section

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
ramana
Posts: 20
Joined: 16 Sep 2013, 14:47

Re: Manual removal of session

Postby ramana » 14 Aug 2015, 07:38

Hi Lapo ,

below is the code we have in catch block:

"
// User name was not found
catch (SQLException e) {
SFSErrorData errData = new SFSErrorData(
SFSErrorCode.GENERIC_ERROR);
errData.addParameter("SQL Error: " + e.getMessage());

throw new SFSLoginException("A SQL Error occurred: "
+ e.getMessage(), errData);
}

"

the lnformation we got in log file is :

"

14 Aug 2015 | 12:54:10,341 | INFO | main | bluebox.v3.SessionFilter | | BlueBox-2X Service (3.0.0) ready.
14 Aug 2015 | 12:54:15,166 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.1.66:58462 } on Server port: 9933 <---> 58462
14 Aug 2015 | 12:54:15,166 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.1.66:58462 } on Server port: 9933 <---> 58462
14 Aug 2015 | 12:54:15,221 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 2, Type: DEFAULT, Logged: No, IP: 192.168.1.66:58463 } on Server port: 9933 <---> 58463
14 Aug 2015 | 12:54:15,221 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 2, Type: DEFAULT, Logged: No, IP: 192.168.1.66:58463 } on Server port: 9933 <---> 58463
14 Aug 2015 | 12:54:15,238 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.1.66:58462 }
14 Aug 2015 | 12:54:15,238 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.1.66:58462 }
14 Aug 2015 | 12:54:15,335 | WARN | SFSWorker:Ext:3 | entities.managers.SFSExtensionManager | | com.smartfoxserver.v2.exceptions.SFSLoginException: Login failed for user: test1
14 Aug 2015 | 12:54:15,335 | WARN | SFSWorker:Ext:3 | entities.managers.SFSExtensionManager | | com.smartfoxserver.v2.exceptions.SFSLoginException: Login failed for user: test1
14 Aug 2015 | 12:54:46,835 | INFO | Scheduler1-thread-1 | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 2, Type: DEFAULT, Logged: No, IP: 192.168.1.66:58463 }
14 Aug 2015 | 12:54:46,835 | INFO | Scheduler1-thread-1 | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 2, Type: DEFAULT, Logged: No, IP: 192.168.1.66:58463 }
14 Aug 2015 | 12:55:06,901 | INFO | pool-1-thread-2 | util.stats.CCULoggerTask | | CCU stats: { Zone: teenpatti }, CCU: 0/0
14 Aug 2015 | 12:55:06,901 | INFO | pool-1-thread-2 | util.stats.CCULoggerTask | | CCU stats: { Zone: teenpatti }, CCU: 0/0
14 Aug 2015 | 12:55:06,910 | INFO | pool-1-thread-2 | util.stats.CCULoggerTask | | CCU stats: { Zone: --=={{{ AdminZone }}}==-- }, CCU: 0/0
14 Aug 2015 | 12:55:06,910 | INFO | pool-1-thread-2 | util.stats.CCULoggerTask | | CCU stats: { Zone: --=={{{ AdminZone }}}==-- }, CCU: 0/0
14 Aug 2015 | 12:55:06,919 | INFO | pool-1-thread-2 | util.stats.CCULoggerTask | | CCU stats: CCU: 0/0
14 Aug 2015 | 12:55:06,919 | INFO | pool-1-thread-2 | util.stats.CCULoggerTask | | CCU stats: CCU: 0/0

"

Thanks.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Manual removal of session

Postby Lapo » 14 Aug 2015, 07:43

Please post the entire class that you use as a Login Handler, and use the Code button in the post window to format it correctly.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
ramana
Posts: 20
Joined: 16 Sep 2013, 14:47

Re: Manual removal of session

Postby ramana » 14 Aug 2015, 07:59

thanks lapo , for your quick reply,

Below is the class we are using for "LoginEventHandler" :

Code: Select all

/**
 *
 */
package game.ourgame.events;

/**
 * @author ***
 *
 */
import game.teenpatti.modal.PlayerProfileModal;
import game.teenpatti.utils.Appmethods;
import game.teenpatti.utils.Commands;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class LoginEventHandler extends BaseServerEventHandler {

   public void handleServerEvent(ISFSEvent event) throws SFSException {
      // Grab parameters from client request
      String userName = (String) event.getParameter(SFSEventParam.LOGIN_NAME);

      userName = userName.trim();
      String cryptedPass = (String) event
            .getParameter(SFSEventParam.LOGIN_PASSWORD);
      ISession session = (ISession) event.getParameter(SFSEventParam.SESSION);
      
      if (!userName.equals("admin")) {
         IDBManager dbManager = getParentExtension().getParentZone()
               .getDBManager();
         Connection connection;

         try {
            // Grab a connection from the DBManager connection pool
            connection = dbManager.getConnection();

            // Build a prepared statement
            PreparedStatement stmt = connection
                  .prepareStatement("SELECT password,user_id FROM user WHERE user_id=?");
            stmt.setString(1, userName.trim());
            
            // Execute query
            ResultSet res = stmt.executeQuery();

            // Verify that one record was found
            if (!res.first()) {
               // This is the part that goes to the client
               SFSErrorData errData = new SFSErrorData(
                     SFSErrorCode.LOGIN_BAD_USERNAME);
               errData.addParameter(userName);
               
               
               // This is logged on the server side
               throw new SFSLoginException("Baddd user name: " + userName,
                     errData);
            }

            String dbPword = res.getString("password");

            // Verify the secure password
            if (!getApi()
                  .checkSecurePassword(session, dbPword, cryptedPass)) {
               SFSErrorData data = new SFSErrorData(
                     SFSErrorCode.LOGIN_BAD_PASSWORD);
               data.addParameter(userName);
               
               throw new SFSLoginException("Login failed for user: "
                     + userName, data);
            }

            // Return connection to the DBManager connection pool
            connection.close();
         }

         // User name was not found
         catch (SQLException e) {
            SFSErrorData errData = new SFSErrorData(
                  SFSErrorCode.GENERIC_ERROR);
            errData.addParameter("SQL Error: " + e.getMessage());

            throw new SFSLoginException("A SQL Error occurred: "
                  + e.getMessage(), errData);
         }

         PlayerProfileModal ppModal = Commands.appInstance.getPlayerProfiles().get(userName);

         if (ppModal != null) {
            // Modify
            ppModal.setPlyerIP(session.getAddress());
         } else {
            // Create
            ppModal = new PlayerProfileModal();
            ppModal.setPlayerId(userName);
            ppModal.setPlyerIP(session.getAddress());
            // ppModal.setGender(Commands.appInstance.dbConn.getPlayerGender(userName));
            Commands.appInstance.getPlayerProfiles().put(userName, ppModal);
         }
      
      }
   }
}


Thanks.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Manual removal of session

Postby Lapo » 14 Aug 2015, 08:24

Thanks.
Can you log something at the end of the method to see if the thread is leaving the method correctly?

Like:

Code: Select all

trace("Exiting login handler...");

as the last line of your code.

Also you should fix the try/catch with a finally as I recommended otherwise every exception will cause a connection leak.

If the method returns without firing any exceptions there's no good reason why the client shouldn't be logged in.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
ramana
Posts: 20
Joined: 16 Sep 2013, 14:47

Re: Manual removal of session

Postby ramana » 14 Aug 2015, 09:54

Hello lapo,

I have placed trace at the end of my method as below and tried the same.
trace was not printed , thread has terminated before.
Please see the code and logs :

Code :

Code: Select all

/**
 *
 */
package game.teenpatti.events;

/**
 * @author ***
 *
 */
import game.teenpatti.modal.PlayerProfileModal;
import game.teenpatti.utils.Commands;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class LoginEventHandler extends BaseServerEventHandler {

   public void handleServerEvent(ISFSEvent event) throws SFSException {
      // Grab parameters from client request
      String userName = (String) event.getParameter(SFSEventParam.LOGIN_NAME);

      userName = userName.trim();
      String cryptedPass = (String) event
            .getParameter(SFSEventParam.LOGIN_PASSWORD);
      ISession session = (ISession) event.getParameter(SFSEventParam.SESSION);

      if (!userName.equals("admin")) {
         IDBManager dbManager = getParentExtension().getParentZone()
               .getDBManager();
         Connection connection = null;
         // Grab a connection from the DBManager connection pool

         try {
            connection = dbManager.getConnection();

            // Build a prepared statement
            PreparedStatement stmt = connection
                  .prepareStatement("SELECT password,user_id FROM user WHERE user_id=?");
            stmt.setString(1, userName.trim());

            // Execute query
            ResultSet res = stmt.executeQuery();

            // Verify that one record was found
            if (!res.first()) {
               // This is the part that goes to the client
               SFSErrorData errData = new SFSErrorData(
                     SFSErrorCode.LOGIN_BAD_USERNAME);
               errData.addParameter(userName);

               // This is logged on the server side
               throw new SFSLoginException("Baddd user name: " + userName,
                     errData);
            }

            String dbPword = res.getString("password");

            // Verify the secure password
            if (!getApi()
                  .checkSecurePassword(session, dbPword, cryptedPass)) {
               SFSErrorData data = new SFSErrorData(
                     SFSErrorCode.LOGIN_BAD_PASSWORD);
               data.addParameter(userName);

               trace(" Pwd is Invalid !! ");
               
               throw new SFSLoginException("Login failed for user: "
                     + userName, data);
            }
         }

         // User name was not found
         catch (SQLException e) {
            SFSErrorData errData = new SFSErrorData(
                  SFSErrorCode.GENERIC_ERROR);
            errData.addParameter("SQL Error: " + e.getMessage());

            throw new SFSLoginException("A SQL Error occurred: "
                  + e.getMessage(), errData);
         }

         finally {
            try {
               if (connection != null) {
                  connection.close();
               }
            } catch (SQLException sqlee) {
               sqlee.printStackTrace();
            }
         }

         PlayerProfileModal ppModal = Commands.appInstance
               .getPlayerProfiles().get(userName);

         if (ppModal != null) {
            ppModal.setPlyerIP(session.getAddress());
         } else {
            // Create
            ppModal = new PlayerProfileModal();
            ppModal.setPlayerId(userName);
            ppModal.setPlyerIP(session.getAddress());
            Commands.appInstance.getPlayerProfiles().put(userName, ppModal);
         }

      }
      
      trace("Exiting login handler...");

   }

}



Log :

14 Aug 2015 | 15:06:12,990 | INFO | main | bluebox.v3.SessionFilter | | BlueBox-2X Service (3.0.0) ready.
14 Aug 2015 | 15:06:17,439 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.1.66:51603 } on Server port: 9933 <---> 51603
14 Aug 2015 | 15:06:17,439 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.1.66:51603 } on Server port: 9933 <---> 51603
14 Aug 2015 | 15:06:17,480 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.1.66:51603 }
14 Aug 2015 | 15:06:17,480 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 1, Type: DEFAULT, Logged: No, IP: 192.168.1.66:51603 }
14 Aug 2015 | 15:06:17,512 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 2, Type: DEFAULT, Logged: No, IP: 192.168.1.66:51604 } on Server port: 9933 <---> 51604
14 Aug 2015 | 15:06:17,512 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 2, Type: DEFAULT, Logged: No, IP: 192.168.1.66:51604 } on Server port: 9933 <---> 51604
14 Aug 2015 | 15:06:17,734 | INFO | SFSWorker:Ext:3 | Extensions | | {teenpatti}: Pwd is Invalid !!
14 Aug 2015 | 15:06:17,734 | INFO | SFSWorker:Ext:3 | Extensions | | {teenpatti}: Pwd is Invalid !!
14 Aug 2015 | 15:06:17,740 | WARN | SFSWorker:Ext:3 | entities.managers.SFSExtensionManager | | com.smartfoxserver.v2.exceptions.SFSLoginException: Login failed for user: test1
14 Aug 2015 | 15:06:17,740 | WARN | SFSWorker:Ext:3 | entities.managers.SFSExtensionManager | | com.smartfoxserver.v2.exceptions.SFSLoginException: Login failed for user: test1
14 Aug 2015 | 15:06:49,622 | INFO | Scheduler1-thread-1 | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 2, Type: DEFAULT, Logged: No, IP: 192.168.1.66:51604 }
14 Aug 2015 | 15:06:49,622 | INFO | Scheduler1-thread-1 | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 2, Type: DEFAULT, Logged: No, IP: 192.168.1.66:51604 }



Thanks.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Manual removal of session

Postby Lapo » 14 Aug 2015, 10:00

I have no idea... there must be something in your code that is not allowing the thread to return.
I can only suggest to debug your code and see at which point the problem is happening.
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Thomasea and 59 guests