MNet cleanup

This commit is contained in:
Anuken 2019-08-23 15:04:31 -04:00
parent de031b1ddb
commit 5f9000db92
3 changed files with 45 additions and 39 deletions

View File

@ -7,8 +7,6 @@ import io.anuke.mindustry.game.EventType.*;
import io.anuke.mindustry.net.Net.*; import io.anuke.mindustry.net.Net.*;
import io.anuke.mindustry.net.Packets.*; import io.anuke.mindustry.net.Packets.*;
import io.anuke.mnet.*; import io.anuke.mnet.*;
import io.anuke.mnet.MSocket;
import io.anuke.mnet.MSocketImpl;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
@ -21,13 +19,11 @@ public class MClient implements ClientProvider, ApplicationListener{
MSocket socket; MSocket socket;
public MClient(){ public MClient(){
Events.on(AppLoadEvent.class, e -> { Events.on(AppLoadEvent.class, event -> Core.app.addListener(this));
Core.app.addListener(this);
});
} }
public void connect(String ip, int port, Runnable success) throws IOException{ public void connect(String ip, int port, Runnable success) throws IOException{
socket = new MSocketImpl(InetAddress.getByName(ip), port, new PacketSerializer()); socket = new MSocket(InetAddress.getByName(ip), port, new PacketSerializer());
socket.addDcListener((sock, reason) -> Core.app.post(() -> Net.handleClientReceived(new Disconnect()))); socket.addDcListener((sock, reason) -> Core.app.post(() -> Net.handleClientReceived(new Disconnect())));
socket.connectAsync(null, 2000, response -> { socket.connectAsync(null, 2000, response -> {
if(response.getType() == ResponseType.ACCEPTED){ if(response.getType() == ResponseType.ACCEPTED){
@ -43,6 +39,20 @@ public class MClient implements ClientProvider, ApplicationListener{
}); });
} }
@Override
public void update(){
if(socket == null) return;
socket.update((sock, object) -> Core.app.post(() -> {
try{
Net.handleClientReceived(object);
}catch(Exception e){
Net.showError(e);
netClient.disconnectQuietly();
}
}));
}
@Override @Override
public void updatePing(){ public void updatePing(){
@ -61,19 +71,6 @@ public class MClient implements ClientProvider, ApplicationListener{
} }
} }
public void update(){
if(socket == null) return;
socket.update((sock, object) -> Core.app.post(() -> {
try{
Net.handleClientReceived(object);
}catch(Exception e){
Net.showError(e);
netClient.disconnectQuietly();
}
}));
}
public int getPing(){ public int getPing(){
return socket == null ? 0 : (int)socket.getPing(); return socket == null ? 0 : (int)socket.getPing();
} }

View File

@ -6,7 +6,6 @@ import io.anuke.mindustry.game.EventType.*;
import io.anuke.mindustry.net.Net.*; import io.anuke.mindustry.net.Net.*;
import io.anuke.mindustry.net.Packets.*; import io.anuke.mindustry.net.Packets.*;
import io.anuke.mnet.*; import io.anuke.mnet.*;
import io.anuke.mnet.MServerSocket;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
@ -18,11 +17,27 @@ public class MServer implements ServerProvider, ApplicationListener{
MServerSocket socket; MServerSocket socket;
public MServer(){ public MServer(){
Events.on(AppLoadEvent.class, e -> { Events.on(AppLoadEvent.class, event -> Core.app.addListener(this));
Core.app.addListener(this);
});
} }
@Override
public void update(){
if(socket == null) return;
socket.update();
for(MSocket socket : socket.getSockets()){
MConnectionImpl c = socket.getUserData();
socket.update((s, msg) -> Core.app.post(() -> {
try{
Net.handleServerReceived(c.id, msg);
}catch(Exception e){
e.printStackTrace();
}
}));
}
}
@Override
public void host(int port) throws IOException{ public void host(int port) throws IOException{
socket = new MServerSocket(port, con -> { socket = new MServerSocket(port, con -> {
MSocket sock = con.accept(null); MSocket sock = con.accept(null);
@ -61,30 +76,17 @@ public class MServer implements ServerProvider, ApplicationListener{
connections.clear(); connections.clear();
} }
public void update(){ @Override
if(socket == null) return;
socket.update();
for(MSocket socket : socket.getSockets()){
MConnectionImpl c = socket.getUserData();
socket.update((s, msg) -> Core.app.post(() -> {
try{
Net.handleServerReceived(c.id, msg);
}catch(Exception e){
e.printStackTrace();
}
}));
}
}
public void close(){ public void close(){
if(socket != null) socket.close(); if(socket != null) socket.close();
} }
@Override
public Iterable<? extends NetConnection> getConnections(){ public Iterable<? extends NetConnection> getConnections(){
return connections; return connections;
} }
@Override
public MConnectionImpl getByID(int id){ public MConnectionImpl getByID(int id){
for(MConnectionImpl n : connections){ for(MConnectionImpl n : connections){
if(n.id == id){ if(n.id == id){

View File

@ -643,6 +643,13 @@ public class ServerControl implements ApplicationListener{
info("Nobody with that name could be found."); info("Nobody with that name could be found.");
} }
}); });
handler.register("gc", "Trigger a grabage collection. Testing onlu.", arg -> {
int pre = (int)(Core.app.getJavaHeap() / 1024 / 1024);
System.gc();
int post = (int)(Core.app.getJavaHeap() / 1024 / 1024);
info("&ly{0}&lg MB collected. Memory usage now at &ly{1}&lg MB.", pre - post, post);
});
} }
private void readCommands(){ private void readCommands(){