Move Connect to socket to another method in Player class.

This commit is contained in:
UnknownShadow200 2017-10-22 22:50:16 +11:00
parent 0aeaca75fb
commit e7d57713ec
4 changed files with 20 additions and 14 deletions

View File

@ -29,6 +29,10 @@ namespace MCGalaxy.Network {
/// <summary> Sets whether this socket operates in low-latency mode (e.g. for TCP, disabes nagle's algorithm). </summary> /// <summary> Sets whether this socket operates in low-latency mode (e.g. for TCP, disabes nagle's algorithm). </summary>
bool LowLatency { set; } bool LowLatency { set; }
/// <summary> Registers receive/send callbacks handlers. </summary>
/// <remarks> Separate, to ensure data is only received/sent with a fully constructed object. </remarks>
void RegisterCallbacks();
/// <summary> Receives next block of received data, asynchronously. </summary> /// <summary> Receives next block of received data, asynchronously. </summary>
void ReceiveNextAsync(); void ReceiveNextAsync();

View File

@ -55,7 +55,8 @@ namespace MCGalaxy.Network {
bool accepted = false; bool accepted = false;
try { try {
p = new Player(listen.socket.EndAccept(result)); p = new Player();
p.Connect(listen.socket.EndAccept(result));
listen.AcceptNextAsync(); listen.AcceptNextAsync();
accepted = true; accepted = true;
} catch (Exception ex) { } catch (Exception ex) {

View File

@ -45,7 +45,7 @@ namespace MCGalaxy.Network {
public void RegisterCallbacks() { public void RegisterCallbacks() {
recvArgs.Completed += recvCallback; recvArgs.Completed += recvCallback;
sendArgs.Completed += sendCallback; sendArgs.Completed += sendCallback;
} }
public string RemoteIP { public string RemoteIP {

View File

@ -48,25 +48,26 @@ namespace MCGalaxy {
SuperUser = true; SuperUser = true;
} }
public Player(Socket s) { internal Player() {
spamChecker = new SpamChecker(this); 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 { try {
TcpSocket tcp = new TcpSocket(this, s); Socket = new TcpSocket(this, s);
Socket = tcp;
tcp.RegisterCallbacks();
ip = Socket.RemoteIP; ip = Socket.RemoteIP;
SessionID = Interlocked.Increment(ref sessionCounter) & SessionIDMask; Socket.RegisterCallbacks();
Logger.Log(LogType.UserActivity, ip + " connected to the server."); 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(); Socket.ReceiveNextAsync();
connections.Add(this); 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; } } public override byte EntityID { get { return id; } }