Beispiel #1
0
        static public void CoreRequestToBytes(MemoryByteStream stream, CoreRequest request)
        {
            switch (request.Type)
            {
            case CoreRequest.Types.KeyPress:
                stream.Write(_coreActionKeyPress);
                stream.Write(request.KeyCode);
                stream.Write(request.KeyDown);
                break;

            case CoreRequest.Types.Reset:
                stream.Write(_coreActionReset);
                break;

            case CoreRequest.Types.LoadDisc:
                stream.Write(_coreActionLoadDisc);
                stream.Write(request.Drive);
                stream.WriteArray(request.MediaBuffer.GetBytes());
                break;

            case CoreRequest.Types.LoadTape:
                stream.Write(_coreActionLoadTape);
                stream.WriteArray(request.MediaBuffer.GetBytes());
                break;

            case CoreRequest.Types.RunUntil:
                stream.Write(_coreActionRunUntil);
                stream.Write(request.StopTicks);
                break;

            case CoreRequest.Types.LoadCore:
                stream.Write(_coreActionLoadCore);
                stream.WriteArray(request.CoreState.GetBytes());
                break;

            case CoreRequest.Types.CoreVersion:
                stream.Write(_coreActionCoreVersion);
                stream.Write(request.Version);
                break;

            case CoreRequest.Types.CreateSnapshot:
                stream.Write(_coreActionCreateSnapshot);
                stream.Write(request.SnapshotId);
                break;

            case CoreRequest.Types.DeleteSnapshot:
                stream.Write(_coreActionDeleteSnapshot);
                stream.Write(request.SnapshotId);
                break;

            case CoreRequest.Types.RevertToSnapshot:
                stream.Write(_coreActionRevertToSnapshot);
                stream.Write(request.SnapshotId);
                break;

            default:
                throw new Exception(String.Format("Unknown CoreRequest type {0}!", request.Type));
            }
        }
Beispiel #2
0
        private void OnNewMessage(byte[] msg)
        {
            MemoryByteStream bs = new MemoryByteStream(msg);

            byte id = bs.ReadByte();

            switch (id)
            {
            case _idSelectMachine:
            {
                string machineName = Serializer.SelectMachineFromBytes(bs);
                ReceiveSelectMachine?.Invoke(machineName);
            }
            break;

            case _idAvailableMachines:
            {
                List <string> machines = Serializer.AvailableMachinesFromBytes(bs);
                ReceiveAvailableMachines?.Invoke(machines);
            }
            break;

            case _idCoreAction:
            {
                CoreAction coreAction = Serializer.CoreActionFromBytes(bs);
                ReceiveCoreAction?.Invoke(coreAction);
            }
            break;

            case _idPing:
            {
                bool   response = bs.ReadBool();
                UInt64 pid      = bs.ReadUInt64();
                ReceivePing?.Invoke(response, pid);
            }
            break;

            case _idRequestAvailableMachines:
            {
                ReceiveRequestAvailableMachines?.Invoke();
            }
            break;

            case _idName:
            {
                string machineName = bs.ReadString();
                ReceiveName?.Invoke(machineName);
            }
            break;

            case _idCoreRequest:
            {
                CoreRequest coreRequest = Serializer.CoreRequestFromBytes(bs);
                ReceiveCoreRequest?.Invoke(coreRequest);
            }
            break;
            }
        }
Beispiel #3
0
        public void SendSelectMachine(string machineName)
        {
            MemoryByteStream bs = new MemoryByteStream();

            bs.Write(_idSelectMachine);
            Serializer.SelectMachineToBytes(bs, machineName);

            SendMessage(bs.AsBytes());
        }
Beispiel #4
0
        public void SendCoreAction(CoreAction coreAction)
        {
            MemoryByteStream bs = new MemoryByteStream();

            bs.Write(_idCoreAction);
            Serializer.CoreActionToBytes(bs, coreAction);

            SendMessage(bs.AsBytes());
        }
Beispiel #5
0
        public void SendCoreRequest(CoreRequest coreRequest)
        {
            MemoryByteStream bs = new MemoryByteStream();

            bs.Write(_idCoreRequest);
            Serializer.CoreRequestToBytes(bs, coreRequest);

            SendMessage(bs.AsBytes());
        }
Beispiel #6
0
        public void SendName(string name)
        {
            MemoryByteStream bs = new MemoryByteStream();

            bs.Write(_idName);
            bs.Write(name);

            SendMessage(bs.AsBytes());
        }
Beispiel #7
0
        public void SendAvailableMachines(IEnumerable <string> availableMachines)
        {
            MemoryByteStream bs = new MemoryByteStream();

            bs.Write(_idAvailableMachines);
            Serializer.AvailableMachinesToBytes(bs, availableMachines);

            SendMessage(bs.AsBytes());
        }
Beispiel #8
0
        public void SendPing(bool response, UInt64 id)
        {
            MemoryByteStream bs = new MemoryByteStream();

            bs.Write(_idPing);
            bs.Write(response);
            bs.Write(id);

            SendMessage(bs.AsBytes());
        }
Beispiel #9
0
        static public void AvailableMachinesToBytes(MemoryByteStream stream, IEnumerable <string> machines)
        {
            int count = machines.Count();

            stream.Write(count);

            for (int i = 0; i < count; i++)
            {
                stream.Write(machines.ElementAt(i));
            }
        }
