{{Quickfixn}} Overriding "SocketInitiatorThread.SetupStream" (e.g. to uncompress stream)

FABIO CATUNDA MARRECO fabio.marreco at modal.com.br
Thu Jun 6 10:24:15 PDT 2019


Hello everybody, thank you for the great project.

I am currently having an issue, and would like to propose a change, if I may....
I have a data provider which needs some customization on the network stream (it requires to be decompressed).
Currently, the class SocketInitiatorThread has a method protected virtual SetupStream() with the comment:
/// <summary>
/// Setup/Connect to the other party.
/// Override this in order to setup other types of streams with other settings
/// </summary>
/// <returns>Stream representing the (network)connection to the other party</returns>
So this is clearly the place I should override and add the customization.
My problem is that this class is created on SocketInitiator.DoConnect, with this implementation:
protected override void DoConnect(SessionID sessionID, Dictionary settings)
{
    Session session = null;

    try
    {
        session = Session.LookupSession(sessionID);
        if (!session.IsSessionTime)
            return;

        IPEndPoint socketEndPoint = GetNextSocketEndPoint(sessionID, settings);
        SetPending(sessionID);
        session.Log.OnEvent("Connecting to " + socketEndPoint.Address + " on port " + socketEndPoint.Port);

        //Setup socket settings based on current section
        var socketSettings = socketSettings_.Clone();
        socketSettings.Configure(settings);

        // Create a Ssl-SocketInitiatorThread if a certificate is given
        SocketInitiatorThread t = new SocketInitiatorThread(this, session, socketEndPoint, socketSettings);
        t.Start();
        AddThread(t);

    }
    catch (System.Exception e)
    {
        if (null != session)
            session.Log.OnEvent(e.Message);
    }
}
So, in order to inherit from SocketInitiatorThread, and override SetupStream, I have to also inherit from SocketInitiator and override DoConnect.
Unfortunatly, many methods used inside DoConnect are private (like GetNextSocketEndPoint, AddThread, etc.). And I´ll end up having to duplicate the whole class.

I propose either:
(1) Extract the line which creates the SocketInitiatorThread to a virtual method
   public virtual CreateSocketInitiatorThread(Session session, IPEndPoint socketEndPoint, SocketSettings socketSettings)
{
     return new SocketInitiatorThread(this, session, socketEndPoint, socketSettings);
}

or
(2) Inject a factory (ISocketInitiatorThreadFactory) into SocketInitiator, such that:
public interface ISocketInitiatorThreadFactory
    {
        SocketInitiatorThread Create(Transport.SocketInitiator initiator, Session session, IPEndPoint socketEndPoint, SocketSettings socketSettings);
    }

    class DefaultSocketInitiatorThreadFactory : ISocketInitiatorThreadFactory
    {
        public SocketInitiatorThread Create(SocketInitiator initiator, Session session, IPEndPoint socketEndPoint, SocketSettings socketSettings)
            => new SocketInitiatorThread(initiator, session, socketEndPoint, socketSettings);
    }
And then create a new constructor receiving the factory. (the other constructors will use the default factory).
I much rather the second approach since the 1st one can lead to violating LSP

Is there any issue I am not seeing, or can I create a pull request for it ?

BTW, I have wrongly created an issue for this, should I close it until we decide what to do ?

Thank you very much

[http://www.modal.com.br/emailmkt/marca-assinatura.jpg]<https://modal.com.br/html/index.php>



FABIO CATUNDA MARRECO




fabio.marreco at modal.com.br <mailto:fabio.marreco at modal.com.br%20>




Tels. 55 21 3223 7801 | 7700
Fax   55 21 3223 7738
www.modal.com.br <http://www.modal.com.br>




A impressão consciente gera economia e responsabilidade com o meio ambiente.

Atenção: Esta comunicação deve ser lida apenas pelo seu destinatário e não pode ser retransmitida sem autorização formal. Se esta mensagem tiver sido recebida indevidamente, por favor destrua-a e retire-a de seu computador.
Qualquer reprodução, disseminação, alteração, distribuição e/ou publicação deste e-mail é estritamente proibida.  
  
Notice of Confidentiality: This document should only be read by those persons to whom it is addressed and is not intended to be relied upon by any person without subsequent written confirmation of its contents. If you have received this e-mail message in error, please destroy it and delete it from your computer.
Any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message is strictly prohibited.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.quickfixn.com/pipermail/quickfixn-quickfixn.com/attachments/20190606/5e4e6283/attachment-0001.html>


More information about the Quickfixn mailing list