📄 serverconnection.java
字号:
/*
* ServerConnection.java
*
* Copyright (C) 2000 Jason M. Hanley
* Released under the GNU General Public License (GPL)
* See license.txt for additional information.
*
* Created on July 23, 2000, 12:08 PM
*/
package fate.network;
import java.net.*;
import java.util.*;
import java.io.*;
import fate.util.*;
import fate.messages.*;
/**
* Handles all networking on the server end.
* <br><br>
* Sets up a server socket to listen on a port and spawns {@link ServerThread}
* objects to handle messages.
*
* @author preylude@s3m.com
* @version 0.1.0
*/
public class ServerConnection extends Thread {
public static final int FATE_PORT = 1234;
ServerSocket serverSocket;
int port;
boolean bReadyToRun;
MapPlayerInfo mapPlayerInfo;
HashMap mapServerThreads;
LinkedList messageBuffer;
/** Creates new ServerConnection */
public ServerConnection( int port, MapPlayerInfo mapPlayerInfo ) {
this.port = port;
this.mapPlayerInfo = mapPlayerInfo;
bReadyToRun = false;
mapServerThreads = new HashMap();
messageBuffer = new LinkedList();
}
/** Initializes the server connection */
public boolean initialize() {
try {
serverSocket = new ServerSocket( port );
} catch ( IOException e ) {
Debug.trace( "ERROR: ServerConnection.initialize(): I/O error" );
//@ TODO: Error handling
return false;
}
bReadyToRun = true;
return true;
}
/** Start processing of incoming connections */
public void run() {
// If the caller tries to start() this thread before initializing, bail
if (!bReadyToRun)
return;
while( true ) {
try {
Debug.trace( "Waiting for connection" );
Socket socket = serverSocket.accept();
Debug.trace( "Connection detected from IP " + socket.getInetAddress().getHostAddress() +
" (" + socket.getInetAddress().getHostName() + ")" );
ServerThread thread = new ServerThread( this, socket, mapPlayerInfo );
thread.start();
} catch (IOException e ) {
// Probably not-fatal.. don't do anything for now
//@ TODO: test this
}
}
}
/** Adds a ServerThread to the map */
public synchronized void addServerThread( int id, ServerThread thread ) {
mapServerThreads.put( new Integer( id ), thread );
}
/** Removes a ServerThread from the map */
public synchronized void removeServerThread( int id ) {
mapServerThreads.remove( new Integer( id ) );
}
/** Sends a message to a client.
To look at: could this possibly hold up the server?
Maybe this should spawn a thread to send the message.. */
public boolean sendMessage( int idClient, GameMessage msg ) throws IOException {
ServerThread thread = (ServerThread) mapServerThreads.get( new Integer( idClient ) );
if (thread != null) {
ObjectOutputStream out = thread.getOutputStream();
if (out != null) {
out.writeObject( msg ); //@ this might hold up the server
return true;
} else {
Debug.trace( "ServerConnection:Error: output stream not ready" );
return false;
}
} else {
Debug.trace( "ServerConnection:Error: client ID not found" );
//@ TODO: error handling
}
return false;
}
/** Called by the communications thread to receive a message */
public synchronized void receiveMessage( Object msg ) throws IOException {
Class c = msg.getClass();
String strClass = c.getName();
Debug.trace( "ServerConnection received a message: " + strClass );
messageBuffer.addLast( msg );
}
/** Returns true if there are messages in the message buffer */
public boolean messagesAvailable() {
return ( messageBuffer.size() > 0 );
}
/** Returns the next message in the buffer, or NULL is there are none.
Should check with messagesAvailable first to see if there are any */
public synchronized Object nextMessage() {
return ( messageBuffer.removeFirst() );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -