예제 #1
0
파일: RPC.cs 프로젝트: parkheeyoung/SlimNet
        bool onReturn(Network.ByteInStream stream)
        {
            int returnId = stream.ReadInt();

            RPCResult result;

            if (!results.TryGetValue(returnId, out result))
            {
                log.Error("No RPC result with the return index #{0} pending", returnId);
                return(false);
            }

            if (stream.ReadBool())
            {
                Reader reader;

                if (!readers.TryGetTypeValue(result.Type, out reader))
                {
                    return(false);
                }

                result.BoxedValue = reader(stream, result.Type);
            }

            log.Info("Received result for {0}", result.Name);

            result.IsDone = true;
            result.Done();

            results.Remove(result.ReturnIndex);

            return(true);
        }
예제 #2
0
        internal void OnNetworkStream(Network.ByteInStream stream)
        {
#if !DEBUG
            try
            {
#endif
            while (stream.Stream.CanRead)
            {
                byte           handlerId = stream.ReadByte();
                IPacketHandler handler   = packetHandlers[handlerId];

                if (handler == null)
                {
                    log.Error("No packet handler for id #{0} found, can't process byte stream", handlerId);
                    return;
                }

                if (!handler.OnPacket(handlerId, this, stream))
                {
                    log.Error("Handler '{0}' failed processing byte stream", handler.GetType().GetPrettyName());
                    return;
                }
            }
#if !DEBUG
        }

        catch (Exception exn)
        {
            log.Error(exn);
        }
#endif
        }
예제 #3
0
파일: RPC.cs 프로젝트: parkheeyoung/SlimNet
 public bool OnPacket(byte _, Context context, Network.ByteInStream stream)
 {
     if (stream.ReadBool())
     {
         return(onCall(stream));
     }
     else
     {
         return(onReturn(stream));
     }
 }
예제 #4
0
파일: RPC.cs 프로젝트: parkheeyoung/SlimNet
        object readActor(Network.ByteInStream s, Type t)
        {
            Actor  a;
            ushort id = s.ReadUShort();

            if (Context.GetActor(id, out a))
            {
                return(a);
            }

            log.Warn("No actor with id #{0} found", id);
            return(null);
        }
예제 #5
0
파일: RPC.cs 프로젝트: parkheeyoung/SlimNet
        object readPlayer(Network.ByteInStream s, Type t)
        {
            Player p;
            ushort id = s.ReadUShort();

            if (Context.GetPlayer(id, out p))
            {
                return(p);
            }

            log.Warn("No player with id #{0} found", id);
            return(null);
        }
예제 #6
0
        bool IPacketHandler.OnPacket(byte eventId, Context context, Network.ByteInStream stream)
        {
            EventDescriptor <T> descriptor = descriptors[eventId];
            T target = ReadEventHeader(stream);

            // Verify event target object

            if (target == null)
            {
                log.Error("Failed to resolve target for event #{0}", eventId);
                return(false);
            }

            // Unpack event
            Event <T> ev = descriptor.Create(target);

            ev.Source         = stream.Player;
            ev.SourceGameTime = stream.RemoteGameTime;
            ev.Unpack(stream);

            // Increase stats counter
            Context.Stats.AddInEvent();

            // Special logic depending on
            // if we're a server or client
            if (context.IsServer)
            {
                EventSources s = ev.AllowedSources;

                bool raisedByOwner = IsSourceTargetOwner(ev);
                bool isOwnerValid  = raisedByOwner && (s & EventSources.Owner) == EventSources.Owner;
                bool isRemoteValid = !raisedByOwner && (s & EventSources.Remotes) == EventSources.Remotes;

                if (isOwnerValid || isRemoteValid)
                {
                    raiseEventOnServer(ev);
                }
                else
                {
                    log.Warn("Event {0} was remotely raised by {1}, but that player is not allowed to raise events of that type on target {2}", ev, ev.Source, ev.Target);
                }
            }
            else
            {
                QueueOnClient(ev);
            }

            // Everythings fine, return true
            return(true);
        }
예제 #7
0
        void onDataMessage(NetIncomingMessage message)
        {
            if (message.SenderConnection.Tag == null)
            {
                log.Error("Received data message from a connection without a tag, unabled to process data");
                return;
            }
            ByteInStream stream = new ByteInStream(message.PeekDataBuffer(), message.LengthBytes, 0);

            stream.Connection     = (IConnection)message.SenderConnection.Tag;
            stream.RemoteGameTime = stream.ReadSingle();

            Receiver.Context.Stats.AddInBytes(message.LengthBytes);
            Receiver.OnDataMessage(stream);
        }
예제 #8
0
파일: RPC.cs 프로젝트: parkheeyoung/SlimNet
        object readPair(Network.ByteInStream s, Type t)
        {
            Type[] tArgs = t.GetGenericArgs();

            Reader r;

            if (!readers.TryGetTypeValue(tArgs[0], out r))
            {
                return(null);
            }

            object first = r(s, tArgs[0]);

            if (!readers.TryGetTypeValue(tArgs[1], out r))
            {
                return(null);
            }

            object second = r(s, tArgs[1]);

            return(Activator.CreateInstance(t, first, second));
        }
