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; } }
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)); } }
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)); }