public override bool SendTo(ClientId id, IMessage msg) { var cl = clients[id]; if (cl.DeferredDisconnect) { Root.Log(LogLevel.Debug, "Server: attempt to send message to disconnected client"); return(false); } using (var rawBuf = new MemoryStream()) { var buf = rawBuf.AsUnclosable(); var header = new MsgHeader { Id = msg.Id, Flags = MessageFlags.None }; if (cl.Secure) { using (var container = new MessageCryptoContainer()) { symCp.Key = cl.SessionKey; // XXX: generate new symCp.IV container.Store(msg, symCp, Protocol); container.Save(buf, Protocol); header.Size = (int)buf.Length; header.Flags |= MessageFlags.Secure; } } else { msg.Save(buf, Protocol); header.Size = (int)buf.Length; } try { header.Save(cl.NetStream, Protocol); rawBuf.WriteTo(cl.NetStream); } catch (IOException e) { if (NetUtil.CheckConnectionReset(e)) { OnClientConnectionReset(cl); return(false); } throw; } return(true); } }
// XXX: write generalized version (both for server and client) and move to core public override bool Send(IMessage msg) { if (!Connected) { Root.Log(LogLevel.Debug, "Client: attempt to send message with no connection"); return(false); } using (var rawBuf = new MemoryStream()) { var buf = rawBuf.AsUnclosable(); var header = new MsgHeader(); header.Id = msg.Id; header.Flags = MessageFlags.None; if (sessionKey != null) { using (var container = new MessageCryptoContainer()) { symCp.Key = sessionKey; // XXX: generate new symCp.IV container.Store(msg, symCp, cfg.Protocol); container.Save(buf, cfg.Protocol); header.Size = (int)buf.Length; header.Flags |= MessageFlags.Secure; } } else { msg.Save(buf, cfg.Protocol); header.Size = (int)buf.Length; } try { header.Save(netStream, cfg.Protocol); rawBuf.WriteTo(netStream); } catch (IOException e) { if (NetUtil.CheckConnectionReset(e)) { OnServerConnectionReset(); return(false); } throw; } } return(true); }