Esempio n. 1
0
        public static void Test()
        {
            RPCDispatcher dispatch = new RPCDispatcher();

            dispatch.Functions[typeof(Serialization.TestStruct)] = (RPCData data) =>
            {
                Serialization.TestStruct obj = (Serialization.TestStruct)data.DeserializedObject;
                Console.WriteLine("RPC Test Successful.");
            };

            // add a rpc call
            Serialization.TestStruct serTest = new Serialization.TestStruct();
            serTest.a = 10;
            serTest.b = 20;
            serTest.c = "hello";
            MemoryStream memStream = new MemoryStream(100);

            RPCStream.AddCall(memStream, serTest);

            // execute rpc calls
            MemoryStream memStreamIn = new MemoryStream(memStream.ToArray());

            RPCData template = new RPCData();

            Stopwatch st = new Stopwatch();

            st.Start();
            for (int i = 0; i < 100000; i++)
            {
                memStreamIn.Position = 0;
                try
                {
                    RPCStream.Execute(memStreamIn, dispatch, template);
                }
                catch
                {
                }
            }
            st.Stop();
            Console.WriteLine("RPC performance: " + st.ElapsedMilliseconds);
        }
Esempio n. 2
0
        public NetWrapOutgoingMessage CreateRPCPacket(params object[] objs)
        {
            NetWrapOutgoingMessage msg    = new NetWrapOutgoingMessage();
            List <byte>            packet = new List <byte>();

            packet.AddRange(RPCHeader);
            packet.Add(255);

            int count = 0;

            foreach (var obj in objs)
            {
                var stream = new MemoryStream(2048);
                RPCStream.AddCall(stream, obj);
                var array = stream.ToArray();
                if (array.Length > 512)
                {
                    packet.AddRange(BitConverter.GetBytes(-array.Length));
                    packet.AddRange(NetCompressByteArray.Wrap(array));
                }
                else
                {
                    packet.AddRange(BitConverter.GetBytes(array.Length));
                    packet.AddRange(array);
                }
                count++;
            }

            // set count to correct count (255 -> count)
            packet[4] = (byte)count;

            msg.Message       = packet.ToArray();
            msg.MessageCount  = packet.Count;
            msg.MessageOffset = 0;
            return(msg);
        }