Class SocketWrapper

java.lang.Object
it.polimi.ingsw.Network.SocketWrapper
Direct Known Subclasses:
KeepAliveSocketWrapper

public class SocketWrapper extends Object
Wrapper around a Socket transfering Messages, removes most of the overhead of handling the connection.
  • Field Details

  • Constructor Details

    • SocketWrapper

      public SocketWrapper(Socket socket) throws IOException
      Create the SocketWrapper around a standard socket
      Parameters:
      socket - the socket to wrap around
      Throws:
      IOException - if the socket/wrapper has issues opening its streams
  • Method Details

    • awaitMessage

      public Message awaitMessage() throws IOException
      Blocks until a message is read
      Returns:
      the read Message
      Throws:
      SocketException - if there's an error while reading from the object stream, the socket will close and this exception is thrown
      IOException
    • close

      public void close() throws IOException
      Attempts to close the socket, if the socket is already closed then it does nothing
      Throws:
      IOException - if any exception happens during closing of the socket
    • isClosed

      public boolean isClosed()
      Check to see if the socket is closed
      Returns:
      true if the socket is closed
    • sendMessage

      public void sendMessage(Message message) throws IOException
      Sends a message to the socket endpoint
      Parameters:
      message - the message to send, required not null
      Throws:
      IOException - if any error happens during the sending of the message
    • getInetAddress

      public InetAddress getInetAddress()
      Get the address this socket is bound to
      Returns:
      the address this socket is connected to.