Esempio 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;
            }
        }
Esempio n. 2
0
        public ClientModule(System.Net.IPAddress host, int port, ClientGame parentGame)
        {
            netSession = new Network.ClientSession(0, new System.Net.IPEndPoint(host, port),
                                                   parentGame.Main.ScriptConsole.Write);

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

                    switch (messageCode)
                    {
                    case 0:
                        if (simulation != null)
                        {
                            throw new InvalidProgramException();
                        }
                        {
                            String episodeName;
                            uint   version;
                            gram.ReadString(out episodeName);
                            gram.ReadUInt(out version, 32);
                            parentGame.StartSimulation(episodeName, version);
                        }
                        break;

                    case 1:
                    {
                        UInt32 entityID;
                        UInt32 syncID;
                        UInt32 dataLength;
                        gram.ReadUInt(out entityID, 32);
                        gram.ReadUInt(out syncID, 8);
                        gram.ReadUInt(out dataLength, 32);
                        var bytes = new byte[dataLength];
                        gram.ReadBytes(bytes, dataLength);

                        if (syncables.ContainsKey(entityID))
                        {
                            foreach (var syncable in syncables[entityID])
                            {
                                //if (syncable.SyncID == syncID)
                                syncable.ReadFullSync(new ReadOnlyDatagram(bytes));
                            }
                        }
                    }
                    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);
                        simulation.EnqueueEvent(messageID, messageData);
                    }
                    break;
                    }
                }
            };

            parentGame.Main.Write("Connected to server " + host + " on port " + port + "\n");
        }