Esempio n. 1
0
        public static Address decodeAddress(KVMemoryStream stream)
        {
            String ip   = stream.ReadString();
            int    port = stream.ReadInt();

            return(new Address(ip, port));
        }
Esempio n. 2
0
        private static KVMemoryStream ReadResponse(Socket socket)
        {
            KVMemoryStream stream        = new KVMemoryStream(64);
            int            readLength    = 0;
            int            messageLength = 0;

            while ((readLength = socket.Receive(readBuffer)) > 0)
            {
                stream.Write(readBuffer, 0, readLength);
                if (messageLength == 0 || readLength >= 4)
                {
                    //calculate message length
                    long oldPos = stream.Position;
                    stream.Position = 0;
                    messageLength   = stream.ReadInt();
                    stream.Position = oldPos;
                }
                if (messageLength <= readLength - 4)
                {
                    stream.Position = 0;
                    return(stream);
                }
            }
            throw new InvalidDataException();
        }
Esempio n. 3
0
 private static SystemInfo decodeSystemInfo(KVMemoryStream stream)
 {
     SystemInfo info = new SystemInfo();
     info.memoryFree = stream.ReadLong();
     info.memoryTotal = stream.ReadLong();
     info.cpuUsage = stream.ReadDouble();
     return info;
 }
Esempio n. 4
0
 public static Region decodeRegion(KVMemoryStream stream)
 {
     int regionId = stream.ReadInt();
     byte[] start = stream.ReadBytes();
     byte[] end = stream.ReadBytes();
     RegionStat stat = decodeRegionStat(stream);
     return new Region(regionId, start, end, stat);
 }
Esempio n. 5
0
        private static void EndWrite(KVMemoryStream stream)
        {
            long pos = stream.Position;

            stream.Position = 0;
            int len = (int)pos - 4;

            stream.WriteInt(len);
        }
Esempio n. 6
0
        private static SystemInfo decodeSystemInfo(KVMemoryStream stream)
        {
            SystemInfo info = new SystemInfo();

            info.memoryFree  = stream.ReadLong();
            info.memoryTotal = stream.ReadLong();
            info.cpuUsage    = stream.ReadDouble();
            return(info);
        }
Esempio n. 7
0
 public static RegionStat decodeRegionStat(KVMemoryStream stream)
 {
     RegionStat stat = new RegionStat();
     stat.keyNum = stream.ReadInt();
     stat.size = stream.ReadLong();
     stat.readCount = stream.ReadLong();
     stat.writeCount = stream.ReadLong();
     return stat;
 }
Esempio n. 8
0
 internal static void WriteUpdateRegionTableRequest(Socket socket)
 {
     using (KVMemoryStream stream = new KVMemoryStream(32))
     {
         stream.Position = 4;
         stream.WriteInt(KVProtocolType.Region_Table_Req);
         EndWrite(stream);
         socket.Send(stream.ToArray());
     }
 }
Esempio n. 9
0
 internal static int ReadDeleteResponse(Socket socket)
 {
     using (KVMemoryStream stream = ReadResponse(socket))
     {
         int len     = stream.ReadInt();
         int type    = stream.ReadInt();
         int retCode = stream.ReadInt();
         return(retCode);
     }
 }
Esempio n. 10
0
        public static Region decodeRegion(KVMemoryStream stream)
        {
            int regionId = stream.ReadInt();

            byte[]     start = stream.ReadBytes();
            byte[]     end   = stream.ReadBytes();
            RegionStat stat  = decodeRegionStat(stream);

            return(new Region(regionId, start, end, stat));
        }
Esempio n. 11
0
        public static RegionStat decodeRegionStat(KVMemoryStream stream)
        {
            RegionStat stat = new RegionStat();

            stat.keyNum     = stream.ReadInt();
            stat.size       = stream.ReadLong();
            stat.readCount  = stream.ReadLong();
            stat.writeCount = stream.ReadLong();
            return(stat);
        }
