コード例 #1
0
ファイル: RPC.cs プロジェクト: parkheeyoung/SlimNet
        internal void Invoke(IEnumerable <Player> players, RPCResult result, string name, params object[] args)
        {
            Network.ByteOutStream stream = new Network.ByteOutStream(256);

            stream.WriteByte(HeaderBytes.RemoteProcedureCall);
            stream.WriteBool(true);
            stream.WriteString(name);

            if (result != null)
            {
                stream.WriteInt(result.ReturnIndex);
                results.Add(result.ReturnIndex, result);
                result.Name = name;
            }

            foreach (object arg in args)
            {
                Writer writer;

                if (!writers.TryGetValue(arg.GetType(), out writer))
                {
                    log.Error("No RPC argument writer for type '{0}', can't send RPC '{1}'", arg.GetTypeName(), name);
                    return;
                }

                writer(stream, arg.GetType(), arg);
            }

            log.Info("Sending {0}", name);

            foreach (Player player in players)
            {
                player.Connection.Queue(stream, true);
            }
        }
コード例 #2
0
ファイル: Connection.cs プロジェクト: parkheeyoung/SlimNet
        public void Send(ByteOutStream header)
        {
            Assert.NotNull(header, "header");

            Context.Peer.NetworkPeer.Send(this, header, reliable, true);
            Context.Peer.NetworkPeer.Send(this, header, unreliable, false);
        }
コード例 #3
0
        void Network.IStreamWriter.WriteToStream(Player player, Network.ByteOutStream stream)
        {
            stream.WriteByte(HeaderBytes.ActorStateStream);
            stream.WriteUShort(Id);
            stream.WriteByte(StateStreamer.Size);

            StateStreamer.Pack(stream);
        }
コード例 #4
0
        void Network.IStreamWriter.WriteToStream(Player player, Network.ByteOutStream stream)
        {
            // Write event id
            stream.WriteByte(EventId);

            // Write handler header
            Handler.WriteEventHeader(this, stream);

            // Write data
            Pack(stream);
        }
コード例 #5
0
ファイル: StateStreamer.cs プロジェクト: parkheeyoung/SlimNet
        public void Pack(Network.ByteOutStream stream)
        {
            Vector3    p;
            Quaternion r;
            byte       angle = 0;

            if (Actor.Context.IsServer)
            {
                p = position;
                r = rotation;

                if (yawOnly)
                {
                    angle = (byte)(yaw * 0.71111f);
                }
            }
            else
            {
                p = Actor.Transform.Position;
                r = Actor.Transform.Rotation;

                if (yawOnly)
                {
                    angle = (byte)(Vector3.SignedAngle(Vector3.Forward, Actor.Transform.Forward, Vector3.Up) * 0.71111f);
                }
            }

            if (compressPosition)
            {
                stream.WriteUShort(HalfUtilities.Pack(p.X));
                stream.WriteUShort(HalfUtilities.Pack(p.Y));
                stream.WriteUShort(HalfUtilities.Pack(p.Z));
            }
            else
            {
                stream.WriteVector3(p);
            }

            if (yawOnly)
            {
                stream.WriteByte(angle);
            }
            else
            {
                p = r.Axis;
                stream.WriteUShort(HalfUtilities.Pack(p.X));
                stream.WriteUShort(HalfUtilities.Pack(p.Y));
                stream.WriteUShort(HalfUtilities.Pack(p.Z));
                stream.WriteByte((byte)(r.Angle * SlimMath.Single.Rad2Deg * 0.71111f));
            }
        }
コード例 #6
0
ファイル: RPC.cs プロジェクト: parkheeyoung/SlimNet
        void writePair(Network.ByteOutStream s, Type t, object v)
        {
            object first  = t.GetProperty("First").GetValue(v, null);
            object second = t.GetProperty("Second").GetValue(v, null);

            Writer firstW;
            Writer secondW;

            if (!writers.TryGetTypeValue(first.GetType(), out firstW) || !writers.TryGetTypeValue(second.GetType(), out secondW))
            {
                return;
            }

            firstW(s, first.GetType(), first);
            secondW(s, second.GetType(), second);
        }
