Пример #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;
            }
        }
Пример #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;
 }
Пример #3
0
        public static byte[] EncodeMessage(String ID, Common.ObjectList Data)
        {
            Initialize();
            var datagram = new WriteOnlyDatagram();

            datagram.WriteString(ID);
            EncodeList(Data, datagram);
            return(datagram.BufferAsArray);
        }
Пример #4
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);
                }
            }
        }
Пример #5
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);
                }
            }
        }
Пример #6
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);
                }
            }
        }