public void sendERR(byte[] var1 = null, byte[] var2 = null) { PYPYCMD cmd = new PYPYCMD(); cmd.cmdtype = PYPYCMDType.ERR; if (var1 != null) { cmd.parameters.Add(var1); if (var2 != null) { cmd.parameters.Add(var2); } } this.send(cmd); }
static public PYPYCMD from_bytes(byte[] data) { int t = data[0]; PYPYCMD cmd = new PYPYCMD(); cmd.cmdtype = (PYPYCMDType)t; int plen = data[1]; uint pos = 2; for (int i = 0; i < plen; i++) { Byte[] vlen_b = new byte[4]; Array.Copy(data, pos, vlen_b, 0, 4); Array.Reverse(vlen_b); uint vlen = BitConverter.ToUInt32(vlen_b, 0); Byte[] pdata = new byte[vlen]; Array.Copy(data, pos + 4, pdata, 0, vlen); cmd.parameters.Add(pdata); pos += (uint)(vlen + 4); } return(cmd); }
public void run() { try { if (useConsole) { Console.WriteLine("Trying to connect to server"); } transport.connect(); } catch (Exception e) { if (useConsole) { Console.WriteLine("Failed to connect to server! Reason: " + e.ToString()); } } bool good = true; while (good) { PYPYCMD cmd = transport.readCMD(); //Console.Write(cmd.ToString()); if (cmd.cmdtype == PYPYCMDType.END) { break; } switch (cmd.cmdtype) { case PYPYCMDType.INIT: { string data = ""; try { data = reader.get_info(); byte[] bytes = Encoding.Default.GetBytes(data); transport.sendOK(bytes); } catch (Exception e) { data = "INIT Failed! Reason: " + e.ToString(); if (useConsole) { Console.WriteLine(data); } byte[] bytes = Encoding.Default.GetBytes(data); transport.sendERR(bytes); good = false; } break; } case PYPYCMDType.FIND: { try { string module = System.Text.Encoding.UTF8.GetString(cmd.parameters[0]); ulong pos = reader.find(module, cmd.parameters[1]); byte[] data = BitConverter.GetBytes(pos); if (BitConverter.IsLittleEndian) { Array.Reverse(data); } transport.sendOK(data); } catch (Exception e) { string data = "FIND Failed! Reason: " + e.ToString(); if (useConsole) { Console.WriteLine(data); } byte[] bytes = Encoding.Default.GetBytes(data); transport.sendERR(bytes); good = false; } break; } case PYPYCMDType.READ: { try { if (BitConverter.IsLittleEndian) { Array.Reverse(cmd.parameters[0]); Array.Reverse(cmd.parameters[1]); } ulong pos = (ulong)BitConverter.ToUInt64(cmd.parameters[0], 0); ulong length = (ulong)BitConverter.ToUInt64(cmd.parameters[1], 0); byte[] data = reader.read(pos, length); transport.sendOK(data); } catch (Exception e) { string data = "READ Failed! Reason: " + e.ToString(); if (useConsole) { Console.WriteLine(data); } byte[] bytes = Encoding.Default.GetBytes(data); transport.sendERR(bytes); good = false; } break; } default: { if (useConsole) { Console.WriteLine("Unknown command!"); } break; } } } if (useConsole) { Console.WriteLine("Finished!"); } }