Ejemplo n.º 1
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;
            }
        }
Ejemplo n.º 2
0
 public static byte[] EncodeMessage(String ID, MISP.ScriptList Data)
 {
     Initialize();
     var datagram = new WriteOnlyDatagram();
     datagram.WriteString(ID);
     EncodeList(Data, datagram);
     return datagram.BufferAsArray;
 }
Ejemplo n.º 3
0
        private void _sendAckMessage(Client to, uint ackID)
        {
            var ackDatagram = new WriteOnlyDatagram();

            ackDatagram.WriteUInt((uint)ServerToClientMessage.Acknowledge, 8);
            ackDatagram.WriteUInt(ackID, 32);
            _send(ackDatagram.BufferAsArray, to);
        }
Ejemplo n.º 4
0
        public void sendDatagram(byte[] data)
        {
            var datagram = new WriteOnlyDatagram();

            datagram.WriteUInt((uint)ClientToServerMessage.Datagram, 8);
            datagram.WriteUInt(0, 32);
            datagram.WriteBytes(data);
            _send(datagram.BufferAsArray);
        }
Ejemplo n.º 5
0
        private void _sendKeepalive(Client to)
        {
            var ackID    = nextAckID++;
            var datagram = new WriteOnlyDatagram();

            datagram.WriteUInt((uint)ServerToClientMessage.Keepalive, 8);
            datagram.WriteUInt((uint)ackID, 32);
            _sendCriticalDatagram(to, datagram.BufferAsArray, ackID);
        }
Ejemplo n.º 6
0
 public void sendCriticalDatagram(byte[] data)
 {
     var ackID = nextAckID++;
     var datagram = new WriteOnlyDatagram();
     datagram.WriteUInt((uint)ClientToServerMessage.Datagram, 8);
     datagram.WriteUInt(ackID, 32);
     datagram.WriteBytes(data);
     _sendCriticalDatagram(datagram.BufferAsArray, ackID);
 }
Ejemplo n.º 7
0
        public void sendDatagram(Client to, byte[] data)
        {
            var datagram = new WriteOnlyDatagram();

            datagram.WriteUInt((uint)ServerToClientMessage.Datagram, 8);
            datagram.WriteUInt(0, 32);
            datagram.WriteBytes(data);
            _send(datagram.BufferAsArray, to);
        }
Ejemplo n.º 8
0
        public static byte[] EncodeMessage(String ID, Common.ObjectList Data)
        {
            Initialize();
            var datagram = new WriteOnlyDatagram();

            datagram.WriteString(ID);
            EncodeList(Data, datagram);
            return(datagram.BufferAsArray);
        }
Ejemplo n.º 9
0
        public void sendCriticalDatagram(byte[] data)
        {
            var ackID    = nextAckID++;
            var datagram = new WriteOnlyDatagram();

            datagram.WriteUInt((uint)ClientToServerMessage.Datagram, 8);
            datagram.WriteUInt(ackID, 32);
            datagram.WriteBytes(data);
            _sendCriticalDatagram(datagram.BufferAsArray, ackID);
        }
Ejemplo n.º 10
0
        public void sendCriticalDatagram(Client to, byte[] data, Action onSuccess = null)
        {
            var ackID    = nextAckID++;
            var datagram = new WriteOnlyDatagram();

            datagram.WriteUInt((uint)ServerToClientMessage.Datagram, 8);
            datagram.WriteUInt(ackID, 32);
            datagram.WriteBytes(data);
            _sendCriticalDatagram(to, datagram.BufferAsArray, ackID, onSuccess);
        }
Ejemplo n.º 11
0
        private void _sendPeerJoinedMessage(Client to, Client about)
        {
            var ackID    = nextAckID++;
            var datagram = new WriteOnlyDatagram();

            datagram.WriteUInt((uint)ServerToClientMessage.PeerJoined, 8);
            datagram.WriteUInt((uint)ackID, 32);
            datagram.WriteBytes(about.Guid.ToByteArray());
            _sendCriticalDatagram(to, datagram.BufferAsArray, ackID);
        }
Ejemplo n.º 12
0
 void IModule.BeginSimulation(Simulation sim)
 {
     this.simulation = sim;
     simulation.sendMessageHandler += (bytes) => {
         var datagram = new WriteOnlyDatagram();
         datagram.WriteUInt(2u, 8);
         datagram.WriteUInt((uint)bytes.Length, 32);
         datagram.WriteBytes(bytes);
         netSession.sendCriticalDatagram(datagram.BufferAsArray);
     };
 }
