Add more netcode

This commit is contained in:
Flare Microsystems
2024-11-13 08:49:21 -08:00
parent a2a5322835
commit 16b66b6094
10 changed files with 369 additions and 113 deletions

View File

@@ -1,14 +1,61 @@
package com.flaremicro.crossjeeves;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import com.flaremicro.crossjeeves.net.NetworkHandler;
import com.flaremicro.crossjeeves.net.ServerHandler;
import com.flaremicro.util.Util;
import static com.flaremicro.crossjeeves.net.ErrorCodes.*;
public class CrossJeevesHost {
int port;
boolean running = false;
List<NetworkHandler> connections = Collections.synchronizedList(new ArrayList<NetworkHandler>());
public CrossJeevesHost(int port) {
// TODO Auto-generated constructor stub
this.port = port;
}
public void endConnections()
{
while(connections.size() > 0)
{
connections.remove(0).disconnect(AGENT_TERMINATED.id, "This agent has terminated");
}
}
public void removeConnection(NetworkHandler netHandler)
{
connections.remove(netHandler);
}
public void startHosting() {
// TODO Auto-generated method stub
public void startHosting() throws IOException {
running = true;
ServerSocket server = new ServerSocket(port);
while (running)
{
Socket sock = null;
try
{
sock = server.accept();
//TODO properties!
ServerHandler handler = new ServerHandler(sock, this, new Properties());
connections.add(handler);
}
catch (IOException ex)
{
ex.printStackTrace();
Util.cleanClose(sock);
}
}
}
}