/// <summary> /// Reads the payload data. /// </summary> private void ReadPayloadCallback(IAsyncResult result) { int bytesRead = this.connection.Client.EndReceive(result); if (bytesRead == 0 && result.IsCompleted) { // TODO: world server disconnect Game.UI.LogLine("Server has closed the connection"); Game.Exit(); return; } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { // get header and packet, handle it ServerHeader header = (ServerHeader)result.AsyncState; InPacket packet = new InPacket(header, ReceiveData); HandlePacket(packet); // start new asynchronous read Start(); } else { // more payload to read Index += bytesRead; Remaining -= bytesRead; BeginRead(new AsyncCallback(ReadPayloadCallback), result.AsyncState); } }
/// <summary> /// Reads the rest of the incoming header. /// </summary> private void ReadHeaderCallback(IAsyncResult result) { //if (ReceiveData.Length != 4 && ReceiveData.Length != 5) // throw new Exception("ReceiveData.Length not in order"); int bytesRead = this.connection.Client.EndReceive(result); if (bytesRead == 0 && result.IsCompleted) { // TODO: world server disconnect Game.UI.LogLine("Server has closed the connection"); Game.Exit(); return; } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { // finished reading header // the first byte was decrypted already, so skip it AuthenticationCrypto.Decrypt(ReceiveData, 1, ReceiveData.Length - 1); ServerHeader header = new ServerHeader(ReceiveData); Game.UI.LogLine(header.ToString(), LogLevel.Debug); if (header.InputDataLength > 5 || header.InputDataLength < 4) { Game.UI.LogException(String.Format("Header.InputataLength invalid: {0}", header.InputDataLength)); } if (header.Size > 0) { // read the packet payload Index = 0; Remaining = header.Size; ReceiveData = new byte[header.Size]; BeginRead(new AsyncCallback(ReadPayloadCallback), header); } else { // the packet is just a header, start next packet HandlePacket(new InPacket(header)); Start(); } } else { // more header to read Index += bytesRead; Remaining -= bytesRead; BeginRead(new AsyncCallback(ReadHeaderCallback)); } }
/// <summary> /// Reads the rest of the incoming header. /// </summary> private void ReadHeaderCallback(object sender, SocketAsyncEventArgs e) { try { int bytesRead = e.BytesTransferred; if (bytesRead == 0) { // TODO: world server disconnect Exchange.AuthMessage = "Server has closed the connection"; //Game.Reconnect(); return; } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { authenticationCrypto.Decrypt(ReceiveData, 1, ReceiveDataLength - 1); ServerHeader header = new ServerHeader(ReceiveData, ReceiveDataLength); if (header.Size > 0) { Index = 0; Remaining = header.Size; ReserveData(header.Size); ReadAsync(ReadPayloadCallback, header); } else { HandlePacket(new InPacket(header)); Start(); } } else { Index += bytesRead; Remaining -= bytesRead; ReadAsync(ReadHeaderCallback); } } catch (ObjectDisposedException ex) { } catch (NullReferenceException ex) { } catch (SocketException ex) { } }
/// <summary> /// Reads the payload data. /// </summary> private void ReadPayloadCallback(object sender, SocketAsyncEventArgs e) { try { int bytesRead = e.BytesTransferred; if (bytesRead == 0) { // TODO: world server disconnect Game.UI.LogLine("Server has closed the connection"); Game.Reconnect(); return; } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { // get header and packet, handle it ServerHeader header = (ServerHeader)SocketAsyncState; QueuePacket(new InPacket(header, ReceiveData, ReceiveDataLength)); // start new asynchronous read Start(); } else { // more payload to read Index += bytesRead; Remaining -= bytesRead; ReadAsync(ReadPayloadCallback, SocketAsyncState); } } catch (NullReferenceException ex) { Game.UI.LogException(ex); } catch (SocketException ex) { Game.UI.LogException(ex); Game.Reconnect(); return; } }
private void ReadPayloadCallback(object sender, SocketAsyncEventArgs e) { try { int bytesRead = e.BytesTransferred; if (bytesRead == 0) { Exchange.AuthMessage = "Disconncted from server."; //Game.Reconnect(); return; } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { // get header and packet, handle it ServerHeader header = (ServerHeader)SocketAsyncState; HandlePacket(new InPacket(header, ReceiveData, ReceiveDataLength)); // start new asynchronous read Start(); } else { // more payload to read Index += bytesRead; Remaining -= bytesRead; ReadAsync(ReadPayloadCallback, SocketAsyncState); } } catch (NullReferenceException ex) { } catch (SocketException ex) { //Game.Reconnect(); return; } }
/// <summary> /// Reads the rest of the incoming header. /// </summary> private void ReadHeaderCallback(object sender, SocketAsyncEventArgs e) { try { int bytesRead = e.BytesTransferred; if (bytesRead == 0) { // TODO: world server disconnect Game.UI.LogLine("Server has closed the connection"); Game.Reconnect(); return; } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { // finished reading header // the first byte was decrypted already, so skip it authenticationCrypto.Decrypt(ReceiveData, 1, ReceiveDataLength - 1); ServerHeader header = new ServerHeader(ReceiveData, ReceiveDataLength); Game.UI.LogDebug(header.ToString()); if (header.InputDataLength > 5 || header.InputDataLength < 4) { Game.UI.LogException(String.Format("Header.InputDataLength invalid: {0}", header.InputDataLength)); } if (header.Size > 0) { // read the packet payload Index = 0; Remaining = header.Size; ReserveData(header.Size); ReadAsync(ReadPayloadCallback, header); } else { // the packet is just a header, start next packet QueuePacket(new InPacket(header)); Start(); } } else { // more header to read Index += bytesRead; Remaining -= bytesRead; ReadAsync(ReadHeaderCallback); } } // these exceptions can happen as race condition on shutdown catch (ObjectDisposedException ex) { Game.UI.LogException(ex); } catch (NullReferenceException ex) { Game.UI.LogException(ex); } catch (SocketException ex) { Game.UI.LogException(ex); } }
internal InPacket(ServerHeader header) : this(header, new byte[] { }) { }
internal InPacket(ServerHeader header, byte[] buffer) : base(new MemoryStream(buffer)) { Header = header; }
internal InPacket(ServerHeader header, byte[] buffer, int bufferLength) : base(new MemoryStream(buffer, 0, bufferLength, false, false)) { Header = header; }
internal InPacket(ServerHeader header) : this(header, new byte[] { }, 0) { }
/// <summary> /// Reads the rest of the incoming header. /// </summary> private void ReadHeaderCallback(object sender, SocketAsyncEventArgs e) { try { int bytesRead = e.BytesTransferred; if (bytesRead == 0) { // TODO: world server disconnect Game.UI.LogLine("Server has closed the connection"); Game.Reconnect(); return; } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { // finished reading header // the first byte was decrypted already, so skip it authenticationCrypto.Decrypt(ReceiveData, 1, ReceiveDataLength - 1); ServerHeader header = new ServerHeader(ReceiveData, ReceiveDataLength); Game.UI.LogDebug(header.ToString()); if (header.InputDataLength > 5 || header.InputDataLength < 4) Game.UI.LogException(String.Format("Header.InputDataLength invalid: {0}", header.InputDataLength)); if (header.Size > 0) { // read the packet payload Index = 0; Remaining = header.Size; ReserveData(header.Size); ReadAsync(ReadPayloadCallback, header); } else { // the packet is just a header, start next packet QueuePacket(new InPacket(header)); Start(); } } else { // more header to read Index += bytesRead; Remaining -= bytesRead; ReadAsync(ReadHeaderCallback); } } // these exceptions can happen as race condition on shutdown catch (ObjectDisposedException ex) { Game.UI.LogException(ex); } catch (NullReferenceException ex) { Game.UI.LogException(ex); } catch (SocketException ex) { Game.UI.LogException(ex); } }
/// <summary> /// Reads the rest of the incoming header. /// </summary> private void ReadHeaderCallback(IAsyncResult result) { int bytesRead = this.connection.Client.EndReceive(result); if (bytesRead == 0 && result.IsCompleted) { // TODO: world server disconnect Game.Exit(); } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { // finished reading header // the first byte was decrypted already, so skip it AuthenticationCrypto.Decrypt(ReceiveData, 1, ReceiveData.Length - 1); ServerHeader header = new ServerHeader(ReceiveData); Index = 0; Remaining = header.Size; ReceiveData = new byte[header.Size]; BeginRead(ReadPayloadCallback, header); } else { // more header to read Index += bytesRead; Remaining -= bytesRead; BeginRead(ReadHeaderCallback); } }
/// <summary> /// Reads the rest of the incoming header. /// </summary> private void ReadHeaderCallback(IAsyncResult result) { //if (ReceiveData.Length != 4 && ReceiveData.Length != 5) // throw new Exception("ReceiveData.Length not in order"); int bytesRead = this.connection.Client.EndReceive(result); if (bytesRead == 0 && result.IsCompleted) { // TODO: world server disconnect Game.UI.LogLine("Server has closed the connection"); Game.Exit(); return; } Interlocked.Add(ref transferred, bytesRead); Interlocked.Add(ref received, bytesRead); if (bytesRead == Remaining) { // finished reading header // the first byte was decrypted already, so skip it AuthenticationCrypto.Decrypt(ReceiveData, 1, ReceiveData.Length - 1); ServerHeader header = new ServerHeader(ReceiveData); Game.UI.LogLine(header.ToString(), LogLevel.Debug); if (header.InputDataLength > 5 || header.InputDataLength < 4) Game.UI.LogException(String.Format("Header.InputataLength invalid: {0}", header.InputDataLength)); if (header.Size > 0) { // read the packet payload Index = 0; Remaining = header.Size; ReceiveData = new byte[header.Size]; BeginRead(new AsyncCallback(ReadPayloadCallback), header); } else { // the packet is just a header, start next packet HandlePacket(new InPacket(header)); Start(); } } else { // more header to read Index += bytesRead; Remaining -= bytesRead; BeginRead(new AsyncCallback(ReadHeaderCallback)); } }