Ejemplo n.º 13
0
        private static void EncodeList(Common.ObjectList data, WriteOnlyDatagram datagram)
        {
            datagram.WriteUInt((uint)data.Count, 8);
            foreach (var item in data)
            {
                if (item == null)
                {
                    datagram.WriteUInt((uint)ScriptTypes.Bool, 8);
                    datagram.WriteUInt(0u, 8);
                    continue;
                }

                var type = item.GetType();
                if (typeCodes.ContainsKey(type))
                {
                    var typeCode = typeCodes[type];
                    datagram.WriteUInt((uint)typeCode, 8);
                    switch (typeCode)
                    {
                    case ScriptTypes.List:
                        EncodeList(item as Common.ObjectList, datagram);
                        break;

                    case ScriptTypes.String:
                        datagram.WriteString(item as String);
                        break;

                    case ScriptTypes.Int32:
                        datagram.WriteBytes(BitConverter.GetBytes((item as int?).Value));
                        break;

                    case ScriptTypes.UInt32:
                        datagram.WriteBytes(BitConverter.GetBytes((item as uint?).Value));
                        break;

                    case ScriptTypes.Single:
                        datagram.WriteBytes(BitConverter.GetBytes((item as float?).Value));
                        break;

                    case ScriptTypes.Bool:
                        datagram.WriteUInt((item as bool?).Value ? 1u : 0u, 8);
                        break;

                    default:
                        throw new InvalidProgramException("Error encoding message", null);
                    }
                }
                else
                {
                    throw new InvalidProgramException("Type " + type.Name + " cannot be serialized to a network message.", null);
                }
            }
        }
Ejemplo n.º 14
0
        public ClientSession(int LocalPort, IPEndPoint serverAddress, Action<String> debugOutput)
        {
            this.debugOutput = debugOutput;

            socket = new UdpClient(LocalPort, AddressFamily.InterNetwork);
            socket.DontFragment = true;
            this.serverAddress = serverAddress;

            var datagram = new WriteOnlyDatagram();
            datagram.WriteUInt((uint)ClientToServerMessage.Join, 8);
            var ackID = nextAckID++;
            datagram.WriteUInt(ackID, 32);
            _sendCriticalDatagram(datagram.BufferAsArray, ackID);
        }
Ejemplo n.º 15
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);
            }
        }
Ejemplo n.º 16
0
        public ClientSession(int LocalPort, IPEndPoint serverAddress, Action <String> debugOutput)
        {
            this.debugOutput = debugOutput;

            socket = new UdpClient(LocalPort, AddressFamily.InterNetwork);
            socket.DontFragment = true;
            this.serverAddress  = serverAddress;

            var datagram = new WriteOnlyDatagram();

            datagram.WriteUInt((uint)ClientToServerMessage.Join, 8);
            var ackID = nextAckID++;

            datagram.WriteUInt(ackID, 32);
            _sendCriticalDatagram(datagram.BufferAsArray, ackID);
        }
Ejemplo n.º 17
0
 private void _sendPeerLeftMessage(Client to, Client about)
 {
     var ackID = nextAckID++;
     var datagram = new WriteOnlyDatagram();
     datagram.WriteUInt((uint)ServerToClientMessage.PeerLeft, 8);
     datagram.WriteUInt((uint)ackID, 32);
     datagram.WriteBytes(about.Guid.ToByteArray());
     _sendCriticalDatagram(to, datagram.BufferAsArray, ackID);
 }
Ejemplo n.º 18
0
        private static void EncodeList(MISP.ScriptList data, WriteOnlyDatagram datagram)
        {
            datagram.WriteUInt((uint)data.Count, 8);
            foreach (var item in data)
            {
                if (item == null)
                {
                    datagram.WriteUInt((uint)ScriptTypes.Bool, 8);
                    datagram.WriteUInt(0u, 8);
                    continue;
                }

                var type = item.GetType();
                if (typeCodes.ContainsKey(type))
                {
                    var typeCode = typeCodes[type];
                    datagram.WriteUInt((uint)typeCode, 8);
                    switch (typeCode)
                    {
                        case ScriptTypes.List:
                            EncodeList(item as MISP.ScriptList, datagram);
                            break;
                        case ScriptTypes.String:
                            datagram.WriteString(item as String);
                            break;
                        case ScriptTypes.Int32:
                            datagram.WriteBytes(BitConverter.GetBytes(MISP.AutoBind.IntArgument(item)));
                            break;
                        case ScriptTypes.UInt32:
                            datagram.WriteBytes(BitConverter.GetBytes(MISP.AutoBind.UIntArgument(item)));
                            break;
                        case ScriptTypes.Single:
                            datagram.WriteBytes(BitConverter.GetBytes(MISP.AutoBind.NumericArgument(item)));
                            break;
                        case ScriptTypes.Bool:
                            datagram.WriteUInt(MISP.AutoBind.BooleanArgument(item) ? 1u : 0u, 8);
                            break;
                        default:
                            throw new MISP.ScriptError("Error encoding message", null);
                    }
                }
                else
                {
                    throw new MISP.ScriptError("Type " + type.Name + " cannot be serialized to a network message.", null);
                }
            }
        }