Beispiel #10
0
        static public List <string> AvailableMachinesFromBytes(MemoryByteStream stream)
        {
            List <string> availableMachines = new List <string>();

            int count = stream.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                availableMachines.Add(stream.ReadString());
            }

            return(availableMachines);
        }
Beispiel #11
0
        static public CoreRequest CoreRequestFromBytes(MemoryByteStream stream)
        {
            byte type = stream.ReadByte();

            switch (type)
            {
            case _coreActionKeyPress:
            {
                byte keyCode = stream.ReadByte();
                bool keyDown = stream.ReadBool();

                return(CoreRequest.KeyPress(keyCode, keyDown));
            }

            case _coreActionReset:
            {
                return(CoreRequest.Reset());
            }

            case _coreActionLoadDisc:
            {
                byte   drive = stream.ReadByte();
                byte[] media = stream.ReadArray();

                return(CoreRequest.LoadDisc(drive, media));
            }

            case _coreActionLoadTape:
            {
                byte[] media = stream.ReadArray();

                return(CoreRequest.LoadTape(media));
            }

            case _coreActionRunUntil:
            {
                UInt64 stopTicks = stream.ReadUInt64();

                return(CoreRequest.RunUntil(stopTicks));
            }

            case _coreActionLoadCore:
            {
                byte[] state = stream.ReadArray();

                return(CoreRequest.LoadCore(new MemoryBlob(state)));
            }

            case _coreActionCoreVersion:
            {
                Int32 version = stream.ReadInt32();

                return(CoreRequest.CoreVersion(version));
            }

            case _coreActionCreateSnapshot:
            {
                Int32 snapshotId = stream.ReadInt32();

                return(CoreRequest.CreateSnapshot(snapshotId));
            }

            case _coreActionDeleteSnapshot:
            {
                Int32 snapshotId = stream.ReadInt32();

                return(CoreRequest.DeleteSnapshot(snapshotId));
            }

            case _coreActionRevertToSnapshot:
            {
                Int32 snapshotId = stream.ReadInt32();

                return(CoreRequest.RevertToSnapshot(snapshotId, null));
            }

            default:
                throw new Exception(String.Format("Unknown CoreRequest type {0}!", type));
            }
        }
Beispiel #12
0
        static public string SelectMachineFromBytes(MemoryByteStream stream)
        {
            string machineName = stream.ReadString();

            return(machineName);
        }
Beispiel #13
0
 static public void SelectMachineToBytes(MemoryByteStream stream, string machineName)
 {
     stream.Write(machineName);
 }
Beispiel #14
0
        static public CoreAction CoreActionFromBytes(MemoryByteStream stream)
        {
            byte type = stream.ReadByte();

            switch (type)
            {
            case _coreActionKeyPress:
            {
                UInt64 ticks   = stream.ReadUInt64();
                byte   keyCode = stream.ReadByte();
                bool   keyDown = stream.ReadBool();

                return(CoreAction.KeyPress(ticks, keyCode, keyDown));
            }

            case _coreActionReset:
            {
                UInt64 ticks = stream.ReadUInt64();

                return(CoreAction.Reset(ticks));
            }

            case _coreActionLoadDisc:
            {
                UInt64 ticks = stream.ReadUInt64();
                byte   drive = stream.ReadByte();
                byte[] media = stream.ReadArray();

                return(CoreAction.LoadDisc(ticks, drive, new MemoryBlob(media)));
            }

            case _coreActionLoadTape:
            {
                UInt64 ticks = stream.ReadUInt64();
                byte[] media = stream.ReadArray();

                return(CoreAction.LoadTape(ticks, new MemoryBlob(media)));
            }

            case _coreActionRunUntil:
            {
                UInt64 ticks     = stream.ReadUInt64();
                UInt64 stopTicks = stream.ReadUInt64();

                return(CoreAction.RunUntil(ticks, stopTicks, null));
            }

            case _coreActionLoadCore:
            {
                UInt64 ticks = stream.ReadUInt64();
                byte[] state = stream.ReadArray();

                return(CoreAction.LoadCore(ticks, new MemoryBlob(state)));
            }

            case _coreActionCoreVersion:
            {
                UInt64 ticks   = stream.ReadUInt64();
                Int32  version = stream.ReadInt32();

                return(CoreAction.CoreVersion(ticks, version));
            }

            case _coreActionCreateSnapshot:
            {
                UInt64 ticks      = stream.ReadUInt64();
                Int32  snapshotId = stream.ReadInt32();

                return(CoreAction.CreateSnapshot(ticks, snapshotId));
            }

            case _coreActionDeleteSnapshot:
            {
                UInt64 ticks      = stream.ReadUInt64();
                Int32  snapshotId = stream.ReadInt32();

                return(CoreAction.DeleteSnapshot(ticks, snapshotId));
            }

            case _coreActionRevertToSnapshot:
            {
                UInt64 ticks      = stream.ReadUInt64();
                Int32  snapshotId = stream.ReadInt32();

                return(CoreAction.RevertToSnapshot(ticks, snapshotId));
            }
            }

            throw new Exception(String.Format("Unknown CoreAction type {0}!", type));
        }