diff --git a/MCGalaxy/Network/Socket/Interfaces.cs b/MCGalaxy/Network/Socket/Interfaces.cs index 0f42c7244..9fd84692c 100644 --- a/MCGalaxy/Network/Socket/Interfaces.cs +++ b/MCGalaxy/Network/Socket/Interfaces.cs @@ -29,6 +29,10 @@ namespace MCGalaxy.Network { /// Sets whether this socket operates in low-latency mode (e.g. for TCP, disabes nagle's algorithm). bool LowLatency { set; } + /// Registers receive/send callbacks handlers. + /// Separate, to ensure data is only received/sent with a fully constructed object. + void RegisterCallbacks(); + /// Receives next block of received data, asynchronously. void ReceiveNextAsync(); diff --git a/MCGalaxy/Network/Socket/TcpListen.cs b/MCGalaxy/Network/Socket/TcpListen.cs index d5b519c67..f83819bc7 100644 --- a/MCGalaxy/Network/Socket/TcpListen.cs +++ b/MCGalaxy/Network/Socket/TcpListen.cs @@ -55,7 +55,8 @@ namespace MCGalaxy.Network { bool accepted = false; try { - p = new Player(listen.socket.EndAccept(result)); + p = new Player(); + p.Connect(listen.socket.EndAccept(result)); listen.AcceptNextAsync(); accepted = true; } catch (Exception ex) { diff --git a/MCGalaxy/Network/Socket/TcpSocket.cs b/MCGalaxy/Network/Socket/TcpSocket.cs index f3830ed32..ff1068a69 100644 --- a/MCGalaxy/Network/Socket/TcpSocket.cs +++ b/MCGalaxy/Network/Socket/TcpSocket.cs @@ -45,7 +45,7 @@ namespace MCGalaxy.Network { public void RegisterCallbacks() { recvArgs.Completed += recvCallback; - sendArgs.Completed += sendCallback; + sendArgs.Completed += sendCallback; } public string RemoteIP { diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index 4c06b85a3..2ae63839c 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -48,25 +48,26 @@ namespace MCGalaxy { SuperUser = true; } - public Player(Socket s) { + internal Player() { spamChecker = new SpamChecker(this); + SessionID = Interlocked.Increment(ref sessionCounter) & SessionIDMask; + for (int i = 0; i < BlockBindings.Length; i++) { + BlockBindings[i] = ExtBlock.FromRaw((byte)i); + } + } + + internal void Connect(Socket s) { try { - TcpSocket tcp = new TcpSocket(this, s); - Socket = tcp; - tcp.RegisterCallbacks(); - + Socket = new TcpSocket(this, s); ip = Socket.RemoteIP; - SessionID = Interlocked.Increment(ref sessionCounter) & SessionIDMask; + Socket.RegisterCallbacks(); + Logger.Log(LogType.UserActivity, ip + " connected to the server."); - - for (int i = 0; i < BlockBindings.Length; i++) { - BlockBindings[i] = ExtBlock.FromRaw((byte)i); - } - Socket.ReceiveNextAsync(); connections.Add(this); + } catch ( Exception e ) { + Leave("Login failed!"); Logger.LogError(e); } - catch ( Exception e ) { Leave("Login failed!"); Logger.LogError(e); } } public override byte EntityID { get { return id; } }