diff --git a/QuickFIXn/AcceptorSocketDescriptor.cs b/QuickFIXn/AcceptorSocketDescriptor.cs index 0fe5f0b56..c3dfbffb3 100644 --- a/QuickFIXn/AcceptorSocketDescriptor.cs +++ b/QuickFIXn/AcceptorSocketDescriptor.cs @@ -26,7 +26,7 @@ public AcceptorSocketDescriptor(IPEndPoint socketEndPoint, SocketSettings socket SocketReactor = new ThreadedSocketReactor(Address, socketSettings, sessionDict, this); } - public void AcceptSession(Session session) + internal void AcceptSession(Session session) { lock (_acceptedSessions) { @@ -39,7 +39,7 @@ public void AcceptSession(Session session) /// /// ID of session to be removed /// true if session removed, false if not found - public bool RemoveSession(SessionID sessionId) + internal bool RemoveSession(SessionID sessionId) { lock (_acceptedSessions) { @@ -47,7 +47,7 @@ public bool RemoveSession(SessionID sessionId) } } - public Dictionary GetAcceptedSessions() + internal Dictionary GetAcceptedSessions() { lock (_acceptedSessions) { diff --git a/QuickFIXn/ClientHandlerThread.cs b/QuickFIXn/ClientHandlerThread.cs index a5cbb074f..56c827811 100755 --- a/QuickFIXn/ClientHandlerThread.cs +++ b/QuickFIXn/ClientHandlerThread.cs @@ -30,21 +30,10 @@ public ExitedEventArgs(ClientHandlerThread clientHandlerThread) private Thread? _thread = null; private volatile bool _isShutdownRequested = false; private readonly SocketReader _socketReader; - private readonly FileLog _log; internal ClientHandlerThread(TcpClient tcpClient, long clientId, QuickFix.Dictionary settingsDict, SocketSettings socketSettings, AcceptorSocketDescriptor? acceptorDescriptor) { - string debugLogFilePath = "log"; - if (settingsDict.Has(SessionSettings.DEBUG_FILE_LOG_PATH)) - debugLogFilePath = settingsDict.GetString(SessionSettings.DEBUG_FILE_LOG_PATH); - else if (settingsDict.Has(SessionSettings.FILE_LOG_PATH)) - debugLogFilePath = settingsDict.GetString(SessionSettings.FILE_LOG_PATH); - - // FIXME - do something more flexible than hardcoding a filelog - _log = new FileLog(debugLogFilePath, new SessionID( - "ClientHandlerThread", clientId.ToString(), "Debug-" + Guid.NewGuid())); - Id = clientId; _socketReader = new SocketReader(tcpClient, socketSettings, this, acceptorDescriptor); } @@ -57,7 +46,7 @@ public void Start() public void Shutdown(string reason) { - Log("shutdown requested: " + reason); + // TODO - need the reason param? _isShutdownRequested = true; } @@ -84,7 +73,6 @@ private void Run() } } - Log("shutdown"); OnExited(); } @@ -92,21 +80,6 @@ private void OnExited() { Exited?.Invoke(this, new ExitedEventArgs(this)); } - /// FIXME do real logging - public void Log(string s) - { - _log.OnEvent(s); - } - - /// - /// Provide StreamReader with access to the log - /// - /// - internal ILog GetLog() - { - return _log; - } - #region Responder Members public bool Send(string data) @@ -135,7 +108,6 @@ protected virtual void Dispose(bool disposing) if (disposing) { _socketReader.Dispose(); - _log.Dispose(); } _disposed = true; } diff --git a/QuickFIXn/SessionSettings.cs b/QuickFIXn/SessionSettings.cs index abefde561..4f06b6579 100755 --- a/QuickFIXn/SessionSettings.cs +++ b/QuickFIXn/SessionSettings.cs @@ -36,7 +36,6 @@ public class SessionSettings public const string SOCKET_CONNECT_PORT = "SocketConnectPort"; public const string RECONNECT_INTERVAL = "ReconnectInterval"; public const string FILE_LOG_PATH = "FileLogPath"; - public const string DEBUG_FILE_LOG_PATH = "DebugFileLogPath"; public const string FILE_STORE_PATH = "FileStorePath"; public const string REFRESH_ON_LOGON = "RefreshOnLogon"; public const string RESET_ON_LOGON = "ResetOnLogon"; diff --git a/QuickFIXn/SocketReader.cs b/QuickFIXn/SocketReader.cs index 4d8eb7e88..a80bf7dcb 100755 --- a/QuickFIXn/SocketReader.cs +++ b/QuickFIXn/SocketReader.cs @@ -34,7 +34,7 @@ internal SocketReader( _tcpClient = tcpClient; _responder = responder; _acceptorDescriptor = acceptorDescriptor; - _stream = Transport.StreamFactory.CreateServerStream(tcpClient, settings, responder.GetLog()); + _stream = Transport.StreamFactory.CreateServerStream(tcpClient, settings); } public void Read() @@ -105,12 +105,13 @@ protected virtual int ReadSome(byte[] buffer, int timeoutMilliseconds) // Nothing read return 0; } - else if (inner is not null) + + if (inner is not null) { throw inner; //rethrow SocketException part (which we have exception logic for) } - else - throw; //rethrow original exception + + throw; //rethrow original exception } } @@ -121,9 +122,9 @@ private void OnMessageFound(string msg) if (_qfSession is null) { _qfSession = Session.LookupSession(Message.GetReverseSessionId(msg)); - if (_qfSession is null || IsAssumedSession(_qfSession.SessionID)) + if (_qfSession is null || IsUnknownSession(_qfSession.SessionID)) { - Log("ERROR: Disconnecting; received message for unknown session: " + msg); + LogSessionEvent("ERROR: Disconnecting; received message for unknown session: " + msg); _qfSession = null; DisconnectClient(); return; @@ -148,7 +149,7 @@ private void OnMessageFound(string msg) } catch (Exception e) { - Log($"Error on Session '{_qfSession.SessionID}': {e}"); + _qfSession.Log.OnEvent($"Error on Session '{_qfSession.SessionID}': {e}"); } } catch (InvalidMessage e) @@ -167,12 +168,13 @@ protected void HandleBadMessage(string msg, Exception e) { if (Fields.MsgType.LOGON.Equals(Message.GetMsgType(msg))) { - Log("ERROR: Invalid LOGON message, disconnecting: " + e.Message); + LogSessionEvent($"ERROR: Invalid LOGON message, disconnecting: {e.Message}"); + // TODO: else session-agnostic log DisconnectClient(); } else { - Log("ERROR: Invalid message: " + e.Message); + LogSessionEvent($"ERROR: Invalid message: {e.Message}"); } } catch (InvalidMessage) @@ -204,7 +206,7 @@ protected void DisconnectClient() _tcpClient.Close(); } - private bool IsAssumedSession(SessionID sessionId) + private bool IsUnknownSession(SessionID sessionId) { return _acceptorDescriptor is not null && !_acceptorDescriptor.GetAcceptedSessions().Any(kv => kv.Key.Equals(sessionId)); @@ -242,7 +244,7 @@ private void HandleExceptionInternal(Session? quickFixSession, Exception cause) break; } - Log($"SocketReader Error: {reason}"); + LogSessionEvent($"SocketReader Error: {reason}"); if (disconnectNeeded) { @@ -254,12 +256,18 @@ private void HandleExceptionInternal(Session? quickFixSession, Exception cause) } /// - /// FIXME do proper logging + /// Log event if session can be identified (TODO: logging if not specific to a session) /// /// - private void Log(string s) + private void LogSessionEvent(string s) { - _responder.Log(s); + if(_qfSession is not null) + _qfSession.Log.OnEvent(s); + else { + // Can't tie this to a session, need a generic log. + // TODO this is a temp console log until I do something better + Console.WriteLine(s); + } } public int Send(string data) diff --git a/QuickFIXn/ThreadedSocketAcceptor.cs b/QuickFIXn/ThreadedSocketAcceptor.cs index ce9993aa3..edd600a78 100755 --- a/QuickFIXn/ThreadedSocketAcceptor.cs +++ b/QuickFIXn/ThreadedSocketAcceptor.cs @@ -145,11 +145,9 @@ private void StartAcceptingConnections() { lock (_sync) { - // FIXME StartSessionTimer(); foreach (AcceptorSocketDescriptor socketDescriptor in _socketDescriptorForAddress.Values) { socketDescriptor.SocketReactor.Start(); - // FIXME log_.Info("Listening for connections on " + socketDescriptor.getAddress()); } } } @@ -161,7 +159,6 @@ private void StopAcceptingConnections() foreach (AcceptorSocketDescriptor socketDescriptor in _socketDescriptorForAddress.Values) { socketDescriptor.SocketReactor.Shutdown(); - // FIXME log_.Info("No longer accepting connections on " + socketDescriptor.getAddress()); } } } diff --git a/QuickFIXn/ThreadedSocketReactor.cs b/QuickFIXn/ThreadedSocketReactor.cs index 17b54215c..80113e5ec 100755 --- a/QuickFIXn/ThreadedSocketReactor.cs +++ b/QuickFIXn/ThreadedSocketReactor.cs @@ -41,6 +41,7 @@ public State ReactorState #endregion + // TODO: internalize. Only used by test. public ThreadedSocketReactor( IPEndPoint serverSocketEndPoint, SocketSettings socketSettings, @@ -137,8 +138,6 @@ public void Run() _clientThreads.Add(t.Id, t); } - // FIXME set the client thread's exception handler here - t.Log("connected"); t.Start(); } else @@ -214,7 +213,7 @@ private void ShutdownClientHandlerThreads() } catch (Exception e) { - t.Log("Error shutting down: " + e.Message); + Log($"Error shutting down: {e.Message}"); } t.Dispose(); } diff --git a/QuickFIXn/Transport/StreamFactory.cs b/QuickFIXn/Transport/StreamFactory.cs index 8b637fea9..16c6528b3 100644 --- a/QuickFIXn/Transport/StreamFactory.cs +++ b/QuickFIXn/Transport/StreamFactory.cs @@ -102,7 +102,7 @@ public static Stream CreateClientStream(IPEndPoint endpoint, SocketSettings sett Stream stream = new NetworkStream(socket, true); if (settings.UseSSL) - stream = new SslStreamFactory(logger, settings).CreateClientStreamAndAuthenticate(stream); + stream = new SslStreamFactory(settings).CreateClientStreamAndAuthenticate(stream); return stream; } @@ -112,10 +112,9 @@ public static Stream CreateClientStream(IPEndPoint endpoint, SocketSettings sett /// /// The TCP client. /// The socket settings. - /// Logger to use. /// an opened and initiated stream which can be read and written to /// tcp client must be connected in order to get stream;tcpClient - public static Stream CreateServerStream(TcpClient tcpClient, SocketSettings settings, ILog logger) + public static Stream CreateServerStream(TcpClient tcpClient, SocketSettings settings) { if (tcpClient.Connected == false) throw new ArgumentException("tcp client must be connected in order to get stream", nameof(tcpClient)); @@ -123,7 +122,7 @@ public static Stream CreateServerStream(TcpClient tcpClient, SocketSettings sett Stream stream = tcpClient.GetStream(); if (settings.UseSSL) { - stream = new SslStreamFactory(logger, settings).CreateServerStreamAndAuthenticate(stream); + stream = new SslStreamFactory(settings).CreateServerStreamAndAuthenticate(stream); } return stream; @@ -227,14 +226,12 @@ public static Stream CreateServerStream(TcpClient tcpClient, SocketSettings sett /// private sealed class SslStreamFactory { - private readonly ILog _log; private readonly SocketSettings _socketSettings; private const string CLIENT_AUTHENTICATION_OID = "1.3.6.1.5.5.7.3.2"; private const string SERVER_AUTHENTICATION_OID = "1.3.6.1.5.5.7.3.1"; - public SslStreamFactory(ILog log, SocketSettings settings) + public SslStreamFactory(SocketSettings settings) { - _log = log; _socketSettings = settings; } @@ -265,7 +262,7 @@ public Stream CreateClientStreamAndAuthenticate(Stream innerStream) } catch (System.Security.Authentication.AuthenticationException ex) { - _log.OnEvent("Unable to perform authentication against server: " + ex.Message); + Log($"Unable to perform authentication against server: {ex.Message}"); throw; } @@ -306,7 +303,7 @@ public Stream CreateServerStreamAndAuthenticate(Stream innerStream) } catch (System.Security.Authentication.AuthenticationException ex) { - _log.OnEvent("Unable to perform authentication against server: " + ex.Message); + Log($"Unable to perform authentication against server: {ex.Message}"); throw; } @@ -376,26 +373,22 @@ private bool VerifyRemoteCertificate( // Validate enhanced key usage if (!ContainsEnhancedKeyUsage(certificate, enhancedKeyUsage)) { if (enhancedKeyUsage == CLIENT_AUTHENTICATION_OID) - _log.OnEvent( - "Remote certificate is not intended for client authentication: It is missing enhanced key usage " + - enhancedKeyUsage); + Log($"Remote certificate is not intended for client authentication: It is missing enhanced key usage {enhancedKeyUsage}"); else - _log.OnEvent( - "Remote certificate is not intended for server authentication: It is missing enhanced key usage " + - enhancedKeyUsage); + Log($"Remote certificate is not intended for server authentication: It is missing enhanced key usage {enhancedKeyUsage}"); return false; } if (string.IsNullOrEmpty(_socketSettings.CACertificatePath)) { - _log.OnEvent("CACertificatePath is not specified"); + Log("CACertificatePath is not specified"); return false; } // If CA Certficiate is specified then validate agains the CA certificate, otherwise it is validated against the installed certificates X509Certificate2? cert = LoadCertificate(_socketSettings.CACertificatePath, null); if (cert is null) { - _log.OnEvent("Remote certificate was not recognized as a valid certificate: " + sslPolicyErrors); + Log($"Remote certificate was not recognized as a valid certificate: {sslPolicyErrors}"); return false; } @@ -420,7 +413,7 @@ private bool VerifyRemoteCertificate( // Any basic authentication check failed, do after checking CA if (sslPolicyErrors != SslPolicyErrors.None) { - _log.OnEvent("Remote certificate was not recognized as a valid certificate: " + sslPolicyErrors); + Log($"Remote certificate was not recognized as a valid certificate: {sslPolicyErrors}"); return false; } @@ -494,6 +487,11 @@ private static bool ContainsEnhancedKeyUsage(X509Certificate certificate, string return null; } + + private void Log(string s) { + // TODO this is just a temp console log until I do something better + Console.WriteLine(s); + } } } }