📄 clientconnection.java
字号:
/*
* ClientConnection.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.util.*;
import java.io.*;
import java.net.*;
import fate.messages.*;
import fate.util.*;
/**
* Attempts to create a network connection between client and server.
* <br><br>
* After calling {@link connect(LoginMessage)}, on sucessful connection with
* the server, this will spawn a new ClientThread to handle incoming messages.
* Messages will be stored in the {@link messageBuffer} and can be retrieved
* sequentially using {@link messagesAvailable()} and {@link nextMessage()}.
*
* @author preylude@s3m.com
* @version 0.1.0
*/
public class ClientConnection extends Object {
LinkedList messageBuffer;
Socket socket;
ObjectInputStream in;
ObjectOutputStream out;
String host;
int port;
/** Creates new ClientConnection */
public ClientConnection( String host, int port ) {
this.host = host;
this.port = port;
messageBuffer = new LinkedList();
}
/** Opens a connection to the server */
public boolean connect( LoginMessage loginMessage )
throws UnknownHostException, IOException {
// Open up the connection to the server using TCP/IP
Debug.trace( "ClientConnection opening socket" );
socket = new Socket( host, port );
Debug.trace( "ClientConnection creating output stream" );
out = new ObjectOutputStream( socket.getOutputStream() );
// Send the username and password to the server
Debug.trace( "ClientConnection sending username/password" );
out.writeObject( loginMessage );
Debug.trace( "ClientConnection creating input stream" );
in = new ObjectInputStream( socket.getInputStream() );
LoginMessage loginReturned;
try {
Debug.trace( "ClientConnection waiting for server response" );
loginReturned = (LoginMessage) in.readObject();
} catch (ClassNotFoundException e) {
// This is not good
Debug.trace( "ERROR: ClientConnection.connect(): Class not found" );
//@ TODO: error handling
return false;
}
// If this was a new user request
if (loginMessage.username.equals( "(newuser)" ) ) {
loginMessage.username = loginReturned.username;
loginMessage.password = loginReturned.password;
return true;
}
if (loginReturned.bAccepted)
{
Debug.trace( "ClientConnection: Password was accepted" );
// We're authenticated, so spawn off a client thread to start giving messages
ClientThread thread = new ClientThread( this, in );
thread.start();
return true;
}
else
{
Debug.trace( "ClientConnection: Invalid password" );
// No connection established -- bad username or password
return false;
}
}
/** Sends a message to the server */
public void sendMessage( GameMessage msg ) {
try {
out.writeObject( msg );
} catch( IOException e ) {
Debug.trace( "ClientConnection.sendMessage: Exception" );
e.printStackTrace();
}
}
/** 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( "ClientConnection 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 + -