private static void ProcessInput(BinaryReader br) { while (true) { RemotingMessageType type = br.ReadRemotingMessageType(); switch (type) { case RemotingMessageType.GameObject: RemoteGameObjects remoteObjects = InteractionService.RemoteGameObjects.Provide(); remoteObjects.ProcessGameObjectMessages(br); break; default: throw new InvalidDataException(); } } }
//private sealed class MyStream : NetworkStream //{ // private static readonly FileStream _input = new FileStream("sourceFile.bin", FileMode.Create, FileAccess.Write, FileShare.ReadWrite); // private static readonly StreamWriter _inputText = new StreamWriter(new FileStream("sourceFile.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite)); // private static readonly FileStream _output = new FileStream("targetFile.bin", FileMode.Create, FileAccess.Write, FileShare.ReadWrite); // public MyStream([NotNull] Socket socket) : base(socket) // { // } // public MyStream([NotNull] Socket socket, Boolean ownsSocket) : base(socket, ownsSocket) // { // } // public MyStream([NotNull] Socket socket, FileAccess access) : base(socket, access) // { // } // public MyStream([NotNull] Socket socket, FileAccess access, Boolean ownsSocket) : base(socket, access, ownsSocket) // { // } // public override void Write(Byte[] buffer, Int32 offset, Int32 size) // { // _inputText.WriteLine($"Position: {_input.Position}, Stack: {Environment.StackTrace}"); // _inputText.WriteLine("-------------------------------------------------------------"); // _input.Write(buffer, offset, size); // base.Write(buffer, offset, size); // } // public override void WriteByte(Byte value) // { // _inputText.WriteLine($"Position: {_input.Position}, Value: {value} Stack: {Environment.StackTrace}"); // _inputText.WriteLine("-------------------------------------------------------------"); // _input.WriteByte(value); // base.WriteByte(value); // } // public override Int32 Read(Byte[] buffer, Int32 offset, Int32 size) // { // var readed = base.Read(buffer, offset, size); // _output.Write(buffer, offset, readed); // _output.Flush(); // return readed; // } // public override Int32 ReadByte() // { // var b = base.ReadByte(); // if (b != -1) // { // _output.WriteByte(checked((Byte)b)); // _output.Flush(); // } // return b; // } //} private static void ReadData(Socket socket) { try { using (NetworkStream stream = new NetworkStream(socket, FileAccess.Read, false)) { BinaryReader br = new BinaryReader(stream, Encoding.UTF8); do { Log.Message("[GameObjectService] Waiting for a next message..."); RemotingMessageType type = br.ReadRemotingMessageType(); switch (type) { case RemotingMessageType.Command: CommandMessageType commandType = br.ReadCommandMessageType(); Log.Message("[GameObjectService] Deserializing command of type {0}", commandType); CommandMessage command = CreateCommand(commandType); command.Deserialize(br); Log.Message("[GameObjectService] Executing command {0}", commandType); command.Execute(); break; default: throw new NotSupportedException(type.ToString()); } } while (socket.Connected); } } catch (Exception ex) { Log.Error(ex, "Failed to read data from the remote channel."); socket.Dispose(); } }
public static void Write(this BinaryWriter bw, RemotingMessageType messageType) { bw.Write((UInt16)messageType); }