Class AbstractConnection

java.lang.Object
megamek.common.net.connections.AbstractConnection
Direct Known Subclasses:
DataStreamConnection

public abstract class AbstractConnection extends Object
Generic bidirectional connection between client and server
  • Field Details

    • marshallingType

      protected int marshallingType
      Type of marshalling used to represent sent packets
    • marshaller

      protected PacketMarshaller marshaller
      Marshaller used to send packets
  • Constructor Details

    • AbstractConnection

      public AbstractConnection(String host, int port, int id)
      Creates new client (connection from client to server) connection
      Parameters:
      host - target host
      port - target port
      id - connection ID
    • AbstractConnection

      public AbstractConnection(Socket socket, int id)
      Creates new Server connection
      Parameters:
      socket - accepted socket
      id - connection ID
  • Method Details

    • isServer

      public boolean isServer()
      Returns:
      True if this is the Server connection.
    • getMarshallingType

      protected int getMarshallingType()
      Returns:
      The type of the marshalling used to send packets.
    • setMarshallingType

      protected void setMarshallingType(int marshallingType)
      Sets the type of the marshalling used to send packets
      Parameters:
      marshallingType - new marshalling type
    • open

      public boolean open()
      Opens the connection
      Returns:
      True on success, false otherwise
    • close

      public void close()
      Closes the socket and shuts down the receiver and sender threads.
    • isClosed

      public boolean isClosed()
      Returns:
      True if the socket for this connection has been closed (or is null).
    • getId

      public int getId()
      Returns:
      the connection ID.
    • setId

      public void setId(int id)
      Sets the connection ID Note: Be careful with using this method
      Parameters:
      id - new connection ID
    • getInetAddress

      public String getInetAddress()
      Returns:
      The address this socket is or was connected to.
    • isCompressed

      public boolean isCompressed()
      Returns:
      True if this connection compresses the sent data.
    • setCompression

      @Deprecated(since="0.51.0", forRemoval=true) public void setCompression(boolean compress)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Sets the compression
      Parameters:
      compress - True when compression is to be used
    • send

      public void send(Packet packet)
      Adds a packet to the send queue to be sent on a separate thread.
    • sendNow

      public void sendNow(SendPacket packet)
      Send the packet now, on a separate thread; This is the blocking call.
    • hasPending

      public boolean hasPending()
      Returns:
      True if there are pending packets.
    • getBytesSent

      public long getBytesSent()
      Returns:
      a very approximate count of how many bytes were sent.
    • getBytesReceived

      public long getBytesReceived()
      Returns:
      a very approximate count of how many bytes were received.
    • addConnectionListener

      public void addConnectionListener(ConnectionListener listener)
      Adds the specified connection listener to receive connection events from connection.
      Parameters:
      listener - the connection listener.
    • removeConnectionListener

      @Deprecated(since="0.51.0", forRemoval=true) public void removeConnectionListener(ConnectionListener listener)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Removes the specified connection listener.
      Parameters:
      listener - the connection listener.
    • getConnectionTypeText

      protected String getConnectionTypeText()
      Returns:
      The connection type ("Client" / "Server") that is used in the debug messages and so on.
    • getInputStream

      protected InputStream getInputStream() throws IOException
      Returns:
      an input stream
      Throws:
      IOException
    • getOutputStream

      protected OutputStream getOutputStream() throws IOException
      Returns:
      an output stream
      Throws:
      IOException
    • getSendBufferSize

      protected int getSendBufferSize() throws SocketException
      Throws:
      SocketException
    • getReceiveBufferSize

      protected int getReceiveBufferSize() throws SocketException
      Throws:
      SocketException
    • update

      public void update()
      Process all incoming data, blocking on the input stream until new input is available. This method should not be synchronized as it should only deal with the input side of things. Without creating separate read/write locks, making this method synchronized would not allow synchronous reads and writes.
    • flush

      public void flush()
      Send all queued packets. This method is synchronized since it deals with the non-thread-safe send queue.
    • processPacket

      protected void processPacket(INetworkPacket np) throws Exception
      Processes a received packet.
      Throws:
      Exception
    • processPacket

      protected void processPacket(SendPacket packet)
      Processes a packet to be sent.
    • readNetworkPacket

      protected abstract INetworkPacket readNetworkPacket() throws Exception
      Reads a complete NetworkPacket. This method must not block, must return null instead.
      Returns:
      the NetworkPacket that was sent.
      Throws:
      Exception
    • sendNetworkPacket

      protected abstract void sendNetworkPacket(byte[] data, boolean zipped) throws Exception
      Sends the data. This must not be blocked for too long
      Parameters:
      data - data to send
      zipped - should the data be compressed
      Throws:
      Exception - if there's an issue with sending the packet
    • processConnectionEvent

      protected void processConnectionEvent(AbstractConnectionEvent event)
      Processes game events occurring on this connection by dispatching them to any registered GameListener objects.
      Parameters:
      event - the game event.