Class Session

java.lang.Object
br.net.dd.netherwingcore.bnetserver.server.Session

public class Session extends Object
Represents a client session in the BNet server. This class manages the state of a single client connection, including reading and writing data, handling SSL encryption/decryption, and dispatching requests to the appropriate services. Each session is associated with a SocketChannel and an SSLEngine for secure communication.
  • Constructor Details

    • Session

      public Session(SocketChannel socketChannel, SSLEngine sslEngine)
      Constructs a new Session for a given SocketChannel and SSLEngine.
      Parameters:
      socketChannel - The SocketChannel associated with this session.
      sslEngine - The SSLEngine for handling SSL encryption/decryption for this session.
  • Method Details

    • start

      public void start()
      Starts the SSL handshake process for this session. This method will initiate the handshake and handle the necessary steps to establish a secure connection with the client. If the handshake fails, the session will be closed.
    • readFromSocket

      public int readFromSocket() throws IOException
      Reads data from the socket and processes it through the SSL engine. This method handles reading encrypted data from the network, decrypting it using the SSLEngine, and storing the decrypted application data in the read buffer. It also manages the SSL handshake state and logs relevant information about the connection and received data. If the peer closes the connection or if there is an SSL error, this method will handle those cases appropriately.
      Returns:
      The number of bytes read from the socket, or -1 if the connection was closed by the peer.
      Throws:
      IOException - If an I/O error occurs during reading from the socket or processing SSL data.
    • processWriteQueue

      public boolean processWriteQueue() throws IOException
      Processes the write queue by encrypting and sending any pending application data to the client. This method handles SSL wrapping of the application data and manages the SSL handshake state if necessary. It will attempt to send all queued messages, handling buffer resizing as needed. If the handshake is not yet complete, it will prioritize completing the handshake before sending application data.
      Returns:
      true if all data in the write queue was successfully sent, false if there is still data pending to be sent.
      Throws:
      IOException - If an I/O error occurs during the SSL wrapping or socket writing process.
    • readHandler

      public SocketReadCallbackResult readHandler()
      Handles reading data from the session's read buffer. This method processes incoming data in stages: reading the header length, reading the header, and reading the payload. It uses helper methods to manage each stage and returns appropriate results based on the processing state.
      Returns:
      A SocketReadCallbackResult indicating whether to keep reading or if processing is complete.
    • sendResponse

      public void sendResponse(int token, com.google.protobuf.Message response)
      Sends a response to the client. This method takes an authentication token and a protobuf Message as the response to be sent. It constructs a header for the response, including the token, service ID, and size of the serialized message. It then creates a MessageBuffer containing the header and the serialized message, and adds it to the write queue to be sent to the client.
      Parameters:
      token - The authentication token associated with this response.
      response - The protobuf Message to be sent as a response to the client.
    • sendResponse

      public void sendResponse(int token, int status)
      Sends a response to the client with a specific status code. This method is used for sending responses that do not include a protobuf message body, but still need to include a header with the token and status. It constructs a header with the provided token and status, and adds it to the write queue to be sent to the client.
      Parameters:
      token - The authentication token associated with this response.
      status - The status code to be included in the response header.
    • hasDataToWrite

      public boolean hasDataToWrite()
      Checks if there is data in the write queue that needs to be sent to the client. This method is used to determine if the session has pending responses that need to be processed and sent.
      Returns:
      true if there is data in the write queue or if the SSL handshake requires a wrap operation, false otherwise.
    • closeSocket

      public void closeSocket()
      Closes the socket and the SSL connection associated with this session. This method ensures that the SSL engine is properly closed before closing the underlying SocketChannel. It also logs the closure of the session and handles any IOException that may occur during the closing process.
    • isOpen

      public boolean isOpen()
      Checks if the underlying SocketChannel is still open. This is important for determining if the session is still active and can be used for communication with the client.
      Returns:
      true if the SocketChannel is open, false otherwise.
    • getClientInfo

      public String getClientInfo()
      Retrieves information about the connected client, such as its remote address.
      Returns:
      A string representation of the client's remote address, or "unknown" if it cannot be determined.
    • isIdle

      public boolean isIdle()
      Checks if the session has been idle for longer than the defined timeout period. This method considers both the time since the last activity and the time since the handshake was completed. If the handshake is completed but no data has been received, it uses a shorter timeout to determine idleness. This helps to identify clients that connect but do not send any data after the handshake.
      Returns:
      true if the session is considered idle, false otherwise.
    • getReadBuffer

      public MessageBuffer getReadBuffer()
    • isAuthenticated

      public boolean isAuthenticated()
    • setAuthenticated

      public void setAuthenticated(boolean authenticated)
    • getAccountName

      public String getAccountName()
    • setAccountName

      public void setAccountName(String accountName)
    • getAccountId

      public int getAccountId()
    • setAccountId

      public void setAccountId(int accountId)