Ejemplo n.º 19
0
 public void sendDatagram(byte[] data)
 {
     var datagram = new WriteOnlyDatagram();
     datagram.WriteUInt((uint)ClientToServerMessage.Datagram, 8);
     datagram.WriteUInt(0, 32);
     datagram.WriteBytes(data);
     _send(datagram.BufferAsArray);
 }
Ejemplo n.º 20
0
 void IModule.BeginSimulation(Simulation sim)
 {
     this.simulation = sim;
     simulation.sendMessageHandler += (bytes) => {
         var datagram = new WriteOnlyDatagram();
         datagram.WriteUInt(2u, 8);
         datagram.WriteUInt((uint)bytes.Length, 32);
         datagram.WriteBytes(bytes);
         netSession.sendCriticalDatagram(datagram.BufferAsArray);
     };
 }
Ejemplo n.º 21
0
 public void sendCriticalDatagram(Client to, byte[] data, Action onSuccess = null)
 {
     var ackID = nextAckID++;
     var datagram = new WriteOnlyDatagram();
     datagram.WriteUInt((uint)ServerToClientMessage.Datagram, 8);
     datagram.WriteUInt(ackID, 32);
     datagram.WriteBytes(data);
     _sendCriticalDatagram(to, datagram.BufferAsArray, ackID, onSuccess);
 }
Ejemplo n.º 22
0
 public void sendDatagram(Client to, byte[] data)
 {
     var datagram = new WriteOnlyDatagram();
     datagram.WriteUInt((uint)ServerToClientMessage.Datagram, 8);
     datagram.WriteUInt(0, 32);
     datagram.WriteBytes(data);
     _send(datagram.BufferAsArray, to);
 }
Ejemplo n.º 23
0
 private void _sendAckMessage(Client to, uint ackID)
 {
     var  ackDatagram = new WriteOnlyDatagram();
     ackDatagram.WriteUInt((uint)ServerToClientMessage.Acknowledge, 8);
     ackDatagram.WriteUInt(ackID, 32);
     _send(ackDatagram.BufferAsArray, to);
 }
Ejemplo n.º 24
0
 private void _sendKeepalive(Client to)
 {
     var ackID = nextAckID++;
     var datagram = new WriteOnlyDatagram();
     datagram.WriteUInt((uint)ServerToClientMessage.Keepalive, 8);
     datagram.WriteUInt((uint)ackID, 32);
     _sendCriticalDatagram(to, datagram.BufferAsArray, ackID);
 }
Ejemplo n.º 25
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);
            }
        }
Ejemplo n.º 26
0
        private static void EncodeList(Common.ObjectList data, WriteOnlyDatagram datagram)
        {
            datagram.WriteUInt((uint)data.Count, 8);
            foreach (var item in data)
            {
                if (item == null)
                {
                    datagram.WriteUInt((uint)ScriptTypes.Bool, 8);
                    datagram.WriteUInt(0u, 8);
                    continue;
                }

                var type = item.GetType();
                if (typeCodes.ContainsKey(type))
                {
                    var typeCode = typeCodes[type];
                    datagram.WriteUInt((uint)typeCode, 8);
                    switch (typeCode)
                    {
                        case ScriptTypes.List:
                            EncodeList(item as Common.ObjectList, datagram);
                            break;
                        case ScriptTypes.String:
                            datagram.WriteString(item as String);
                            break;
                        case ScriptTypes.Int32:
                            datagram.WriteBytes(BitConverter.GetBytes((item as int?).Value));
                            break;
                        case ScriptTypes.UInt32:
                            datagram.WriteBytes(BitConverter.GetBytes((item as uint?).Value));
                            break;
                        case ScriptTypes.Single:
                            datagram.WriteBytes(BitConverter.GetBytes((item as float?).Value));
                            break;
                        case ScriptTypes.Bool:
                            datagram.WriteUInt((item as bool?).Value ? 1u : 0u, 8);
                            break;
                        default:
                            throw new InvalidProgramException("Error encoding message", null);
                    }
                }
                else
                {
                    throw new InvalidProgramException("Type " + type.Name + " cannot be serialized to a network message.", null);
                }
            }
        }