// a bit dirty. only used by WorldClientCollection public void Send(SegmentStream stream) { if (Socket == null || !Connected) { stream.Dispose(); return; } var args = ObjectPoolMgr.ObtainObject <PoolableSocketArgs>(); try { args.Completed += OnSendCompleted; args.SetBuffer(stream.Segment.Buffer.Array, stream.Segment.Offset, (int)(stream.Position)); args.UserToken = stream; if (!Socket.SendAsync(args)) { args.Completed -= OnSendCompleted; stream.Dispose(); ObjectPoolMgr.ReleaseObject(args); } } catch { args.Dispose(); stream.Dispose(); // args could be disposed if an error occured throw; } }
public void Send(SegmentStream stream) { if (this.Socket != null && this.Connected) { SocketAsyncEventArgs socketAsyncEventArgs = ObjectPoolMgr.ObtainObject <SocketAsyncEventArgs>(); socketAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(this.OnSendCompleted); socketAsyncEventArgs.SetBuffer(stream.Segment.Buffer.Array, stream.Segment.Offset, (int)stream.Position); socketAsyncEventArgs.UserToken = stream; if (!this.Socket.SendAsync(socketAsyncEventArgs)) { socketAsyncEventArgs.Completed -= new EventHandler <SocketAsyncEventArgs>(this.OnSendCompleted); stream.Dispose(); ObjectPoolMgr.ReleaseObject <SocketAsyncEventArgs>(socketAsyncEventArgs); } this.LastActivity = DateTime.Now; } }
/// <summary> /// Copy the contents of a stored file into an opened stream /// </summary> /// <param name="entry">Entry information of file to extract</param> /// <param name="_stream">Stream to store the uncompressed data</param> /// <returns>True if success, false if not.</returns> /// <remarks>Unique compression methods are Store and Deflate</remarks> public bool ExtractFile(ZipEntry entry, Stream stream) { if (!stream.CanWrite) { throw new InvalidOperationException("Stream cannot be written"); } // check signature byte[] signature = new byte[4]; this.zipFileStream.Seek(entry.HeaderOffset, SeekOrigin.Begin); this.zipFileStream.Read(signature, 0, 4); if (BitConverter.ToUInt32(signature, 0) != 0x04034b50) { return(false); } // Prepare streams stream.SetLength(entry.FileSize); var outStream = new CrcStream(stream); this.zipFileStream.Seek(entry.FileOffset, SeekOrigin.Begin); // Select input stream for inflating or just reading Stream inStream = new SegmentStream(this.zipFileStream, entry.CompressedSize); if (entry.Method == Compression.Deflate) { using (var intPutStream = new Ionic.Zlib.DeflateStream(inStream, Ionic.Zlib.CompressionMode.Decompress)) { intPutStream.FlushMode = FlushType.Full; int buffSize = 4096; byte[] buff = new byte[buffSize]; int size = 0; do { size = intPutStream.Read(buff, 0, buffSize); if (size > 0) { outStream.Write(buff, 0, size); } } while (size > 0); } //inStream = new DeflateStream(inStream, CompressionMode.Decompress, true); } // Decompress if (entry.Method == Compression.LZMA) { var decoder = new SevenZip.Compression.LZMA.Decoder(); const int PropsLength = 5; var buffer = new byte[PropsLength]; inStream.Read(buffer, 0, sizeof(int)); if (BitConverter.ToInt32(buffer, 0) != 0x00051409) { throw new Exception("Invalid LZMA stream signature"); } if (inStream.Read(buffer, 0, PropsLength) != PropsLength) { throw new Exception("Invalid LZMA properties length"); } decoder.SetDecoderProperties(buffer); decoder.Code(inStream, outStream, entry.CompressedSize, entry.FileSize, null); } else { inStream.CopyTo(outStream); } stream.Flush(); //if (entry.Method == Compression.Deflate) inStream.Dispose(); if (entry.Crc32 != outStream.WriteCrc) { throw new Exception("Uncompressed file CRC mismatch"); } return(true); }