Esempio n. 1
0
        public object Send(object data)
        {
            try
            {
                CheckConnection();

                byte[] dat        = fastBinaryJSON.BJSON.ToBJSON(data);
                bool   compressed = false;
#if minilzo
                if (dat.Length > NetworkClient.Config.CompressDataOver)
                {
                    log.Info("compressing data over limit : " + dat.Length.ToString("#,#"));
                    compressed = true;
                    dat        = RaptorDB.MiniLZO.Compress(dat);
                    log.Info("new size : " + dat.Length.ToString("#,#"));
                }
#endif
                byte[] len = Helper.GetBytes(dat.Length, false);

                using (NetworkStream ns = new NetworkStream(_client.Client))
                {
                    BufferedStream n = new BufferedStream(ns, Config.BufferSize);
                    // header
                    n.WriteByte((byte)((UseBJSON ? 3 : 0) + (compressed ? 4 : 0)));
                    n.Write(len, 0, 4);
                    // data
                    n.Write(dat, 0, dat.Length);
                    // read response
                    byte[] hdr = new byte[5];
                    n.Read(hdr, 0, 5);
                    int    count     = Helper.ToInt32(hdr, 1);
                    byte[] recd      = new byte[count];
                    int    bytesRead = 0;
                    int    chunksize = 1;
                    while (bytesRead < count && chunksize > 0)
                    {
                        bytesRead    +=
                            chunksize = n.Read
                                            (recd, bytesRead, count - bytesRead);
                    }
#if minilzo
                    if ((hdr[0] & (byte)4) == (byte)4)
                    {
                        recd = RaptorDB.MiniLZO.Decompress(recd);
                    }
#endif
                    if ((hdr[0] & (byte)3) == (byte)3)
                    {
                        return(fastBinaryJSON.BJSON.ToObject(recd));
                    }
                }
            }
            catch
            {
            }
            return(null);
        }
Esempio n. 2
0
        public object Send(object data)
        {
            try
            {
                CheckConnection();

                byte[] hdr = new byte[5];
                hdr[0] = (UseBJSON ? (byte)3 : (byte)0);
                byte[] dat        = fastBinaryJSON.BJSON.ToBJSON(data);
                bool   compressed = false;
                if (dat.Length > NetworkClient.Config.CompressDataOver)
                {
                    log.Info("compressing data over limit : " + dat.Length.ToString("#,#"));
                    compressed = true;
                    dat        = MiniLZO.Compress(dat);
                    log.Info("new size : " + dat.Length.ToString("#,#"));
                }
                byte[] len = Helper.GetBytes(dat.Length, false);
                hdr[0] = (byte)(3 + (compressed ? 4 : 0));
                Array.Copy(len, 0, hdr, 1, 4);
                _client.Client.Send(hdr);
                _client.Client.Send(dat);

                byte[] rechdr = new byte[5];
                using (NetworkStream n = new NetworkStream(_client.Client))
                {
                    n.Read(rechdr, 0, 5);
                    int    c         = Helper.ToInt32(rechdr, 1);
                    byte[] recd      = new byte[c];
                    int    bytesRead = 0;
                    int    chunksize = 1;
                    while (bytesRead < c && chunksize > 0)
                    {
                        bytesRead    +=
                            chunksize = n.Read
                                            (recd, bytesRead, c - bytesRead);
                    }
                    if ((rechdr[0] & (byte)4) == (byte)4)
                    {
                        recd = MiniLZO.Decompress(recd);
                    }
                    if ((rechdr[0] & (byte)3) == (byte)3)
                    {
                        return(fastBinaryJSON.BJSON.ToObject(recd));
                    }
                }
            }
            catch
            {
            }
            return(null);
        }