Exemplo n.º 1
0
        void IModule.Update(float elapsedSeconds)
        {
            netSession.update();

            var   rawTickInterval = sim.settings.GetLocalProperty("tick-interval");
            float tickInterval    = 1.0f;

            if (rawTickInterval != null)
            {
                try { tickInterval = Convert.ToSingle(rawTickInterval); }
                catch (Exception) { }
            }
            ;
            if (tickInterval <= 0)
            {
                tickInterval = 1.0f;
            }

            tickSeconds += elapsedSeconds;
            if (tickSeconds > tickInterval)
            {
                tickSeconds -= tickInterval;
                var datagram = new Network.WriteOnlyDatagram();

                foreach (var message in pendingMessages)
                {
                    datagram.WriteUInt(2u, 8);
                    datagram.WriteUInt((uint)message.Length, 32);
                    datagram.WriteBytes(message);
                }
                pendingMessages.Clear();

                foreach (var syncable in syncables)
                {
                    var syncData = new WriteOnlyDatagram();
                    syncable.WriteFullSync(syncData);
                    if (syncData.LengthInBytes > 0)
                    {
                        datagram.WriteUInt(1u, 8);
                        //datagram.WriteUInt(syncable.EntityID, 32);
                        //datagram.WriteUInt(syncable.SyncID, 8);
                        datagram.WriteUInt((uint)syncData.LengthInBytes, 32);
                        datagram.WriteBytes(syncData.BufferAsArray);
                    }
                }

                netSession.broadcastCriticalDatagram(datagram.BufferAsArray);
            }
        }
Exemplo n.º 2
0
        void IModule.BeginSimulation(Simulation sim)
        {
            this.sim = sim;
            sim.sendMessageHandler += (bytes) => { pendingMessages.Add(bytes); };

            try
            {
                netSession = new Network.ServerSession(port, sim.debug);

                netSession.onClientJoined += (client) =>
                {
                    var welcomeDatagram = new Network.WriteOnlyDatagram();
                    welcomeDatagram.WriteUInt(0, 8);
                    welcomeDatagram.WriteString(sim.Content.Module.Name);
                    welcomeDatagram.WriteUInt((uint)sim.Content.Module.Version, 32);
                    netSession.sendCriticalDatagram(client, welcomeDatagram.BufferAsArray, () =>
                    {
                        sim.EnqueueEvent("on-new-client", new MISP.ScriptList(client));
                    });
                };

                netSession.onDatagramReceived += (client, bytes) =>
                {
                    var gram = new Network.ReadOnlyDatagram(bytes);
                    while (gram.More)
                    {
                        uint messageCode = 0;
                        gram.ReadUInt(out messageCode, 8);

                        switch (messageCode)
                        {
                        case 0:
                            //Should never receive this message.
                            break;

                        case 1:
                            //Should never receive this message.
                            break;

                        case 2:
                        {
                            UInt32 length;
                            gram.ReadUInt(out length, 32);
                            byte[] message = new byte[length];
                            gram.ReadBytes(message, length);
                            string          messageID   = null;
                            MISP.ScriptList messageData = null;
                            ScriptMessage.DecodeMessage(message, out messageID, out messageData);
                            sim.EnqueueEvent(messageID, messageData);
                        }
                        break;
                        }
                    }
                };
            }
            catch (Exception e)
            {
                System.Console.WriteLine("While trying to create a server module, " + e.Message);
                throw e;
            }
        }