mirror of
https://bitbucket.org/projectswg/lightspeed.git
synced 2026-09-30 16:12:56 -04:00
Completely overhauled how the GUI operates and key parts of the backend
This commit is contained in:
@@ -43,8 +43,10 @@ import java.nio.channels.SocketChannel;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import com.projectswg.common.callback.CallbackManager;
|
||||
import com.projectswg.common.control.Assert;
|
||||
import com.projectswg.common.info.Log;
|
||||
|
||||
|
||||
@@ -52,6 +54,7 @@ public class TCPServer {
|
||||
|
||||
private final CallbackManager<TCPCallback> callbackManager;
|
||||
private final Map<SocketAddress, SocketChannel> sockets;
|
||||
private final AtomicBoolean running;
|
||||
private final InetAddress addr;
|
||||
private final int port;
|
||||
private final int bufferSize;
|
||||
@@ -65,6 +68,7 @@ public class TCPServer {
|
||||
public TCPServer(InetAddress addr, int port, int bufferSize) {
|
||||
this.callbackManager = new CallbackManager<>("tcpserver-"+port, 1);
|
||||
this.sockets = new HashMap<>();
|
||||
this.running = new AtomicBoolean(false);
|
||||
this.addr = addr;
|
||||
this.port = port;
|
||||
this.bufferSize = bufferSize;
|
||||
@@ -72,7 +76,15 @@ public class TCPServer {
|
||||
listener = new TCPListener();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return channel.socket().getLocalPort();
|
||||
}
|
||||
|
||||
public void bind() throws IOException {
|
||||
if (running.getAndSet(true)) {
|
||||
Assert.fail();
|
||||
return;
|
||||
}
|
||||
callbackManager.start();
|
||||
channel = ServerSocketChannel.open();
|
||||
channel.socket().bind(new InetSocketAddress(addr, port));
|
||||
@@ -85,6 +97,7 @@ public class TCPServer {
|
||||
}
|
||||
|
||||
public SocketChannel connectTo(InetSocketAddress sock) throws IOException {
|
||||
Assert.notNull(sock);
|
||||
synchronized (sockets) {
|
||||
SocketChannel sc = sockets.get(sock);
|
||||
if (sc == null || (!sc.isConnected() && !sc.isConnectionPending())) {
|
||||
@@ -97,10 +110,10 @@ public class TCPServer {
|
||||
}
|
||||
|
||||
public boolean disconnect(SocketAddress sock) {
|
||||
Assert.notNull(sock);
|
||||
synchronized (sockets) {
|
||||
SocketChannel sc = sockets.get(sock);
|
||||
if (sc == null)
|
||||
return false;
|
||||
Assert.notNull(sc);
|
||||
sockets.remove(sock);
|
||||
try {
|
||||
Socket s = sc.socket();
|
||||
@@ -123,11 +136,14 @@ public class TCPServer {
|
||||
}
|
||||
|
||||
public boolean close() {
|
||||
if (!running.getAndSet(false)) {
|
||||
Assert.fail();
|
||||
return false;
|
||||
}
|
||||
callbackManager.stop();
|
||||
listener.stop();
|
||||
try {
|
||||
if (channel != null)
|
||||
channel.close();
|
||||
channel.close();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -138,6 +154,9 @@ public class TCPServer {
|
||||
public boolean send(InetSocketAddress sock, ByteBuffer data) {
|
||||
synchronized (sockets) {
|
||||
SocketChannel sc = sockets.get(sock);
|
||||
Assert.notNull(sc);
|
||||
Assert.test(sc.isConnected());
|
||||
Assert.test(data.hasRemaining());
|
||||
try {
|
||||
if (sc != null && sc.isConnected()) {
|
||||
while (data.hasRemaining())
|
||||
|
||||
Reference in New Issue
Block a user