public static Address decodeAddress(KVMemoryStream stream) { String ip = stream.ReadString(); int port = stream.ReadInt(); return(new Address(ip, port)); }
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(); }
private static SystemInfo decodeSystemInfo(KVMemoryStream stream) { SystemInfo info = new SystemInfo(); info.memoryFree = stream.ReadLong(); info.memoryTotal = stream.ReadLong(); info.cpuUsage = stream.ReadDouble(); return info; }
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); }
private static void EndWrite(KVMemoryStream stream) { long pos = stream.Position; stream.Position = 0; int len = (int)pos - 4; stream.WriteInt(len); }
private static SystemInfo decodeSystemInfo(KVMemoryStream stream) { SystemInfo info = new SystemInfo(); info.memoryFree = stream.ReadLong(); info.memoryTotal = stream.ReadLong(); info.cpuUsage = stream.ReadDouble(); return(info); }
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; }
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()); } }
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); } }
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)); }
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); }
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()); } }
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()); } }
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; }
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); }
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; }
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); } }
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); }
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()); } }
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); } }
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); } }
public static Address decodeAddress(KVMemoryStream stream) { String ip = stream.ReadString(); int port = stream.ReadInt(); return new Address(ip, port); }
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()); } }
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(); }