private static XLoper DecodeBool(Stream s) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeBool; x.Bool = s.ReadByte() != 0; return x; }
public XLoper Execute(String name, XLoper[] args) { Console.WriteLine(name); XLoper x = new XLoper(); x.Type = XLoper.xlTypeStr; x.Str = "#Unknown Function"; if (name.Equals("RandTest")) { x.Type = XLoper.xlTypeMulti; x.Arr.Arr = new XLoper[new Random().Next(50) + 2]; x.Arr.Rows = (uint)x.Arr.Arr.Length; x.Arr.Columns = 1; Random r = new Random(); for (int i = 0; i < x.Arr.Arr.Length; i++) { x.Arr.Arr[i] = MakeRandom(r); } } else if (name.Equals("ArrayTest")) { } return x; }
public static void Encode(XLoper x, Stream s) { s.WriteByte((byte) x.Type); switch (x.Type) { case XLoper.xlTypeBool: EncodeBoolean(x, s); break; case XLoper.xlTypeErr: EncodeError(x, s); break; case XLoper.xlTypeInt: EncodeInt(x, s); break; case XLoper.xlTypeMissing: break; case XLoper.xlTypeMulti: EncodeMulti(x, s); break; case XLoper.xlTypeNil: break; case XLoper.xlTypeNum: EncodeNum(x, s); break; case XLoper.xlTypeStr: EncodeStr(x, s); break; default: throw new IOException("Invalid XLoper type encountered: " + x.Type); } }
private static XLoper DecodeMissing() { XLoper x = new XLoper(); x.Type = XLoper.xlTypeMissing; return(x); }
private XLoper MakeRandom(Random r) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeNil; int choice = r.Next(7); switch (choice) { case 0: x.Type = XLoper.xlTypeStr; x.Str = MakeRandomString(r.Next(1000)); break; case 1: x.Type = XLoper.xlTypeNum; x.Num = r.NextDouble() * 1000; break; case 2: x.Type = XLoper.xlTypeInt; x.W = r.Next(1000); break; case 3: x.Type = XLoper.xlTypeBool; x.Bool = r.Next(2) == 1; break; case 4: x.Type = XLoper.xlTypeStr; x.Str = ""; break; } return x; }
private static void EncodeNum(XLoper x, Stream s) { long l = BitConverter.DoubleToInt64Bits(x.Num); WriteDWORD((uint)(l >> 32), s); WriteDWORD((uint)l, s); }
private static XLoper DecodeStr(Stream s) { int len = s.ReadByte(); if (len == -1) { throw new EndOfStreamException(); } byte[] b = new byte[len]; int r = s.Read(b, 0, len); if (r == -1) { throw new EndOfStreamException(); } while (r < len) { int sf = s.Read(b, r, len - r); if (sf == -1) { throw new EndOfStreamException(); } r += sf; } XLoper x = new XLoper(); x.Type = XLoper.xlTypeStr; x.Str = Encoding.UTF8.GetString(b); return(x); }
private static XLoper DecodeNil() { XLoper x = new XLoper(); x.Type = XLoper.xlTypeNil; return(x); }
private static XLoper DecodeError(Stream s) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeErr; x.W = (int)ReadDWORD(s); return x; }
public XLoper Execute(String name, XLoper[] args) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeNil; return x; }
private static XLoper DecodeError(Stream s) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeErr; x.W = (int)ReadDWORD(s); return(x); }
private static XLoper DecodeBool(Stream s) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeBool; x.Bool = s.ReadByte() != 0; return(x); }
public XLoper Execute(String name, XLoper[] args) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeNil; return(x); }
private static void EncodeMulti(XLoper x, Stream s) { WriteDWORD(x.Arr.Rows, s); WriteDWORD(x.Arr.Columns, s); for (int i = 0; i < x.Arr.Arr.Length; i++) { Encode(x.Arr.Arr[i], s); } }
private static XLoper DecodeNum(Stream s) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeNum; x.Num = BitConverter.Int64BitsToDouble(((long)ReadDWORD(s) << 32) | (long)ReadDWORD(s)); return(x); }
private static void EncodeStr(XLoper x, Stream s) { byte[] b = Encoding.UTF8.GetBytes(x.Str); if (b.Length > 255) { s.WriteByte((byte)255); s.Write(b, 0, 255); } else { s.WriteByte((byte)b.Length); s.Write(b, 0, b.Length); } }
private static XLoper DecodeMulti(Stream s) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeMulti; x.Arr = new multi(); x.Arr.Rows = ReadDWORD(s); x.Arr.Columns = ReadDWORD(s); uint len = x.Arr.Rows * x.Arr.Columns; x.Arr.Arr = new XLoper[len]; for (uint i = 0; i < len; i++) { x.Arr.Arr[i] = Decode(s); } return(x); }
public void Run() { while (socket.IsBound) { try { XLoper name = BinaryCodec.Decode(stream); if (name.Type != XLoper.xlTypeStr) { throw new Exception("Protocol Error: expecting function name"); } XLoper argc = BinaryCodec.Decode(stream); if (argc.Type != XLoper.xlTypeInt) { throw new Exception("Protocol Error: expecting arg count"); } XLoper[] args = new XLoper[argc.W]; for (int i = 0; i < argc.W; i++) { args[i] = BinaryCodec.Decode(stream); } try { XLoper res = handler.Execute(name.Str, args); BinaryCodec.Encode(res, stream); } catch (RequestException re) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeStr; x.Str = re.Message; BinaryCodec.Encode(x, stream); } } catch (Exception e) { Console.WriteLine(e.StackTrace); socket.Close(); } } }
public static void Encode(XLoper x, Stream s) { s.WriteByte((byte)x.Type); switch (x.Type) { case XLoper.xlTypeBool: EncodeBoolean(x, s); break; case XLoper.xlTypeErr: EncodeError(x, s); break; case XLoper.xlTypeInt: EncodeInt(x, s); break; case XLoper.xlTypeMissing: break; case XLoper.xlTypeMulti: EncodeMulti(x, s); break; case XLoper.xlTypeNil: break; case XLoper.xlTypeNum: EncodeNum(x, s); break; case XLoper.xlTypeStr: EncodeStr(x, s); break; default: throw new IOException("Invalid XLoper type encountered: " + x.Type); } }
private static XLoper DecodeMulti(Stream s) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeMulti; x.Arr = new multi(); x.Arr.Rows = ReadDWORD(s); x.Arr.Columns = ReadDWORD(s); uint len = x.Arr.Rows * x.Arr.Columns; x.Arr.Arr = new XLoper[len]; for (uint i = 0; i < len; i++) { x.Arr.Arr[i] = Decode(s); } return x; }
private static XLoper DecodeNil() { XLoper x = new XLoper(); x.Type = XLoper.xlTypeNil; return x; }
private static void EncodeBoolean(XLoper x, Stream s) { s.WriteByte((byte)(x.Bool ? 1 : 0)); }
private static void EncodeError(XLoper x, Stream s) { WriteDWORD((uint)x.Err, s); }
private static void EncodeInt(XLoper x, Stream s) { WriteDWORD((uint)x.W, s); }
private static XLoper DecodeNum(Stream s) { XLoper x = new XLoper(); x.Type = XLoper.xlTypeNum; x.Num = BitConverter.Int64BitsToDouble(((long)ReadDWORD(s) << 32) | (long)ReadDWORD(s)); return x; }
private static void EncodeBoolean(XLoper x, Stream s) { s.WriteByte((byte) (x.Bool ? 1 : 0)); }
private static void EncodeInt(XLoper x, Stream s) { WriteDWORD((uint) x.W, s); }
private static XLoper DecodeStr(Stream s) { int len = s.ReadByte(); if (len == -1) throw new EndOfStreamException(); byte[] b = new byte[len]; int r = s.Read(b, 0, len); if (r == -1) throw new EndOfStreamException(); while (r < len) { int sf = s.Read(b, r, len - r); if (sf == -1) throw new EndOfStreamException(); r += sf; } XLoper x = new XLoper(); x.Type = XLoper.xlTypeStr; x.Str = Encoding.UTF8.GetString(b); return x; }
private static XLoper DecodeMissing() { XLoper x = new XLoper(); x.Type = XLoper.xlTypeMissing; return x; }