Ejemplo n.º 1
0
        public virtual void EndWrite()
        {
#if AGNOS_TRANSPORT_DEBUG
            System.Console.WriteLine("Transport.EndWrite");
#endif
            AssertBeganWrite();
            if (wbuffer.Length > 0)
            {
                writeSInt32(outStream, wseq);

                if (compressionThreshold > 0 && wbuffer.Length >= compressionThreshold)
                {
                    compressionBuffer.Position = 0;
                    compressionBuffer.SetLength(0);
                    using (DeflateStream dfl = new DeflateStream(compressionBuffer, CompressionMode.Compress, true)) {
                        wbuffer.WriteTo(dfl);
                    }
#if AGNOS_TRANSPORT_DEBUG
                    //System.Console.WriteLine("Transport.EndWrite seq={0}, len={1}, uncomp={2}", wseq, compressionBuffer.Length, wbuffer.Length);
                    System.Console.WriteLine(">> " + repr(wbuffer.ToArray()));
#endif
                    writeSInt32(outStream, (int)compressionBuffer.Length);            // packet length
                    writeSInt32(outStream, (int)wbuffer.Length);                      // uncompressed length
                    compressionBuffer.WriteTo(outStream);
                }
                else
                {
#if AGNOS_TRANSPORT_DEBUG
                    System.Console.WriteLine(">> seq={0}, len={1}, uncomp=0", wseq, wbuffer.Length);
                    System.Console.WriteLine(">> " + repr(wbuffer.ToArray()));
#endif
                    writeSInt32(outStream, (int)wbuffer.Length);    // packet length
                    writeSInt32(outStream, 0);                      // 0 means no compression
                    wbuffer.WriteTo(outStream);
                }

                outStream.Flush();
            }

            wbuffer.Position = 0;
            wbuffer.SetLength(0);
            wlock.Release();
        }
Ejemplo n.º 2
0
        //
        // read interface
        //
        public virtual int BeginRead()
        {
            if (rlock.IsHeldByCurrentThread())
            {
                throw new IOException("BeginRead is not reentrant");
            }

#if AGNOS_TRANSPORT_DEBUG
            System.Console.WriteLine("TransportBegin.BeginRead");
#endif
            rlock.Acquire();

            try {
                int seq                = readSInt32(inStream);
                int packetLength       = readSInt32(inStream);
                int uncompressedLength = readSInt32(inStream);

#if AGNOS_TRANSPORT_DEBUG
                System.Console.WriteLine(">> seq={0}, len={1}, uncomp={2}", seq, packetLength, uncompressedLength);
#endif
                if (readStream != null)
                {
                    throw new InvalidOperationException("readStream must be null at this point");
                }

                readStream = new BoundInputStream(inStream, packetLength, true, false);
                if (uncompressedLength > 0)
                {
                    readStream = new BoundInputStream(new DeflateStream(readStream, CompressionMode.Decompress, false), packetLength, false, true);
                }
                return(seq);
            } catch (Exception) {
                readStream = null;
                rlock.Release();
                throw;
            }
        }