2013-05-26 2 views
3

MMORPG 용 소켓 서버를 만들고 있습니다. 작성하기 쉽고 가독성을 위해 명령과 처리기를 구성하는 방법을 잘 모르겠습니다.소켓 게임 서버에서 명령/처리기를 구성하는 방법?

나는 ban, kick, identify 등과 같이 데이터 [0]에서 명령을 받게 될 것입니다. HashMap을 사용할 수 있고 인터페이스 명령을 가지고 있으며 각 처리기/명령을 구현하는 명령. 그런 다음 HashMap을 만들고 거기에서부터 시작합니다. 나는 또한 당신이 반성 할 수 있다는 것을 읽었습니다. 경험 많은 프로그래머가하고자하는 일을하고 싶습니다.

클라이언트 클래스 :

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 

public class Client implements Runnable { 

    private Socket socket; 
    private Server server; 
    private boolean isConnected; 
    private BufferedReader in; 
    private PrintWriter out; 

    public Client(Socket socket, Server server) { 
     this.socket = socket; 
     this.server = server; 
     this.isConnected = true; 
     this.in = null; 
     this.out = null; 
    } 

    @Override 
    public void run() { 
     try { 
      in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      out = new PrintWriter(socket.getOutputStream(), true); 

      while (isConnected) { 
       String recv = in.readLine().trim(); 

       if (recv != null) { 
        String[] data = recv.split("%"); 
       } 
      } 
     } catch (IOException e) {} 
    } 

    public synchronized void send(String data) { 
     out.println(data); 
    } 
} 

답변

2

당신이 원하는 것은 Command Pattern을 사용하는 것입니다.

다음은 명령 패턴을 사용하여 클라이언트의 명령을 처리하는 방법입니다.

/* Command Exception. */ 
public class ClientCommandException extends Exception { 
    public ClientCommandException(String msg) { 
     super(msg); 
    } 
} 

/* The ClientCommand interface */ 
public interface ClientCommand { 
    void execute(Client client, String[] params) throws ClientCommandException; 
} 

import java.util.HashMap; 
import java.util.Arrays; 

/* Container for commands */ 
public class Commands implements ClientCommand { 
    private HashMap<String, ClientCommand> cmds; 

    public Commands() { 
     cmds = new HashMap<String, ClientCommand>(); 
    } 

    public void addCommand(ClientCommand cmd, String name) { 
     cmds.put(name, cmd); 
    } 

    public void execute(Client client, String[] params) throws ClientCommandException { 
     ClientCommand cmd = cmds.get(params[0]); 
     if(cmd != null) { 
      cmd.execute(client, Arrays.copyOfRange(params, 1, params.length)); 
     } else { 
      throw new ClientCommandException("Unknown Command: " + params[0]); 
     } 
    } 
} 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 

public class Client implements Runnable { 

    private boolean isConnected; 
    private Commands cmds; 
    private BufferedReader in; 
    private PrintWriter out; 

    private class EchoCommand implements ClientCommand { 
     public void execute(Client client, String[] params) throws ClientCommandException { 
      StringBuilder b = new StringBuilder("Echo back:"); 
      int len = params.length; 
      for(int i = 0; i < len; i++) { 
       b.append(' '); 
       b.append(params[i]); 
      } 
      client.send(b.toString()); 
     } 
    } 

    private class DisconnectCommand implements ClientCommand { 
     public void execute(Client client, String[] params) throws ClientCommandException { 
      client.close(); 
     } 
    } 

    public Client() { 
     cmds = new Commands(); 
     cmds.addCommand(new EchoCommand(), "echo"); 
     cmds.addCommand(new DisconnectCommand(), "disconnect"); 
     /* sub-commands */ 
     Commands server = new Commands(); 
     server.addCommand(new EchoCommand(), "print"); 
     cmds.addCommand(server, "server"); 
     isConnected = true; 
    } 

    public void addCommand(ClientCommand cmd, String name) { 
     cmds.addCommand(cmd, name); 
    } 

    public void close() { 
     isConnected = false; 
    } 

    @Override 
    public void run() { 
     try { 
      in = new BufferedReader(new InputStreamReader(System.in)); 
      out = new PrintWriter(System.out, true); 

      while (isConnected) { 
       String recv = in.readLine().trim(); 

       if (recv != null) { 
        String[] data = recv.split("%"); 
        try { 
         cmds.execute(this, data); 
        } catch(ClientCommandException e) { 
         /* Return some error back to the client. */ 
         out.println(e.toString()); 
        } 
       } 
      } 
     } catch (IOException e) {} 
    } 

    public synchronized void send(String data) { 
     out.println(data); 
    } 

    public static void main(String[] args){ 
     Client client = new Client(); 
     System.out.println("Start Client."); 
     client.run(); 
    } 
} 
관련 문제