コード例 #1
0
ファイル: ServerModule.cs プロジェクト: Blecki/GemgineCore
        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
ファイル: ScriptMessage.cs プロジェクト: hvp/Gemgine
 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
        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);
        }
コード例 #4
0
ファイル: ClientSession.cs プロジェクト: Blecki/GemgineCore
        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);
        }
コード例 #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);
        }
コード例 #6
0
ファイル: ClientSession.cs プロジェクト: hvp/Gemgine
 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);
 }
コード例 #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);
        }
コード例 #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);
        }
コード例 #9
0
ファイル: ClientSession.cs プロジェクト: Blecki/GemgineCore
        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);
        }
コード例 #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);
        }
コード例 #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);
        }
コード例 #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);
     };
 }
コード例 #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);
                }
            }
        }
コード例 #14
0
ファイル: ClientSession.cs プロジェクト: hvp/Gemgine
        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);
        }
コード例 #15
0
ファイル: ServerModule.cs プロジェクト: Blecki/GemgineCore
        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);
            }
        }
コード例 #16
0
ファイル: ClientSession.cs プロジェクト: Blecki/GemgineCore
        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);
        }
コード例 #17
0
ファイル: ServerSession.cs プロジェクト: hvp/Gemgine
 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);
 }
コード例 #18
0
ファイル: ScriptMessage.cs プロジェクト: hvp/Gemgine
        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);
                }
            }
        }
コード例 #19
0
ファイル: ClientSession.cs プロジェクト: hvp/Gemgine
 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);
 }
コード例 #20
0
ファイル: ClientModule.cs プロジェクト: Blecki/GemgineCore
 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);
     };
 }
コード例 #21
0
ファイル: ServerSession.cs プロジェクト: hvp/Gemgine
 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);
 }
コード例 #22
0
ファイル: ServerSession.cs プロジェクト: hvp/Gemgine
 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);
 }
コード例 #23
0
ファイル: ServerSession.cs プロジェクト: hvp/Gemgine
 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);
 }
コード例 #24
0
ファイル: ServerSession.cs プロジェクト: hvp/Gemgine
 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);
 }
コード例 #25
0
ファイル: ServerModule.cs プロジェクト: Blecki/GemgineCore
        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);
            }
        }
コード例 #26
0
ファイル: ScriptMessage.cs プロジェクト: Blecki/GemgineCore
        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);
                }
            }
        }