Package megamek.common.net.connections
Class AbstractConnection
java.lang.Object
megamek.common.net.connections.AbstractConnection
- Direct Known Subclasses:
DataStreamConnection
Generic bidirectional connection between client and server
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected PacketMarshallerMarshaller used to send packetsprotected intType of marshalling used to represent sent packets -
Constructor Summary
ConstructorsConstructorDescriptionAbstractConnection(String host, int port, int id) Creates new client (connection from client to server) connectionAbstractConnection(Socket socket, int id) Creates new Server connection -
Method Summary
Modifier and TypeMethodDescriptionvoidaddConnectionListener(ConnectionListener listener) Adds the specified connection listener to receive connection events from connection.voidclose()Closes the socket and shuts down the receiver and sender threads.voidflush()Send all queued packets.longlongprotected StringintgetId()protected InputStreamprotected intprotected OutputStreamprotected intprotected intbooleanbooleanisClosed()booleanbooleanisServer()booleanopen()Opens the connectionprotected voidProcesses game events occurring on this connection by dispatching them to any registered GameListener objects.protected voidProcesses a received packet.protected voidprocessPacket(SendPacket packet) Processes a packet to be sent.protected abstract INetworkPacketReads a complete NetworkPacket.voidremoveConnectionListener(ConnectionListener listener) Deprecated, for removal: This API element is subject to removal in a future version.voidAdds a packet to the send queue to be sent on a separate thread.protected abstract voidsendNetworkPacket(byte[] data, boolean zipped) Sends the data.voidsendNow(SendPacket packet) Send the packet now, on a separate thread; This is the blocking call.voidsetCompression(boolean compress) Deprecated, for removal: This API element is subject to removal in a future version.voidsetId(int id) Sets the connection ID Note: Be careful with using this methodprotected voidsetMarshallingType(int marshallingType) Sets the type of the marshalling used to send packetsvoidupdate()Process all incoming data, blocking on the input stream until new input is available.
-
Field Details
-
marshallingType
protected int marshallingTypeType of marshalling used to represent sent packets -
marshaller
Marshaller used to send packets
-
-
Constructor Details
-
AbstractConnection
Creates new client (connection from client to server) connection- Parameters:
host- target hostport- target portid- connection ID
-
AbstractConnection
Creates new Server connection- Parameters:
socket- accepted socketid- 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
- 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, 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
Adds a packet to the send queue to be sent on a separate thread. -
sendNow
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
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
- Returns:
- The connection type ("Client" / "Server") that is used in the debug messages and so on.
-
getInputStream
- Returns:
- an input stream
- Throws:
IOException
-
getOutputStream
- Returns:
- an output stream
- Throws:
IOException
-
getSendBufferSize
- Throws:
SocketException
-
getReceiveBufferSize
- 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
Processes a received packet.- Throws:
Exception
-
processPacket
Processes a packet to be sent. -
readNetworkPacket
Reads a complete NetworkPacket. This method must not block, must return null instead.- Returns:
- the NetworkPacket that was sent.
- Throws:
Exception
-
sendNetworkPacket
Sends the data. This must not be blocked for too long- Parameters:
data- data to sendzipped- should the data be compressed- Throws:
Exception- if there's an issue with sending the packet
-
processConnectionEvent
Processes game events occurring on this connection by dispatching them to any registered GameListener objects.- Parameters:
event- the game event.
-