MNet cleanup
This commit is contained in:
@@ -7,8 +7,6 @@ import io.anuke.mindustry.game.EventType.*;
|
||||
import io.anuke.mindustry.net.Net.*;
|
||||
import io.anuke.mindustry.net.Packets.*;
|
||||
import io.anuke.mnet.*;
|
||||
import io.anuke.mnet.MSocket;
|
||||
import io.anuke.mnet.MSocketImpl;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
@@ -21,13 +19,11 @@ public class MClient implements ClientProvider, ApplicationListener{
|
||||
MSocket socket;
|
||||
|
||||
public MClient(){
|
||||
Events.on(AppLoadEvent.class, e -> {
|
||||
Core.app.addListener(this);
|
||||
});
|
||||
Events.on(AppLoadEvent.class, event -> Core.app.addListener(this));
|
||||
}
|
||||
|
||||
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.connectAsync(null, 2000, response -> {
|
||||
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
|
||||
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(){
|
||||
return socket == null ? 0 : (int)socket.getPing();
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import io.anuke.mindustry.game.EventType.*;
|
||||
import io.anuke.mindustry.net.Net.*;
|
||||
import io.anuke.mindustry.net.Packets.*;
|
||||
import io.anuke.mnet.*;
|
||||
import io.anuke.mnet.MServerSocket;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
@@ -18,11 +17,27 @@ public class MServer implements ServerProvider, ApplicationListener{
|
||||
MServerSocket socket;
|
||||
|
||||
public MServer(){
|
||||
Events.on(AppLoadEvent.class, e -> {
|
||||
Core.app.addListener(this);
|
||||
});
|
||||
Events.on(AppLoadEvent.class, event -> 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{
|
||||
socket = new MServerSocket(port, con -> {
|
||||
MSocket sock = con.accept(null);
|
||||
@@ -61,30 +76,17 @@ public class MServer implements ServerProvider, ApplicationListener{
|
||||
connections.clear();
|
||||
}
|
||||
|
||||
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 close(){
|
||||
if(socket != null) socket.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<? extends NetConnection> getConnections(){
|
||||
return connections;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MConnectionImpl getByID(int id){
|
||||
for(MConnectionImpl n : connections){
|
||||
if(n.id == id){
|
||||
|
||||
Reference in New Issue
Block a user