Exemple #1
0
 private static XLoper DecodeBool(Stream s)
 {
     XLoper x = new XLoper();
     x.Type = XLoper.xlTypeBool;
     x.Bool = s.ReadByte() != 0;
     return x;
 }
Exemple #2
0
        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;
        }
Exemple #3
0
 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);
     }
 }
Exemple #4
0
        private static XLoper DecodeMissing()
        {
            XLoper x = new XLoper();

            x.Type = XLoper.xlTypeMissing;
            return(x);
        }
Exemple #5
0
        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;
        }
Exemple #6
0
        private static void EncodeNum(XLoper x, Stream s)
        {
            long l = BitConverter.DoubleToInt64Bits(x.Num);

            WriteDWORD((uint)(l >> 32), s);
            WriteDWORD((uint)l, s);
        }
Exemple #7
0
        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);
        }
Exemple #8
0
        private static XLoper DecodeNil()
        {
            XLoper x = new XLoper();

            x.Type = XLoper.xlTypeNil;
            return(x);
        }
Exemple #9
0
 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;
        }
Exemple #11
0
        private static XLoper DecodeError(Stream s)
        {
            XLoper x = new XLoper();

            x.Type = XLoper.xlTypeErr;
            x.W    = (int)ReadDWORD(s);
            return(x);
        }
Exemple #12
0
        private static XLoper DecodeBool(Stream s)
        {
            XLoper x = new XLoper();

            x.Type = XLoper.xlTypeBool;
            x.Bool = s.ReadByte() != 0;
            return(x);
        }
Exemple #13
0
        public XLoper Execute(String name, XLoper[] args)
        {
            XLoper x = new XLoper();

            x.Type = XLoper.xlTypeNil;

            return(x);
        }
Exemple #14
0
 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);
     }
 }
Exemple #15
0
        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);
        }
Exemple #16
0
 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);
     }
 }
Exemple #17
0
        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);
        }
Exemple #18
0
        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();
                }
            }
        }
Exemple #19
0
        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);
            }
        }
Exemple #20
0
 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;
 }
Exemple #21
0
 private static XLoper DecodeNil()
 {
     XLoper x = new XLoper();
     x.Type = XLoper.xlTypeNil;
     return x;
 }
Exemple #22
0
 private static void EncodeBoolean(XLoper x, Stream s)
 {
     s.WriteByte((byte)(x.Bool ? 1 : 0));
 }
Exemple #23
0
 private static void EncodeError(XLoper x, Stream s)
 {
     WriteDWORD((uint)x.Err, s);
 }
Exemple #24
0
 private static void EncodeInt(XLoper x, Stream s)
 {
     WriteDWORD((uint)x.W, s);
 }
Exemple #25
0
 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;
 }
Exemple #26
0
 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);
     }
 }
Exemple #27
0
 private static void EncodeBoolean(XLoper x, Stream s)
 {
     s.WriteByte((byte) (x.Bool ? 1 : 0));
 }
Exemple #28
0
 private static void EncodeError(XLoper x, Stream s)
 {
     WriteDWORD((uint)x.Err, s);
 }
Exemple #29
0
 private static void EncodeInt(XLoper x, Stream s)
 {
     WriteDWORD((uint) x.W, s);
 }
Exemple #30
0
 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);
     }
 }
Exemple #31
0
 private static void EncodeNum(XLoper x, Stream s)
 {
     long l = BitConverter.DoubleToInt64Bits(x.Num);
     WriteDWORD((uint)(l >> 32), s);
     WriteDWORD((uint)l, s);
 }
Exemple #32
0
 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;
 }
Exemple #33
0
 private static XLoper DecodeMissing()
 {
     XLoper x = new XLoper();
     x.Type = XLoper.xlTypeMissing;
     return x;
 }
Exemple #34
0
        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();
                }
            }
        }