예제 #9
0
        public void Unpack(Network.ByteInStream stream)
        {
            if (compressPosition)
            {
                position = new Vector3(
                    HalfUtilities.Unpack(stream.ReadUShort()),
                    HalfUtilities.Unpack(stream.ReadUShort()),
                    HalfUtilities.Unpack(stream.ReadUShort())
                    );
            }
            else
            {
                position = stream.ReadVector3();
            }

            if (yawOnly)
            {
                yaw      = stream.ReadByte() * 1.40625f;
                rotation = Quaternion.RotationAxis(Vector3.Up, yaw * SlimMath.Single.Deg2Rad);
            }
            else
            {
                rotation = Quaternion.RotationAxis(
                    new Vector3(
                        HalfUtilities.Unpack(stream.ReadUShort()),
                        HalfUtilities.Unpack(stream.ReadUShort()),
                        HalfUtilities.Unpack(stream.ReadUShort())
                        ),
                    (stream.ReadByte() * 1.40625f * SlimMath.Single.Deg2Rad)
                    );
            }

            buffer.Push(Actor.Context.Time.GameTime,
                        new State {
                Position = position, Rotation = rotation
            }
                        );
        }
예제 #10
0
        internal bool UnpackSyncData(Network.ByteInStream stream)
        {
            uint indexes = stream.ReadUInt();

            if (indexes == UInt32.MaxValue)
            {
                log.Debug("Received full sync for {0}", Actor);
            }
            else
            {
                log.Debug("Received delta sync for {0}", Actor);
            }

            for (int i = 0; i < Values.Length; ++i)
            {
                if ((indexes & ((uint)1 << i)) != 0)
                {
                    Values[i].Unpack(stream);
                }
            }

            return(true);
        }
예제 #11
0
파일: RPC.cs 프로젝트: parkheeyoung/SlimNet
        bool onCall(Network.ByteInStream stream)
        {
            string name = stream.ReadString();

            Receiver receiver;

            if (!receivers.TryGetValue(name, out receiver))
            {
                log.Error("No receiver for '{0}' found", name);
                return(false);
            }

            int returnIndex = 0;
            int length      = receiver.Parameters.Length;

            if (receiver.ReturnsValue)
            {
                returnIndex = stream.ReadInt();
            }

            if (receiver.RequiresInfoArgument)
            {
                length += 1;
            }

            object[] args = new object[length];

            for (var i = 0; i < receiver.Parameters.Length; ++i)
            {
                Type   t = receiver.Parameters[i];
                Reader reader;

                if (!readers.TryGetTypeValue(t, out reader))
                {
                    return(false);
                }

                args[i] = reader(stream, t);

                if (args[i] is Player)
                {
                    if (!ReferenceEquals(args[i], stream.Player))
                    {
                        log.Error("RPC from {0} tried to use object for {1}", stream.Player, args[i]);
                        return(false);
                    }
                }
            }

            if (receiver.RequiresInfoArgument)
            {
                RPCInfo info = new RPCInfo();

                info.Caller         = stream.Player;
                info.Context        = Context;
                info.RemoteGameTime = stream.RemoteGameTime;

                args[args.Length - 1] = info;
            }

            log.Info("Received {0}", receiver.Name);

            if (receiver.ReturnsValue)
            {
                object result = receiver.Method.Invoke(receiver.Instance, args);
                Network.ByteOutStream resultStream = new Network.ByteOutStream(128);

                resultStream.WriteByte(HeaderBytes.RemoteProcedureCall);
                resultStream.WriteBool(false);
                resultStream.WriteInt(returnIndex);

                if (result == null)
                {
                    resultStream.WriteBool(false);
                }
                else
                {
                    Writer writer;
                    Type   t = result.GetType();

                    resultStream.WriteBool(true);

                    if (writers.TryGetTypeValue(t, out writer))
                    {
                        writer(resultStream, t, result);
                    }
                }

                stream.Player.Connection.Queue(resultStream, true);
            }
            else
            {
                receiver.Method.Invoke(receiver.Instance, args);
            }

            return(true);
        }
예제 #12
0
파일: Peer.cs 프로젝트: corefan/SlimNet
 public abstract void OnDataMessage(ByteInStream stream);
예제 #13
0
 public override void OnDataMessage(Network.ByteInStream stream)
 {
     Context.Time.UpdateOffset(stream.RemoteGameTime);
     Context.OnNetworkStream(stream);
 }
예제 #14
0
 public override void OnDataMessage(Network.ByteInStream stream)
 {
     Context.Time.Update();
     Context.OnNetworkStream(stream);
 }
예제 #15
0
 internal abstract T ReadEventHeader(Network.ByteInStream stream);
예제 #16
0
 protected override TEnum UnpackValue(Network.ByteInStream stream)
 {
     return((TEnum)Enum.ToObject(typeof(TEnum), stream.ReadInt()));
 }
예제 #17
0
 public abstract void Unpack(Network.ByteInStream stream);
예제 #18
0
 public override void Unpack(SlimNet.Network.ByteInStream stream)
 {
     Message = stream.ReadString();
 }