com.realtime.crossfire.jxclient.server.server
Class DefaultServerConnection

java.lang.Object
  extended by com.realtime.crossfire.jxclient.server.server.DefaultServerConnection
All Implemented Interfaces:
ServerConnection
Direct Known Subclasses:
DefaultCrossfireServerConnection

public abstract class DefaultServerConnection
extends java.lang.Object
implements ServerConnection

One of the two most important classes, ServerConnection performs most of the network-related work. It either decodes commands sent by the server itself, or delegates their processing to other classes, like Map or Faces.


Field Summary
private  ClientSocket clientSocket
          The ClientSocket instance used to connect to Crossfire servers.
 
Constructor Summary
protected DefaultServerConnection(DebugWriter debugProtocol)
          Creates a new instance.
 
Method Summary
 void addClientSocketListener(ClientSocketListener clientSocketListener)
          Adds a ClientSocketListener to notify.
 void connect(java.lang.String hostname, int port)
          Attempts to connect the client to a server.
 void disconnect(java.lang.String reason)
          Disconnects from the server.
 void removeClientSocketListener(ClientSocketListener clientSocketListener)
          Removes a ClientSocketListener to notify.
 void start()
          Starts operation.
 void stop()
          Stops operation.
protected  void writePacket(byte[] packet, int length)
          Writes a Crossfire Message on the socket, so it is sent to the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clientSocket

@NotNull
private final ClientSocket clientSocket
The ClientSocket instance used to connect to Crossfire servers.

Constructor Detail

DefaultServerConnection

protected DefaultServerConnection(@Nullable
                                  DebugWriter debugProtocol)
                           throws java.io.IOException
Creates a new instance.

Parameters:
debugProtocol - tf non-null, write all protocol commands to this writer
Throws:
java.io.IOException - if an internal error occurs
Method Detail

addClientSocketListener

public void addClientSocketListener(@NotNull
                                    ClientSocketListener clientSocketListener)
Adds a ClientSocketListener to notify.

Specified by:
addClientSocketListener in interface ServerConnection
Parameters:
clientSocketListener - the client socket listener to add

connect

public void connect(@NotNull
                    java.lang.String hostname,
                    int port)
Attempts to connect the client to a server.

Specified by:
connect in interface ServerConnection
Parameters:
hostname - the hostname to connect to
port - the port to connect to

disconnect

public void disconnect(@NotNull
                       java.lang.String reason)
Disconnects from the server. Does nothing if not connected.

Specified by:
disconnect in interface ServerConnection
Parameters:
reason - the reason for the disconnect

removeClientSocketListener

public void removeClientSocketListener(@NotNull
                                       ClientSocketListener clientSocketListener)
Removes a ClientSocketListener to notify.

Specified by:
removeClientSocketListener in interface ServerConnection
Parameters:
clientSocketListener - the client socket listener to remove

start

public void start()
Starts operation.


stop

public void stop()
          throws java.lang.InterruptedException
Stops operation.

Throws:
java.lang.InterruptedException - if stopping was interrupted

writePacket

protected void writePacket(@NotNull
                           byte[] packet,
                           int length)
Writes a Crossfire Message on the socket, so it is sent to the server.

Parameters:
packet - the packet to be sent; it does not include the length bytes but only actual payload data
length - the length of packet; if the array is larger, excess data is ignored