Exemplo n.º 1
0
        void Accept(TcpClient client)
        {
            client.ReceiveBufferSize = 0;
            client.SendBufferSize    = NetworkClient.Config.BufferSize;
            using (NetworkStream ns = client.GetStream())
            {
                BufferedStream n = new BufferedStream(ns, NetworkClient.Config.BufferSize);
                {
                    byte[] hdr = new byte[5];
                    n.Read(hdr, 0, 5);
                    int    count     = BitConverter.ToInt32(hdr, 1);
                    byte[] data      = new byte[count];
                    int    bytesRead = 0;
                    int    chunksize = 1;
                    while (bytesRead < count && chunksize > 0)
                    {
                        bytesRead    +=
                            chunksize = n.Read
                                            (data, bytesRead, count - bytesRead);
                    }
#if minilzo
                    if ((hdr[0] & (byte)4) == (byte)4)
                    {
                        data = RaptorDB.MiniLZO.Decompress(data);
                    }
#endif
                    object o = fastBinaryJSON.BJSON.ToObject(data);
                    // handle payload
                    object r          = _handler(o);
                    bool   compressed = false;
                    var    dataret    = fastBinaryJSON.BJSON.ToBJSON(r);
#if minilzo
                    if (dataret.Length > NetworkClient.Config.CompressDataOver)
                    {
                        log.Info("compressing data over limit : " + dataret.Length.ToString("#,#"));
                        compressed = true;
                        dataret    = RaptorDB.MiniLZO.Compress(dataret);
                        log.Info("new size : " + dataret.Length.ToString("#,#"));
                    }
                    if (dataret.Length > NetworkClient.Config.LogDataSizesOver)
                    {
                        log.Info("data size (bytes) = " + dataret.Length.ToString("#,#"));
                    }
#endif
                    byte[] len = Helper.GetBytes(dataret.Length, false);
                    // header
                    n.WriteByte((byte)((true ? 3 : 0) + (compressed ? 4 : 0)));
                    n.Write(len, 0, 4);
                    // data
                    n.Write(dataret, 0, dataret.Length);
                    n.Flush();
                    //n.Close();

                    //client.Close();
                    //int wait = 0;
                    //bool close = false;
                    //var dt = FastDateTime.Now;
                    //while (n.DataAvailable == false && close == false)
                    //{
                    //    wait++;
                    //    if (wait < 10000) // kludge : for insert performance
                    //        Thread.Sleep(0);
                    //    else
                    //    {
                    //        Thread.Sleep(1);
                    //        // wait done -> close connection
                    //        if (FastDateTime.Now.Subtract(dt).TotalSeconds > NetworkClient.Config.KillConnectionSeconds)
                    //            close = true;
                    //    }
                    //}
                    //if (close)
                    //    break;
                }
            }
            client.Close();
        }