Exemplo n.º 1
0
        public void TestFilled()
        {
            ByteCache.FillCache callback = delegate(byte[] buffer, out long cacheStart, out int cacheLength)
            {
                cacheStart  = 5;
                cacheLength = 4;
                buffer[0]   = 5;
                buffer[1]   = 6;
                buffer[2]   = 7;
                buffer[3]   = 8;
            };
            var  cache = new ByteCache(256);
            byte res;

            cache.Cache(callback);
            Assert.Equal(5, cache.Start);
            Assert.Equal(4, cache.Length);
            Assert.False(cache.TryReadByte(0, out res));
            Assert.False(cache.TryReadByte(4, out res));
            for (int i = 0; i < 4; ++i)
            {
                Assert.True(cache.TryReadByte(i + 5, out res));
                Assert.Equal(i + 5, res);
            }
        }
Exemplo n.º 2
0
        public void TestEmpty()
        {
            var  cache = new ByteCache(256);
            byte res;

            Assert.False(cache.TryReadByte(0, out res));
            Assert.False(cache.TryReadByte(1, out res));
        }
Exemplo n.º 3
0
 private void init()
 {
     using (var reader = log.ReaderPool.Get())
     {
         hint = reader.GetFileHint(id);
     }
     cache             = new ByteCache(256);
     cacheLock         = new object();
     serializationLock = new object();
     otherLogFiles     = new Dictionary <FileLog, long>();
 }
Exemplo n.º 4
0
 private void  Init()
 {
     _byteCache        = new ByteCache(this);
     _shortCache       = new ShortCache(this);
     _intCache         = new IntCache(this);
     _floatCache       = new FloatCache(this);
     _longCache        = new LongCache(this);
     _doubleCache      = new DoubleCache(this);
     _stringCache      = new StringCache(this);
     _stringIndexCache = new StringIndexCache(this);
 }
Exemplo n.º 5
0
 private void  Init()
 {
     lock (this)
     {
         caches = new HashMap<Type, Cache>(7);
         caches[typeof(sbyte)] = new ByteCache(this);
         caches[typeof(short)] = new ShortCache(this);
         caches[typeof(int)] = new IntCache(this);
         caches[typeof(float)] = new FloatCache(this);
         caches[typeof(long)] = new LongCache(this);
         caches[typeof(double)] = new DoubleCache(this);
         caches[typeof(string)] = new StringCache(this);
         caches[typeof(StringIndex)] = new StringIndexCache(this);
     }
 }
Exemplo n.º 6
0
 private void  Init()
 {
     lock (this)
     {
         caches = new HashMap <Type, Cache>(7);
         caches[typeof(sbyte)]       = new ByteCache(this);
         caches[typeof(short)]       = new ShortCache(this);
         caches[typeof(int)]         = new IntCache(this);
         caches[typeof(float)]       = new FloatCache(this);
         caches[typeof(long)]        = new LongCache(this);
         caches[typeof(double)]      = new DoubleCache(this);
         caches[typeof(string)]      = new StringCache(this);
         caches[typeof(StringIndex)] = new StringIndexCache(this);
     }
 }
Exemplo n.º 7
0
		private void  Init()
		{
			lock (this)
			{
                Support.Dictionary<Type, Cache> caches2 = new Support.Dictionary<Type, Cache>();
                caches2[typeof(sbyte)] = new ByteCache(this);
                caches2[typeof(short)] = new ShortCache(this);
                caches2[typeof(int)] = new IntCache(this);
                caches2[typeof(float)] = new FloatCache(this);
                caches2[typeof(long)] = new LongCache(this);
                caches2[typeof(double)] = new DoubleCache(this);
                caches2[typeof(string)] = new StringCache(this);
                caches2[typeof(StringIndex)] = new StringIndexCache(this);
                caches2[typeof(System.IComparable)] = new CustomCache(this);
                caches2[typeof(System.Object)] = new AutoCache(this);
                caches = caches2;
			}
		}
