예제 #1
0
파일: LogWriter.cs 프로젝트: aooshi/adf
        /// <summary>
        /// 刷新缓冲区
        /// </summary>
        public void Flush()
        {
            byte[] buffer = null;
            int    size   = 0;

            //
            lock (this.cacheStream)
            {
                size = (int)this.cacheStream.Position;
                if (size > 0)
                {
                    //reset position to read
                    this.cacheStream.Position = 0;
                    //read data
                    buffer = StreamHelper.Receive(this.cacheStream, (int)size);
                    //reset position to next write
                    this.cacheStream.Position = 0;
                }

                //reset flush
                this.flushing = false;
            }

            //
            if (size > 0)
            {
                this.Flush(buffer);
            }
        }
예제 #2
0
        private void CloseChunkGzip()
        {
            var crlfBuffer = this.Encoding.GetBytes("\r\n");
            var buffers    = new ArraySegment <byte> [4];

            //
            //gzip
            this.chunkGzipStream.Close();
            var contentLength = (int)this.chunkStream.Length;

            this.chunkStream.Position = 0;
            contentBuffer             = StreamHelper.Receive(this.chunkStream, contentLength);
            this.chunkStream.SetLength(0);
            //len
            var lenBuffer = this.Encoding.GetBytes(contentLength.ToString("x"));

            buffers[0] = new ArraySegment <byte>(lenBuffer);
            buffers[1] = new ArraySegment <byte>(crlfBuffer);
            //content
            buffers[2] = new ArraySegment <byte>(contentBuffer);
            buffers[3] = new ArraySegment <byte>(crlfBuffer);
            //
            try
            {
                this.socket.Send(buffers);
            }
            catch
            {
                SocketHelper.TryClose(this.socket);
                StreamHelper.TryClose(this.chunkGzipStream);
                StreamHelper.TryClose(this.chunkStream);
                this.chunkWriteEvent.Set();
                throw;
            }
        }
예제 #3
0
        /// <summary>
        /// 初始化一个NetworkStream数据帧
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="headerBuffer"></param>
        public WebSocketDataFrame(NetworkStream stream, byte[] headerBuffer)
        {
            _header = new WebSocketDataFrameHeader(headerBuffer);

            //扩展长度
            if (_header.Length == 126)
            {
                _extend = StreamHelper.Receive(stream, 2);
            }
            else if (_header.Length == 127)
            {
                _extend = StreamHelper.Receive(stream, 8);
            }

            //是否有掩码
            if (_header.HasMask)
            {
                _mask = StreamHelper.Receive(stream, 4);
            }

            //content length
            var content_length = 0L;

            if (_extend.Length == 0)
            {
                content_length = _header.Length;
            }
            else if (_extend.Length == 2)
            {
                content_length = (int)_extend[0] * 256 + (int)_extend[1];
            }
            else
            {
                int n = 1;
                for (int i = 7; i >= 0; i--)
                {
                    content_length += (int)_extend[i] * n;
                    n *= 256;
                }

                if (content_length > int.MaxValue)
                {
                    throw new WebException("Data length transfinite");
                }
            }

            this.Content = StreamHelper.Receive(stream, (int)content_length);
            if (_header.HasMask)
            {
                Mask(this.Content, _mask);
            }
        }
예제 #4
0
파일: Memcache.cs 프로젝트: aooshi/adf
        ///// <summary>
        ///// This method loads the data from cache into a Hashtable.
        ///// </summary>
        ///// <param name="hm">hashmap to store data into</param>
        ///// <param name="type"></param>
        //private void LoadItems(Hashtable hm, Type type)
        //{
        //    while (true)
        //    {
        //        string line = this.ReadLine();

        //        if (line.StartsWith(VALUE))
        //        {
        //            String key = "";
        //            Object value = null;

        //            this.ParseValue(line, type, out key, out value);
        //            // store the object into the cache
        //            hm[key] = value;
        //        }
        //        else if (END == line)
        //        {
        //            //finished
        //            break;
        //        }
        //    }
        //}

        private void ParseValue(string line, Type type, out string key, out object value)
        {
            string[] info = line.Split(' ');
            key = info[1];
            int flag   = int.Parse(info[2], NumberFormatInfo.InvariantInfo);
            int length = int.Parse(info[3], NumberFormatInfo.InvariantInfo);

            // read obj into buffer
            byte[] buf = StreamHelper.Receive(this.stream, length);
            //clear end \r\n
            var clearCRLFofEnd = new byte[128];
            var clearPosition  = 0;

            Adf.StreamHelper.ReadLine(this.stream, clearCRLFofEnd, ref clearPosition);

            // check for compression
            if ((flag & F_COMPRESSED) == F_COMPRESSED)
            {
                try
                {
                    buf = Adf.CompressHelper.Decompress(buf);
                }
                catch (Exception e)
                {
                    throw new IOException("uncompression Exception " + e.Message);
                }
            }

            // we can only take out serialized objects
            if ((flag & F_SERIALIZED) == F_SERIALIZED)
            {
                try
                {
                    value = this.BinarySerializable.Deserialize(type, buf);
                }
                catch (Exception e)
                {
                    throw new IOException("SerializationException " + e.Message);
                }
            }
            else if ((flag & F_BINARY) == F_BINARY)
            {
                value = buf;
            }
            else
            {
                value = this.Encoding.GetString(buf);
            }
        }
예제 #5
0
 /// <summary>
 /// Write Chunk Content
 /// </summary>
 /// <param name="contentBuffer"></param>
 private void Chunk(byte[] contentBuffer)
 {
     lock (this.chunkWriteEvent)
     {
         var crlfBuffer = this.Encoding.GetBytes("\r\n");
         var buffers    = new ArraySegment <byte> [4];
         //
         int contentLength = contentBuffer.Length;
         //gzip
         if (this.acceptGzip && this.gzipThreshold != 0)
         {
             this.chunkGzipStream.Write(contentBuffer, 0, contentBuffer.Length);
             contentLength = (int)this.chunkStream.Length;
             if (contentLength < 256)
             {
                 //小字节退出,待下次压缩或关闭压缩
                 return;
             }
             this.chunkStream.Position = 0;
             contentBuffer             = StreamHelper.Receive(this.chunkStream, contentLength);
             this.chunkStream.SetLength(0);
         }
         //len
         var lenBuffer = this.Encoding.GetBytes(contentLength.ToString("x"));
         buffers[0] = new ArraySegment <byte>(lenBuffer);
         buffers[1] = new ArraySegment <byte>(crlfBuffer);
         //content
         buffers[2] = new ArraySegment <byte>(contentBuffer);
         buffers[3] = new ArraySegment <byte>(crlfBuffer);
         //
         try
         {
             this.socket.Send(buffers);
         }
         catch
         {
             SocketHelper.TryClose(this.socket);
             StreamHelper.TryClose(this.chunkGzipStream);
             StreamHelper.TryClose(this.chunkStream);
             this.chunkWriteEvent.Set();
             throw;
         }
     }
 }