コード例 #7
0
        public void Send(IConnection connection, Network.ByteOutStream header, Queue <Network.IStreamWriter> data, bool reliable)
        {
            if (data.Count == 0)
            {
                return;
            }

            int mtu         = MTU;
            int headerSize  = header.Size;
            int messageSize = mtu - headerSize;

            IStreamWriter     writer        = null;
            NetConnection     netConnection = ((LidgrenConnection)connection).Connection;
            ByteOutStream     stream        = new ByteOutStream(messageSize);
            NetDeliveryMethod delivery      = reliable ? NetDeliveryMethod.ReliableOrdered : NetDeliveryMethod.UnreliableSequenced;

            while (data.Count > 0)
            {
                writer = data.Dequeue();

                if (writer == null)
                {
                    log.Warn("A null value was queued on the outgoing message queue for {0}", connection);
                    continue;
                }

                if (stream.Size + writer.Size > messageSize && stream.Size > 0)
                {
                    sendStream(connection.Player, netConnection, delivery, stream, header, mtu);
                    stream.Reset();
                }

                writer.WriteToStream(connection.Player, stream);
            }

            if (stream.Size > 0)
            {
                sendStream(connection.Player, netConnection, delivery, stream, header, mtu);
            }
        }
コード例 #8
0
ファイル: Context.cs プロジェクト: parkheeyoung/SlimNet
        public bool Send()
        {
            if (NetworkQueue.Count > 0)
            {
                Peer.ContextPlugin.BeforeSend();

                // Copy dirty connections
                HashSet <Network.IConnection> connections = new HashSet <Network.IConnection>(NetworkQueue);
                NetworkQueue.Clear();

                // Make sure we have an up-to-date time
                Time.Update();

                //
                Scheduler.RunExpired();

                // Create message header
                Network.ByteOutStream header = new Network.ByteOutStream(4);
                header.WriteSingle(Time.GameTime);

                // Send
                foreach (Network.IConnection connection in connections)
                {
                    connection.Send(header);
                }

                foreach (Synchronizable sync in SynchronizationQueue)
                {
                    sync.DirtyIndexes = 0;
                }

                SynchronizationQueue.Clear();
                Peer.ContextPlugin.AfterSend();

                return(true);
            }

            return(false);
        }
コード例 #9
0
        internal void PackSyncData(uint indexes, Network.ByteOutStream stream)
        {
            if (indexes == 0 || !IsActive)
            {
                return;
            }

            log.Debug("Sending data for {0}", Actor);

            stream.WriteByte(HeaderBytes.Synchronizable);
            stream.WriteActor(Actor);
            stream.WriteUShort((ushort)DataSize);
            stream.WriteUInt(indexes);

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

            return;
        }
コード例 #10
0
ファイル: ChatMessage.cs プロジェクト: parkheeyoung/SlimNet
 public override void Pack(SlimNet.Network.ByteOutStream stream)
 {
     stream.WriteString(Message);
 }
コード例 #11
0
 public override void Pack(Network.ByteOutStream stream)
 {
     stream.WriteInt((int)(ValueType)Value);
 }
コード例 #12
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);
        }
コード例 #13
0
        void sendStream(Player player, NetConnection connection, NetDeliveryMethod delivery, ByteOutStream data, ByteOutStream header, int mtu)
        {
            // Create message
            NetOutgoingMessage message = Peer.CreateMessage(Math.Max(mtu, data.Size + header.Size));

            // Write header + data
            message.WriteRaw(header.Stream.Data, 0, header.Stream.Size);
            message.WriteRaw(data.Stream.Data, 0, data.Stream.Size);

            // Record status
            player.Stats.AddOutBytes(message.LengthBytes + 2);
            player.Context.Stats.AddOutBytes(message.LengthBytes + 2);

            // Send message
            Peer.SendMessage(message, connection, delivery);
        }
コード例 #14
0
 public void WriteToStream(Player player, Network.ByteOutStream stream)
 {
     PackSyncData(DirtyIndexes, stream);
 }
コード例 #15
0
 public abstract void Pack(Network.ByteOutStream stream);
コード例 #16
0
 public override void Pack(SlimNet.Network.ByteOutStream stream)
 {
     stream.WriteByte(State);
 }
コード例 #17
0
 public void WriteToStream(Player player, Network.ByteOutStream stream)
 {
     synchronizable.PackSyncData(UInt32.MaxValue, stream);
 }
コード例 #18
0
ファイル: Connection.cs プロジェクト: corefan/SlimNet
        public void Send(ByteOutStream header)
        {
            Assert.NotNull(header, "header");

            Context.Peer.NetworkPeer.Send(this, header, reliable, true);
            Context.Peer.NetworkPeer.Send(this, header, unreliable, false);
        }
コード例 #19
0
 internal abstract void WriteEventHeader(Event <T> ev, Network.ByteOutStream stream);