Esempio n. 12
0
 internal static void WriteStatRequest(Socket socket)
 {
     using (KVMemoryStream stream = new KVMemoryStream(64))
     {
         stream.Position = 4;
         stream.WriteInt(KVProtocolType.Stat_Req);
         EndWrite(stream);
         socket.Send(stream.ToArray());
     }
 }
Esempio n. 13
0
 internal static void WriteGetRequest(Socket socket, byte[] key)
 {
     using (KVMemoryStream stream = new KVMemoryStream(64))
     {
         stream.Position = 4;
         stream.WriteInt(KVProtocolType.Get_Req);
         stream.WriteByte(1);
         stream.WriteBytes(key);
         EndWrite(stream);
         socket.Send(stream.ToArray());
     }
 }
Esempio n. 14
0
 public static RegionTable decodeRegionTable(KVMemoryStream stream)
 {
     RegionTable table =new RegionTable();
     int size = stream.ReadInt();
     for (int i = 0; i < size; i++)
     {
         Region region = decodeRegion(stream);
         Address addr = decodeAddress(stream);
         table.addRegion(region, addr);
     }
     return table;
 }
Esempio n. 15
0
        public static RegionTable decodeRegionTable(KVMemoryStream stream)
        {
            RegionTable table = new RegionTable();
            int         size  = stream.ReadInt();

            for (int i = 0; i < size; i++)
            {
                Region  region = decodeRegion(stream);
                Address addr   = decodeAddress(stream);
                table.addRegion(region, addr);
            }
            return(table);
        }
Esempio n. 16
0
 internal static DataServerStruct decodeDataServer(KVMemoryStream stream)
 {
     Address addr = decodeAddress(stream);
     int weight = stream.ReadInt();
     SystemInfo info = decodeSystemInfo(stream);
     int size = stream.ReadInt();
     DataServerStruct dataServer = new DataServerStruct(addr, weight, info);
     for (int i = 0; i < size; i++)
     {
         Region region = decodeRegion(stream);
         dataServer.addRegion(region);
     }
     return dataServer;
 }
Esempio n. 17
0
 internal static int ReadGetResponse(Socket socket, out byte[] value)
 {
     using (KVMemoryStream stream = ReadResponse(socket))
     {
         int    len     = stream.ReadInt();
         int    type    = stream.ReadInt();
         int    retCode = stream.ReadInt();
         int    retry   = stream.ReadByte();
         int    ttl     = stream.ReadInt();
         byte[] key     = stream.ReadBytes();
         value = stream.ReadBytes();
         return(retCode);
     }
 }
Esempio n. 18
0
        internal static DataServerStruct decodeDataServer(KVMemoryStream stream)
        {
            Address          addr       = decodeAddress(stream);
            int              weight     = stream.ReadInt();
            SystemInfo       info       = decodeSystemInfo(stream);
            int              size       = stream.ReadInt();
            DataServerStruct dataServer = new DataServerStruct(addr, weight, info);

            for (int i = 0; i < size; i++)
            {
                Region region = decodeRegion(stream);
                dataServer.addRegion(region);
            }
            return(dataServer);
        }
Esempio n. 19
0
 internal static void WriteIncrRequest(Socket socket, byte[] key, int incremental, int initValue)
 {
     using (KVMemoryStream stream = new KVMemoryStream(64))
     {
         stream.Position = 4;
         stream.WriteInt(KVProtocolType.Incr_Req);
         stream.WriteByte(1);
         stream.WriteInt(0);
         stream.WriteBytes(key);
         stream.WriteInt(incremental);
         stream.WriteInt(initValue);
         EndWrite(stream);
         socket.Send(stream.ToArray());
     }
 }
