Пример #1
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);
                }
            }
        }
Пример #2
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);
        }
Пример #3
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);
 }
Пример #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);
        }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
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);
        }
Пример #8
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);
     };
 }
Пример #9
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);
     };
 }
Пример #10
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);
            }
        }
Пример #11
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);
 }
Пример #12
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);
 }
Пример #13
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);
 }
Пример #14
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);
 }
Пример #15
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);
                }
            }
        }
Пример #16
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);
                }
            }
        }