Exemplo n.º 8
0
 private void  Init()
 {
     lock (this)
     {
         Support.Dictionary <Type, Cache> caches2 = new Support.Dictionary <Type, Cache>();
         caches2[typeof(sbyte)]              = new ByteCache(this);
         caches2[typeof(short)]              = new ShortCache(this);
         caches2[typeof(int)]                = new IntCache(this);
         caches2[typeof(float)]              = new FloatCache(this);
         caches2[typeof(long)]               = new LongCache(this);
         caches2[typeof(double)]             = new DoubleCache(this);
         caches2[typeof(string)]             = new StringCache(this);
         caches2[typeof(StringIndex)]        = new StringIndexCache(this);
         caches2[typeof(System.IComparable)] = new CustomCache(this);
         caches2[typeof(System.Object)]      = new AutoCache(this);
         caches = caches2;
     }
 }
Exemplo n.º 9
0
        public void TestShiftLeft()
        {
            ByteCache.FillCache callback = delegate(byte[] buffer, out long cacheStart, out int cacheLength)
            {
                cacheStart  = 5;
                cacheLength = 4;
                buffer[0]   = 5;
                buffer[1]   = 6;
                buffer[2]   = 7;
                buffer[3]   = 8;
            };
            byte res;
            var  cache = new ByteCache(256);

            cache.Cache(callback);
            cache.ShiftLeft(6);
            Assert.Equal(6, cache.Start);
            Assert.Equal(3, cache.Length);
            Assert.False(cache.TryReadByte(5, out res));
            Assert.True(cache.TryReadByte(6, out res));
            Assert.Equal(6, res);
        }
Exemplo n.º 10
0
        public void TestTryReadBytesToBuffer()
        {
            ByteCache.FillCache callback = delegate(byte[] buffer, out long cacheStart, out int cacheLength)
            {
                cacheStart  = 5;
                cacheLength = 4;
                buffer[0]   = 5;
                buffer[1]   = 6;
                buffer[2]   = 7;
                buffer[3]   = 8;
            };
            var  tmp   = new byte[10];
            var  cache = new ByteCache(256);
            long firstRead;
            int  read;

            Assert.False(cache.TryReadBytesToBuffer(tmp, 5, 1, 0, out firstRead, out read));
            cache.Cache(callback);

            Assert.True(cache.TryReadBytesToBuffer(tmp, 5, 1, 0, out firstRead, out read));
            Assert.Equal(5, firstRead);
            Assert.Equal(1, read);
            Assert.Equal(5, tmp[0]);

            Assert.True(cache.TryReadBytesToBuffer(tmp, 3, 5, 2, out firstRead, out read));
            Assert.Equal(5, firstRead);
            Assert.Equal(3, read);
            Assert.Equal(5, tmp[4]);
            Assert.Equal(6, tmp[5]);
            Assert.Equal(7, tmp[6]);

            Assert.True(cache.TryReadBytesToBuffer(tmp, 7, 5, 2, out firstRead, out read));
            Assert.Equal(7, firstRead);
            Assert.Equal(2, read);
            Assert.Equal(7, tmp[2]);
            Assert.Equal(8, tmp[3]);
        }
Exemplo n.º 11
0
 public CDNFolder()
 {
     cache      = new ByteCache <string>();
     cache.Load = (x) => File.ReadAllBytes(x);
 }
Exemplo n.º 12
0
    public void Init()
    {
        this.mWebSocket = new WebSocketServer("ws://127.0.0.1:6324");
        this.mWebSocket.Start(socket => {
            socket.OnOpen = delegate()
            {
                this.cache_map[socket] = new ByteCache();
                this.socket_map.Add(++this.max_connect_id, socket);
                Log.Debug("用户连接:" + socket.ConnectionInfo.ClientIpAddress);
            };

            socket.OnClose = delegate()
            {
                this.cache_map.Remove(socket);
                this.socket_map.Remove(socket);
                Log.Debug("用户断线:" + socket.ConnectionInfo.ClientIpAddress);
            };

            socket.OnError = delegate(Exception e)
            {
                Log.Debug(e.ToString());
            };

            socket.OnMessage = delegate(string str)
            {
                Log.Debug("用户发送数据:" + socket.ConnectionInfo.ClientIpAddress + ":" + str);
            };

            socket.OnBinary = delegate(byte[] bytes)
            {
                ByteCache cache = this.cache_map[socket];
                cache.AddBytes(bytes, bytes.Length);

                try
                {
                    while (true)
                    {
                        byte[] msg_bytes = cache.DivideMessage();
                        if (msg_bytes == null)
                        {
                            break;
                        }

                        int connect_id;
                        if (this.socket_map.HaveValue(socket))
                        {
                            connect_id = this.socket_map.GetKey(socket);
                        }
                        else
                        {
                            Exception e = new Exception("Socket对应的连接ID不存在!!!");
                            throw e;
                        }
                        this.UnPackProtocol(msg_bytes, connect_id);
                    }
                }
                catch (Exception e)
                {
                    Log.Debug(e.ToString());
                    socket.Close();
                }
            };
        });
        Log.Debug("服务器WebSocket正在监听 127.0.0.1:6324");
    }
