public OutPacket(InPacket inPacket) { m_stream = new MemoryStream(DefaultBufferSize); m_disposed = false; WriteUShort(inPacket.OperationCode); WriteBytes(inPacket.ReadLeftoverBytes()); }
public override void Dispatch(InPacket inPacket) { using (OutPacket outPacket = new OutPacket(inPacket)) { if (ServerSession.Instance != null) { ServerSession.Instance.Send(outPacket.ToArray()); } } }
public abstract void Dispatch(InPacket inPacket);
/// <summary> /// Used as IAsyncResult parser for ContinueReading(). /// </summary> /// <param name="pIAR">The result AsyncCallback makes</param> private void EndReading(IAsyncResult pIAR) { int amountReceived = 0; try { amountReceived = _socket.EndReceive(pIAR); } catch (Exception ex) { //Console.WriteLine(TypeName + " : " + ex.ToString()); amountReceived = 0; } if (amountReceived == 0) { // We got a disWvsBeta.Common.Sessions here! OnDisconnectINTERNAL(); return; } // Add amount of bytes received to _bufferpos so we know if we got everything. _bufferpos += amountReceived; // Check if we got all data. There is _no_ way we would have received more bytes than needed. Period. if (_bufferpos == _bufferlen) { // It seems we have all data we need // Now check if we got a header if (_header) { if (!_encryption && _receivingFromServer) { // Unencrypted Packet have a short header with plain length. ushort length = (ushort)(_buffer[0] | _buffer[1] << 8); StartReading(length); } else { int length = GetHeaderLength(_buffer); StartReading(length); } } else { InPacket packet; if (_encryption) { _buffer = Decrypt(_buffer); packet = new InPacket(_buffer); Dispatch(packet); } else { _encryption = true; // First packet received or sent is unencrypted. All others are. packet = new InPacket(_buffer, false); this.ServerInfo = new ServerInfo(); this.ServerInfo.Version = packet.ReadUShort(); this.ServerInfo.Subversion = packet.ReadString(); _encryptIV = packet.ReadBytes(4); _decryptIV = packet.ReadBytes(4); this.ServerInfo.Localisation = (Localisation)packet.ReadByte(); Session.AssignUserKey(this.ServerInfo); this.OnHandshake(this.ServerInfo); } StartReading(4, true); } } else { ContinueReading(); } }