1
0

Add networklistener

This commit is contained in:
Victor 2014-03-16 13:15:28 +02:00
parent 52c5fb6048
commit 18821b065f
4 changed files with 27 additions and 6 deletions

View File

@ -11,8 +11,8 @@ public class GameClient implements Constants {
private final SocketHelper helper; private final SocketHelper helper;
public GameClient(String host) throws IOException { public GameClient(String host, NetworkListener listener) throws IOException {
helper = new SocketHelper(new Socket(host, PORT)); helper = new SocketHelper(new Socket(host, PORT), listener);
} }
public SocketHelper getHelper() { public SocketHelper getHelper() {

View File

@ -12,9 +12,9 @@ public class GameServer implements Constants {
private final ServerSocket serverSocket; private final ServerSocket serverSocket;
private final SocketHelper helper; private final SocketHelper helper;
public GameServer() throws IOException { public GameServer(NetworkListener listener) throws IOException {
serverSocket = new ServerSocket(PORT); serverSocket = new ServerSocket(PORT);
helper = new SocketHelper(serverSocket.accept()); helper = new SocketHelper(serverSocket.accept(), listener);
} }
public SocketHelper getHelper() { public SocketHelper getHelper() {

View File

@ -0,0 +1,13 @@
package com.annimon.turrets;
/**
*
* @author aNNiMON
*/
public interface NetworkListener {
public static final int
ON_CONNECT = 1;
public void onStatusChanged(int status, Object data);
}

View File

@ -9,18 +9,26 @@ import java.net.Socket;
* *
* @author aNNiMON * @author aNNiMON
*/ */
public class SocketHelper { public class SocketHelper extends Thread {
private final Socket socket; private final Socket socket;
private final DataInputStream dis; private final DataInputStream dis;
private final DataOutputStream dos; private final DataOutputStream dos;
public SocketHelper(Socket socket) throws IOException { private final NetworkListener listener;
public SocketHelper(Socket socket, NetworkListener listener) throws IOException {
this.socket = socket; this.socket = socket;
this.listener = listener;
dis = new DataInputStream(socket.getInputStream()); dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream()); dos = new DataOutputStream(socket.getOutputStream());
} }
@Override
public void run() {
listener.onStatusChanged(NetworkListener.ON_CONNECT, null);
}
public void close() throws IOException { public void close() throws IOException {
if (dis != null) dis.close(); if (dis != null) dis.close();
if (dos != null) dos.close(); if (dos != null) dos.close();