⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverthread.java

📁 Massively Multiplayer Space Trading and Combat game. This is an online strategy game, not a 3D space
💻 JAVA
字号:
/*
 * ServerThread.java
 *
 * Copyright (C) 2000 Jason M. Hanley
 * Released under the GNU General Public License (GPL)
 * See license.txt for additional information.
 *
 * Created on July 24, 2000, 9:38 PM
 */
 
package fate.network;

import java.io.*;
import java.net.*;
import java.util.*;

import fate.util.*;
import fate.messages.*;
import fate.*;

/** 
 * Handles verification and message processing of individual clients.
 *
 * @author  preylude@s3m.com
 * @version 0.1.0
 */
public class ServerThread extends Thread {
  
  ServerConnection parent;
  Socket socket;
  PlayerInfo playerInfo;
  ObjectInputStream in;
  ObjectOutputStream out;
  MapPlayerInfo mapPlayerInfo;

  /** Creates new ServerThread */
  public ServerThread( ServerConnection parent, Socket socket,
      MapPlayerInfo mapPlayerInfo )
      throws IOException {
    this.parent = parent;
    this.socket = socket;
    this.mapPlayerInfo = mapPlayerInfo;
  }
  
  /** Returns the socket associated with this ServerThread */
  public ObjectOutputStream getOutputStream() {
    return out;
  }
  
  /** Continues reading messages until an IO expection happens */
  public void run() {
    try {
      Debug.trace( "ServerThread creating input stream" );
      in = new ObjectInputStream( socket.getInputStream() );
      Debug.trace( "ServerThread reading LoginMessage" );
      LoginMessage login = (LoginMessage) in.readObject();
      Debug.trace( "ServerThread creating output stream" );
      out = new ObjectOutputStream( socket.getOutputStream() );
      
      // If this is a new player, send back the PlayerInfo
      if ( login.username.equals( "(newuser)" ) ) {
        playerInfo = new PlayerInfo( "", "" );
        int newID = mapPlayerInfo.put( playerInfo );
        Debug.trace( "ServerConnection making new user: " + Integer.toString( newID ) );
        playerInfo.username = "user" + Integer.toString( newID );
        playerInfo.password = "pass" + Integer.toString( newID );
        playerInfo.idPlayer = FateServer.universe.createNewPlayer( newID );
        login = new LoginMessage( playerInfo.username, playerInfo.password, false );
        out.writeObject( login );
        socket.close();
        return;
      }
      
      // If the correct password is supplied
      playerInfo = (PlayerInfo)mapPlayerInfo.find( login.username );
      if ( playerInfo != null && playerInfo.password.equals( login.password ) ) {
        Debug.trace( "Password verified" );
        login = new LoginMessage( "", "", true );
        Debug.trace( "ServerConnection sending response" );
        out.writeObject( login );
        parent.addServerThread( playerInfo.id, this );
        playerInfo.state.bConnected = true;
      } else {
        Debug.trace( "Incorrect password" );
        socket.close();
        return;
      }
      
      // Main message receiving loop
      Debug.trace( "ServerThread entering message loop" );
      while (true) {
        GameMessage gameMessage = (GameMessage) in.readObject();
        ServerMessage serverMessage = new ServerMessage( 
          playerInfo.id, gameMessage );        
        parent.receiveMessage( serverMessage );
      }
    } catch ( IOException e ) {
      // IO Exception.. gotta bail
      Debug.trace( "ServerThread.run(): IOException - Thread stopping");
      parent.removeServerThread( playerInfo.id );
      playerInfo.state.reset();
      //@ TODO: error handling
    } catch ( ClassNotFoundException e ) {
      // Class not found.. this is bad too
      Debug.trace( "ServerThread.run(): ClassNotFoundException - Thread stopping");
      parent.removeServerThread( playerInfo.id );
      playerInfo.state.reset();
      //@ TODO: error handling
    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -