public void SendCommand(CommandMessageType type, string message) { var str = $"{(int)type}|{message}|"; var package = Encoding.UTF8.GetBytes(str); _stream.Write(package, 0, package.Length); }
private static CommandMessage CreateCommand(CommandMessageType type) { switch (type) { case CommandMessageType.ChangeValue: return(new ChangeValueCommandMessage()); case CommandMessageType.ChangeReference: return(new ChangeReferenceCommandMessage()); case CommandMessageType.Duplicate: return(new DuplicateCommandMessage()); default: throw new NotSupportedException(type.ToString()); } }
//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, CommandMessageType messageType) { bw.Write((UInt16)messageType); }
public CommandMessage(CommandMessageType messageType) { MessageType = messageType; }
//public SimpleCommandMessage(string message) // : this(message, CommandMessageType.Information) //{ //} public SimpleCommandMessage(string message, CommandMessageType type) { Contract.Assert(message != null); Message = message; Type = type; }