Exemplo n.º 13
0
        public override Boolean AttachStorage(String myStorageLocation)
        {
            lock (this)
            {

                if (_FileStream == null)
                {

                        _StorageLocation    = myStorageLocation;
                    var _ImageFileLocation  = GetImageFileLocation(myStorageLocation);

                    _WriteQueueLock         = new WriteQueueLock();
                    _FileStream             = new FileStream(_ImageFileLocation, FileMode.Open);
                    _ReadQueue              = new ReadQueueManager(_WriteQueueLock);
                    _WriteQueue             = new WriteQueueManager(_WriteQueueLock);

                    _ReadQueue.SetFileStream(_FileStream);
                    _WriteQueue.SetFileStream(_FileStream);

                    // Configure the ByteCache
                    if (_ByteCacheSettings == null)
                        _ByteCacheSettings = new CacheSettings()
                            {
                                TimerDueTime                 = TimeSpan.FromSeconds(3),
                                TimerPeriod                  = TimeSpan.FromSeconds(120),
                                AbsoluteExpirationTimeSpan   = TimeSpan.FromSeconds(120),
                                ExpirationType               = ExpirationTypes.Absolute
                            };

                    _ByteCache = new ByteCache("<FileStorage> " + _StorageLocation, _ByteCacheSettings);

                    _WriteQueue.NotificationDispatcher = _NotificationDispatcher;
                    _WriteQueue.OnFlushSucceeded += new FlushSucceededHandler(WriteQueue_OnFlushSucceeded);

                    return true;

                }

                return false;

            }
        }
Exemplo n.º 14
0
 public FromUnseekableStream(StreamFactory factory, int cacheSize = 5 *1024 *1024)
 {
     cache        = new ByteCache(cacheSize);
     this.factory = factory;
 }
Exemplo n.º 15
0
    private void RecieveMessage(object client)
    {
        Socket client_socket = (Socket)client;

        while (true)
        {
            try
            {
                byte[] data   = new byte[Const.BUFFSIZE];
                int    length = client_socket.Receive(data);
                LogTool.Tip("服务端接收消息:" + client_socket.RemoteEndPoint.ToString() + "长度为:" + length);

                if (length > 0)
                {
                    if (!cache_map.ContainsKey(client_socket))
                    {
                        cache_map[client_socket] = new ByteCache();
                    }
                    ByteCache cache = cache_map[client_socket];
                    cache.AddBytes(data, length);

                    while (true)
                    {
                        byte[] msg_bytes = cache.DivideMessage();
                        if (msg_bytes == null)
                        {
                            break;
                        }

                        IMessage protocol = Protocol.Decode(msg_bytes);
                        if (protocol != null)
                        {
                            int connect_id;
                            if (this.socket_map.HaveValue(client_socket))
                            {
                                connect_id = this.socket_map.GetKey(client_socket);
                            }
                            else if (this.temp_socket_map.HaveValue(client_socket))
                            {
                                connect_id = this.temp_socket_map.GetKey(client_socket);
                            }
                            else
                            {
                                Exception e = new Exception("Socket对应的连接ID不存在!!!");
                                throw e;
                            }

                            this.DispatchProtocol(protocol, connect_id);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogTool.Tip(e.StackTrace);
                client_socket.Shutdown(SocketShutdown.Both);
                client_socket.Close();
                break;
            }
        }
    }