public bool SendCmdRequest(CmdRequest request) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect("127.0.0.1", Port); using (NetworkStream stream = new NetworkStream(socket)) { using (BsonWriter writer = new BsonWriter(stream)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(writer, request); writer.Flush(); writer.Close(); } stream.Close(); } } catch(Exception ex) { Logger.AddError(string.Format("Failed to send cmd request to checker with port = {0}", Port), ex); return false; } return true; }
protected virtual byte[] Serialize(object obj) { using (var ms = new MemoryStream()) { var bw = new BsonWriter(ms); Serializer.Serialize(bw, obj); bw.Close(); return ms.ToArray(); } }
public void CloseOutput() { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); Assert.IsTrue(ms.CanRead); writer.Close(); Assert.IsFalse(ms.CanRead); ms = new MemoryStream(); writer = new BsonWriter(ms) { CloseOutput = false }; Assert.IsTrue(ms.CanRead); writer.Close(); Assert.IsTrue(ms.CanRead); }
private void WriteReply(NetworkStream stream, CmdServerReply reply) { JsonSerializer serializer = new JsonSerializer(); using (BsonWriter writer = new BsonWriter(stream)) { serializer.Serialize(writer, reply); writer.Flush(); writer.Close(); } }