Esempio n. 20
0
 internal static int ReadRegionTableResponse(Socket socket, out RegionTable table)
 {
     using (KVMemoryStream stream = ReadResponse(socket))
     {
         int len     = stream.ReadInt();
         int type    = stream.ReadInt();
         int retCode = stream.ReadInt();
         if (retCode == ProtocolCode.Success)
         {
             table = DecoderUtil.decodeRegionTable(stream);
         }
         else
         {
             table = null;
         }
         return(retCode);
     }
 }
Esempio n. 21
0
 internal static int ReadStatResponse(Socket socket, out DataServerStruct[] dataServers)
 {
     using (KVMemoryStream stream = ReadResponse(socket))
     {
         int len     = stream.ReadInt();
         int type    = stream.ReadInt();
         int retCode = stream.ReadInt();
         dataServers = null;
         int size = stream.ReadInt();
         if (size > 0)
         {
             dataServers = new DataServerStruct[size];
             for (int i = 0; i < size; i++)
             {
                 dataServers[i] = DecoderUtil.decodeDataServer(stream);
             }
         }
         return(retCode);
     }
 }
Esempio n. 22
0
 public static Address decodeAddress(KVMemoryStream stream)
 {
     String ip = stream.ReadString();
     int port = stream.ReadInt();
     return new Address(ip, port);
 }
Esempio n. 23
0
 internal static void WriteIncrRequest(Socket socket, byte[] key, int incremental, int initValue)
 {
     using (KVMemoryStream stream = new KVMemoryStream(64))
     {
         stream.Position = 4;
         stream.WriteInt(KVProtocolType.Incr_Req);
         stream.WriteByte(1);
         stream.WriteInt(0);
         stream.WriteBytes(key);
         stream.WriteInt(incremental);
         stream.WriteInt(initValue);
         EndWrite(stream);
         socket.Send(stream.ToArray());
     }
 }
Esempio n. 24
0
 internal static void WriteSetRequest(Socket socket, byte[] key, byte[] value)
 {
     using (KVMemoryStream stream = new KVMemoryStream(64))
     {
         stream.Position = 4;
         stream.WriteInt(KVProtocolType.Set_Req);
         stream.WriteByte(1);
         stream.WriteInt(0);
         stream.WriteBytes(key);
         stream.WriteBytes(value);
         EndWrite(stream);
         socket.Send(stream.ToArray());
     }
 }
Esempio n. 25
0
 internal static void WriteStatRequest(Socket socket)
 {
     using (KVMemoryStream stream = new KVMemoryStream(64))
     {
         stream.Position = 4;
         stream.WriteInt(KVProtocolType.Stat_Req);
         EndWrite(stream);
         socket.Send(stream.ToArray());
     }
 }
Esempio n. 26
0
 internal static void WriteUpdateRegionTableRequest(Socket socket)
 {
     using (KVMemoryStream stream = new KVMemoryStream(32))
     {
         stream.Position = 4;
         stream.WriteInt(KVProtocolType.Region_Table_Req);
         EndWrite(stream);
         socket.Send(stream.ToArray());
     }
 }
Esempio n. 27
0
 private static void EndWrite(KVMemoryStream stream)
 {
     long pos = stream.Position;
     stream.Position = 0;
     int len = (int)pos - 4;
     stream.WriteInt(len);
 }
Esempio n. 28
0
 private static KVMemoryStream ReadResponse(Socket socket)
 {
     KVMemoryStream stream = new KVMemoryStream(64);
     int readLength = 0;
     int messageLength = 0;
     while ((readLength = socket.Receive(readBuffer)) > 0)
     {
         stream.Write(readBuffer, 0, readLength);
         if (messageLength == 0 || readLength >= 4)
         {
             //calculate message length
             long oldPos = stream.Position;
             stream.Position = 0;
             messageLength = stream.ReadInt();
             stream.Position = oldPos;
         }
         if (messageLength <= readLength - 4)
         {
             stream.Position = 0;
             return stream;
         }
     }
     throw new InvalidDataException();
 }