|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectmegamek.common.net.AbstractConnection
public abstract class AbstractConnection
Generic bidirectional connection between client and server
| Nested Class Summary | |
|---|---|
protected static interface |
AbstractConnection.INetworkPacket
Connection layer data packet. |
(package private) static class |
AbstractConnection.SendQueue
|
| Field Summary | |
|---|---|
(package private) long |
bytesSent
Bytes send during the connection lifecycle |
(package private) PacketMarshaller |
marshaller
Marshaller used to send packets |
protected int |
marshallingType
Type of marshalling used to represent sent packets |
(package private) boolean |
zipData
Indicates the need to compress sent data |
| Constructor Summary | |
|---|---|
AbstractConnection(java.net.Socket socket,
int id)
Creates new Server connection |
|
AbstractConnection(java.lang.String host,
int port,
int id)
Creates new client (connection from client to server) connection |
|
| Method Summary | |
|---|---|
void |
addConnectionListener(ConnectionListener listener)
Adds the specified connection listener to receive connection events from connection. |
long |
bytesReceived()
Returns a very approximate count of how many bytes were received |
long |
bytesSent()
Returns a very approximate count of how many bytes were sent |
void |
close()
Closes the socket and shuts down the receiver and sender threads |
protected void |
doFlush()
|
void |
flush()
this is the method that should be overridden |
protected java.lang.String |
getConnectionTypeAbbrevation()
Returns the the connection type abbrevation (client/server) that used in the debug messages and so on. |
int |
getId()
Returns the connection ID |
java.lang.String |
getInetAddress()
|
protected java.io.InputStream |
getInputStream()
Returns an input stream |
protected int |
getMarshallingType()
Returns the type of the marshalling used to send packets |
protected java.io.OutputStream |
getOutputStream()
Returns an output stream |
boolean |
hasPending()
Returns true if there are pending packets |
boolean |
isCompressed()
Returns true if this connection compress the sent data |
boolean |
isServer()
Returns true if it's the Server connection |
boolean |
open()
Opens the connection |
protected void |
processConnectionEvent(ConnectionEvent event)
Processes game events occurring on this connection by dispatching them to any registered GameListener objects. |
protected void |
processPacket(AbstractConnection.INetworkPacket np)
process a received packet |
protected void |
processPacket(megamek.common.net.AbstractConnection.SendPacket packet)
process a packet to be sent |
protected abstract AbstractConnection.INetworkPacket |
readNetworkPacket()
Reads a complete NetworkPacket must not block, must return
null instead |
void |
removeConnectionListener(ConnectionListener listener)
Removes the specified connection listener. |
protected void |
reportLastCommands()
Appends the last commands sent/received to the given StringBuffer |
protected void |
reportLastCommands(boolean sent)
Appends the last commands sent or received to the given StringBuffer dependig on the sent parameter |
protected void |
reportReceiveException(java.lang.Exception ex)
Reports receive exception to the System.err |
protected void |
reportReceiveException(java.lang.Exception ex,
java.lang.StringBuffer buffer)
Appends the receive exception report to the given StringBuffer |
protected void |
reportSendException(java.lang.Exception ex,
megamek.common.net.AbstractConnection.SendPacket packet)
Reports receive exception to the System.err |
void |
send(Packet packet)
Adds a packet to the send queue to be send on a seperate thread. |
protected abstract void |
sendNetworkPacket(byte[] data,
boolean zipped)
Sends the data must not block for too long |
void |
sendNow(megamek.common.net.AbstractConnection.SendPacket packet)
Send packet now; This is the blocking call. |
void |
setCompression(boolean compress)
Sets the compression |
void |
setId(int id)
Sets the connection ID |
protected void |
setMarshallingType(int marshallingType)
Sets the type of the marshalling used to send packets |
long |
update()
checks if there is anything to send or receive and sends or receives that stuff. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
long bytesSent
protected int marshallingType
PacketMarshaller marshaller
boolean zipData
| Constructor Detail |
|---|
public AbstractConnection(java.lang.String host,
int port,
int id)
host - target hostport - target portid - connection ID
public AbstractConnection(java.net.Socket socket,
int id)
socket - accepted socketid - connection ID| Method Detail |
|---|
public boolean isServer()
true if it's the Server connection
true if it's the Server connectionprotected int getMarshallingType()
protected void setMarshallingType(int marshallingType)
marshallingType - new marhalling typepublic boolean open()
open in interface IConnectiontrue on success, false otherwisepublic void close()
close in interface IConnectionpublic int getId()
getId in interface IConnectionpublic void setId(int id)
setId in interface IConnectionid - new connection ID Be careful with this...public java.lang.String getInetAddress()
getInetAddress in interface IConnectionpublic boolean isCompressed()
true if this connection compress the sent data
true if this connection compress the sent datapublic void setCompression(boolean compress)
compress - public void send(Packet packet)
send in interface IConnectionpublic void sendNow(megamek.common.net.AbstractConnection.SendPacket packet)
public boolean hasPending()
true if there are pending packets
hasPending in interface IConnectiontrue if there are pending packetspublic long bytesSent()
bytesSent in interface IConnectionpublic long bytesReceived()
bytesReceived in interface IConnectionpublic void addConnectionListener(ConnectionListener listener)
addConnectionListener in interface IConnectionlistener - the connection listener.public void removeConnectionListener(ConnectionListener listener)
removeConnectionListener in interface IConnectionlistener - the connection listener.
protected void reportSendException(java.lang.Exception ex,
megamek.common.net.AbstractConnection.SendPacket packet)
System.err
ex - Exceptionpacket - Packetprotected void reportReceiveException(java.lang.Exception ex)
System.err
ex - Exception
protected void reportReceiveException(java.lang.Exception ex,
java.lang.StringBuffer buffer)
StringBuffer
ex - Exceptionprotected void reportLastCommands()
StringBuffer
buffer - StringBuffer to add the report toprotected void reportLastCommands(boolean sent)
StringBuffer dependig on the sent parameter
buffer - StringBuffer to add the report tosent - indicates which commands (sent/received) should be reportedprotected java.lang.String getConnectionTypeAbbrevation()
protected java.io.InputStream getInputStream()
throws java.io.IOException
java.io.IOException
protected java.io.OutputStream getOutputStream()
throws java.io.IOException
java.io.IOExceptionpublic long update()
update in interface IConnectionpublic void flush()
flush in interface IConnectionprotected void doFlush()
protected void processPacket(AbstractConnection.INetworkPacket np)
throws java.lang.Exception
java.lang.Exception
protected void processPacket(megamek.common.net.AbstractConnection.SendPacket packet)
throws java.lang.Exception
java.lang.Exception
protected abstract AbstractConnection.INetworkPacket readNetworkPacket()
throws java.lang.Exception
NetworkPacket must not block, must return
null instead
NetworkPacket that was sent.
java.lang.Exception
protected abstract void sendNetworkPacket(byte[] data,
boolean zipped)
throws java.lang.Exception
data - data to sendzipped - should the data be compressed
java.lang.Exceptionprotected void processConnectionEvent(ConnectionEvent event)
event - the game event.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||