Ejemplo n.º 1
0
        public void SetRemote(string name, object value, bool tcp)
        {
            if (IsClient)
            {
                // if(HasAuthority) {
                lock (Client.Out) {
                    Client.Current.WriteHeader(NetworkData.UpdateField);
                    Client.Out.WriteLong(NetworkId);
                    Client.Out.WriteBool(tcp);
                    Client.Out.WriteString(name);
                    Client.Out.WriteByte(NetworkData.GetObjectType(value));
                    NetworkData.WriteObject(value, Client.Out);
                    if (tcp)
                    {
                        Client.Current.WriteTcp();
                    }
                    else
                    {
                        Client.Current.WriteUdp();
                    }
                }

                /* } else {
                 *  throw new System.InvalidOperationException("Cannot set field without authority");
                 * } */
            }
            else if (IsServer)
            {
                foreach (Socket client in ServerRegistry.Clients.Values)
                {
                    lock (client.Buffer) {
                        client.Buffer.WriteByte(NetworkData.Data);
                        client.Buffer.WriteByte(NetworkData.UpdateField);
                        client.Buffer.WriteLong(NetworkId);
                        client.Buffer.WriteString(name);
                        client.Buffer.WriteByte(NetworkData.GetObjectType(value));
                        NetworkData.WriteObject(value, client.Buffer);
                        if (tcp)
                        {
                            client.WriteBufferTcp();
                        }
                        else
                        {
                            client.WriteBufferUdp();
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void InvokeRemote(string name, params object[] args)
        {
            if (IsClient)
            {
                // if(HasAuthority) {
                lock (Client.Out) {
                    Client.Current.WriteHeader(NetworkData.InvokeRPC);
                    Client.Out.WriteLong(NetworkId);
                    Client.Out.WriteString(name);
                    Client.Out.WriteByte((byte)args.Length);
                    foreach (object arg in args)
                    {
                        Client.Out.WriteByte(NetworkData.GetObjectType(arg));
                        NetworkData.WriteObject(arg, Client.Out);
                    }
                    Client.Current.WriteTcp();
                }

                /*} else {
                 *  throw new System.InvalidOperationException("Cannot invoke RPC without authority: " + name);
                 * }*/
            }
            else if (IsServer)
            {
                foreach (Socket client in ServerRegistry.Clients.Values)
                {
                    lock (client.Buffer) {
                        client.Buffer.WriteByte(NetworkData.Data);
                        client.Buffer.WriteByte(NetworkData.InvokeRPC);
                        client.Buffer.WriteLong(NetworkId);
                        client.Buffer.WriteString(name);
                        client.Buffer.WriteByte((byte)args.Length);
                        foreach (object arg in args)
                        {
                            client.Buffer.WriteByte(NetworkData.GetObjectType(arg));
                            NetworkData.WriteObject(arg, client.Buffer);
                        }
                        client.WriteBufferTcp();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static void Query(string data, QueryCallback then, params object[] args)
        {
            if (!Side.IsClient)
            {
                throw new InvalidOperationException("Cannot call client-side functions when not on the client");
            }

            Queries[QueryNonce] = then;
            Debug.Log("Querying data: " + data + "...");

            lock (Out) {
                Out.WriteByte(NetworkData.QueryDatabase);
                Out.WriteLong(QueryNonce++);
                Out.WriteString(data);
                Out.WriteByte((byte)args.Length);
                foreach (object arg in args)
                {
                    Out.WriteByte(NetworkData.GetObjectType(arg));
                    NetworkData.WriteObject(arg, Out);
                }

                Current.WriteTcp();
            }
        }
Ejemplo n.º 4
0
        public static byte[] Serialize(object obj)
        {
            if (obj == null)
            {
                return(new byte[0]);
            }
            if (obj is ISerializer)
            {
                ISerializer s      = (ISerializer)obj;
                byte[]      serial = s.Serialize();
                byte[]      bytes  = new byte[serial.Length + 2];
                bytes[0] = 1;
                bytes[1] = s.GetSerializerId();
                Array.Copy(serial, 0, bytes, 2, serial.Length);
                return(bytes);
            }
            else if (obj.GetType().IsArray)
            {
                object[] list = (object[])obj;

                ByteBuffer buffer = new ByteBuffer(1024);
                buffer.WriteByte(2);
                buffer.WriteInt(list.Length);
                foreach (object item in list)
                {
                    if (item == null)
                    {
                        buffer.WriteInt(0);
                    }
                    else
                    {
                        byte[] serial = Serialize(item);
                        buffer.WriteInt(serial.Length);
                        buffer.WriteBytes(serial);
                    }
                }

                buffer.Flip();

                return(buffer.Bytes);
            }
            else if (obj is IList)
            {
                IList list = (IList)obj;

                ByteBuffer buffer = new ByteBuffer(1024);
                buffer.WriteByte(3);
                buffer.WriteInt(list.Count);
                foreach (object item in list)
                {
                    if (item == null)
                    {
                        buffer.WriteInt(0);
                    }
                    else
                    {
                        byte[] serial = Serialize(item);
                        buffer.WriteInt(serial.Length);
                        buffer.WriteBytes(serial);
                    }
                }

                buffer.Flip();

                return(buffer.Bytes);
            }
            else if (obj.GetType().IsPrimitive || obj is Vector3 || obj is Quaternion)
            {
                ByteBuffer buf = new ByteBuffer(18); // largest possible object here is a Quaternion, which will be header byte + object type byte + 16 data bytes
                buf.WriteByte(4);
                buf.WriteByte(NetworkData.GetObjectType(obj));
                NetworkData.WriteObject(obj, buf);

                buf.Flip();

                return(buf);
            }

            MemoryStream stream = new MemoryStream();

            Formatter.Serialize(stream, obj);

            return(stream.ToArray());
        }