Skip to content

Commit

Permalink
Merge pull request #826 from gbirchmeier/nullablesocketsession
Browse files Browse the repository at this point in the history
cleanup/nullable Socket and Session classes
  • Loading branch information
gbirchmeier authored Jan 31, 2024
2 parents cf7c3ff + 9015e28 commit f6d44ad
Show file tree
Hide file tree
Showing 17 changed files with 779 additions and 941 deletions.
112 changes: 56 additions & 56 deletions AcceptanceTest/ATApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,171 +13,171 @@ public ATApplication()
{
}

public void OnMessage(QuickFix.FIX40.NewOrderSingle nos, SessionID sessionID)
public void OnMessage(QuickFix.FIX40.NewOrderSingle nos, SessionID sessionId)
{
ProcessNOS(nos, sessionID);
ProcessNOS(nos, sessionId);
}

public void OnMessage(QuickFix.FIX41.NewOrderSingle nos, SessionID sessionID)
public void OnMessage(QuickFix.FIX41.NewOrderSingle nos, SessionID sessionId)
{
ProcessNOS(nos, sessionID);
ProcessNOS(nos, sessionId);
}

public void OnMessage(QuickFix.FIX42.NewOrderSingle nos, SessionID sessionID)
public void OnMessage(QuickFix.FIX42.NewOrderSingle nos, SessionID sessionId)
{
ProcessNOS(nos, sessionID);
ProcessNOS(nos, sessionId);
}

public void OnMessage(QuickFix.FIX42.SecurityDefinition message, SessionID sessionID)
public void OnMessage(QuickFix.FIX42.SecurityDefinition message, SessionID sessionId)
{
Echo(message, sessionID);
Echo(message, sessionId);
}

public void OnMessage(QuickFix.FIX43.NewOrderSingle nos, SessionID sessionID)
public void OnMessage(QuickFix.FIX43.NewOrderSingle nos, SessionID sessionId)
{
ProcessNOS(nos, sessionID);
ProcessNOS(nos, sessionId);
}

public void OnMessage(QuickFix.FIX43.SecurityDefinition message, SessionID sessionID)
public void OnMessage(QuickFix.FIX43.SecurityDefinition message, SessionID sessionId)
{
Echo(message, sessionID);
Echo(message, sessionId);
}

public void OnMessage(QuickFix.FIX44.NewOrderSingle nos, SessionID sessionID)
public void OnMessage(QuickFix.FIX44.NewOrderSingle nos, SessionID sessionId)
{
ProcessNOS(nos, sessionID);
ProcessNOS(nos, sessionId);
}

public void OnMessage(QuickFix.FIX44.SecurityDefinition message, SessionID sessionID)
public void OnMessage(QuickFix.FIX44.SecurityDefinition message, SessionID sessionId)
{
Echo(message, sessionID);
Echo(message, sessionId);
}

public void OnMessage(QuickFix.FIX44.QuoteRequest message, SessionID sessionID)
public void OnMessage(QuickFix.FIX44.QuoteRequest message, SessionID sessionId)
{
Echo(message, sessionID);
Echo(message, sessionId);
}

public void OnMessage(QuickFix.FIX50.NewOrderSingle nos, SessionID sessionID)
public void OnMessage(QuickFix.FIX50.NewOrderSingle nos, SessionID sessionId)
{
ProcessNOS(nos, sessionID);
ProcessNOS(nos, sessionId);
}

public void OnMessage(QuickFix.FIX50.SecurityDefinition message, SessionID sessionID)
public void OnMessage(QuickFix.FIX50.SecurityDefinition message, SessionID sessionId)
{
Echo(message, sessionID);
Echo(message, sessionId);
}

public void OnMessage(QuickFix.FIX50SP1.NewOrderSingle nos, SessionID sessionID)
public void OnMessage(QuickFix.FIX50SP1.NewOrderSingle nos, SessionID sessionId)
{
ProcessNOS(nos, sessionID);
ProcessNOS(nos, sessionId);
}

public void OnMessage(QuickFix.FIX50SP1.SecurityDefinition message, SessionID sessionID)
public void OnMessage(QuickFix.FIX50SP1.SecurityDefinition message, SessionID sessionId)
{
Echo(message, sessionID);
Echo(message, sessionId);
}

public void OnMessage(QuickFix.FIX50SP2.NewOrderSingle nos, SessionID sessionID)
public void OnMessage(QuickFix.FIX50SP2.NewOrderSingle nos, SessionID sessionId)
{
ProcessNOS(nos, sessionID);
ProcessNOS(nos, sessionId);
}

public void OnMessage(QuickFix.FIX50SP2.SecurityDefinition message, SessionID sessionID)
public void OnMessage(QuickFix.FIX50SP2.SecurityDefinition message, SessionID sessionId)
{
Echo(message, sessionID);
Echo(message, sessionId);
}

protected void Echo(Message message, SessionID sessionID)
private static void Echo(Message message, SessionID sessionId)
{
Message echo = new(message);
Session.SendToTarget(echo, sessionID);
Session.SendToTarget(echo, sessionId);
}

protected void ProcessNOS(Message message, SessionID sessionID)
private void ProcessNOS(Message message, SessionID sessionId)
{
Message echo = new(message);

bool possResend = false;
if (message.Header.IsSetField(QuickFix.Fields.Tags.PossResend))
possResend = message.Header.GetBoolean(QuickFix.Fields.Tags.PossResend);

KeyValuePair<string, SessionID> pair = new(message.GetString(QuickFix.Fields.Tags.ClOrdID), sessionID);
KeyValuePair<string, SessionID> pair = new(message.GetString(QuickFix.Fields.Tags.ClOrdID), sessionId);
if (possResend && _clOrdIDs.Contains(pair))
return;
_clOrdIDs.Add(pair);

Session.SendToTarget(echo, sessionID);
Session.SendToTarget(echo, sessionId);
}


public void OnMessage(QuickFix.FIX41.News news, SessionID sessionID) { ProcessNews(news, sessionID); }
public void OnMessage(QuickFix.FIX42.News news, SessionID sessionID) { ProcessNews(news, sessionID); }
public void OnMessage(QuickFix.FIX43.News news, SessionID sessionID) { ProcessNews(news, sessionID); }
public void OnMessage(QuickFix.FIX44.News news, SessionID sessionID) { ProcessNews(news, sessionID); }
public void OnMessage(QuickFix.FIX50.News news, SessionID sessionID) { ProcessNews(news, sessionID); }
public void OnMessage(QuickFix.FIX50SP1.News news, SessionID sessionID) { ProcessNews(news, sessionID); }
public void OnMessage(QuickFix.FIX50SP2.News news, SessionID sessionID) { ProcessNews(news, sessionID); }
public void OnMessage(QuickFix.FIX41.News news, SessionID sessionId) { ProcessNews(news, sessionId); }
public void OnMessage(QuickFix.FIX42.News news, SessionID sessionId) { ProcessNews(news, sessionId); }
public void OnMessage(QuickFix.FIX43.News news, SessionID sessionId) { ProcessNews(news, sessionId); }
public void OnMessage(QuickFix.FIX44.News news, SessionID sessionId) { ProcessNews(news, sessionId); }
public void OnMessage(QuickFix.FIX50.News news, SessionID sessionId) { ProcessNews(news, sessionId); }
public void OnMessage(QuickFix.FIX50SP1.News news, SessionID sessionId) { ProcessNews(news, sessionId); }
public void OnMessage(QuickFix.FIX50SP2.News news, SessionID sessionId) { ProcessNews(news, sessionId); }

public void ProcessNews(Message msg, SessionID sessionID)
private void ProcessNews(Message msg, SessionID sessionId)
{
if (msg.IsSetField(QuickFix.Fields.Tags.Headline) && (msg.GetString(QuickFix.Fields.Tags.Headline) == "STOPME"))
{
if (this.StopMeEvent != null)
StopMeEvent();
}
else
Echo(msg, sessionID);
Echo(msg, sessionId);
}

public void OnMessage(QuickFix.FIX44.TradeCaptureReportRequest msg, SessionID sessionID)
public void OnMessage(QuickFix.FIX44.TradeCaptureReportRequest msg, SessionID sessionId)
{
// do nothing, just swallow it.
}

#region Application Methods

public void OnCreate(SessionID sessionID)
public void OnCreate(SessionID sessionId)
{
Session session = Session.LookupSession(sessionID);
Session? session = Session.LookupSession(sessionId);

// Hey QF/N users, don't do this in a real app.
session?.Reset("AT Session Reset");
}

public void OnLogout(SessionID sessionID)
public void OnLogout(SessionID sessionId)
{
_clOrdIDs.Clear();
}

public void OnLogon(SessionID sessionID)
public void OnLogon(SessionID sessionId)
{ }

public void FromApp(Message message, SessionID sessionID)
public void FromApp(Message message, SessionID sessionId)
{
try
{
string msgType = message.Header.GetString(QuickFix.Fields.Tags.MsgType);
//string msgType = message.Header.GetString(QuickFix.Fields.Tags.MsgType);
// log_.OnEvent("Got message " + msgType);
// System.Console.WriteLine("===got message " + msgType);

Crack(message, sessionID);
Crack(message, sessionId);
}
catch (UnsupportedMessageType)
{
throw;
}
catch (System.Exception e)
{
Session.LookupSession(sessionID).Log.OnEvent("Exception during FromApp: " + e.ToString() + "\n while processing msg (" + message.ToString() + ")");
Session.LookupSession(sessionId)?.Log.OnEvent($"Exception during FromApp: {e}\n while processing msg ({message})");
}
}

public void FromAdmin(Message message, SessionID sessionID)
public void FromAdmin(Message message, SessionID sessionId)
{ }

public void ToAdmin(Message message, SessionID sessionID) { }
public void ToApp(Message message, SessionID sessionID) { }
public void ToAdmin(Message message, SessionID sessionId) { }
public void ToApp(Message message, SessionID sessionId) { }

#endregion
}
Expand Down
3 changes: 3 additions & 0 deletions QuickFIXn/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

# CA1310: Specify StringComparison for correctness
dotnet_diagnostic.CA1310.severity = error

# Initializing field by default value is redundant
dotnet_diagnostic.CA1805.severity = none
26 changes: 15 additions & 11 deletions QuickFIXn/AbstractInitiator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class AbstractInitiator : IInitiator

#endregion

public AbstractInitiator(
protected AbstractInitiator(
IApplication app,
IMessageStoreFactory storeFactory,
SessionSettings settings,
Expand Down Expand Up @@ -167,8 +167,8 @@ public void Stop(bool force)
{
foreach (SessionID sessionId in _connected)
{
Session session = Session.LookupSession(sessionId);
if (session.IsEnabled)
Session? session = Session.LookupSession(sessionId);
if (session is not null && session.IsEnabled)
{
session.Logout();
}
Expand All @@ -185,8 +185,11 @@ public void Stop(bool force)
lock (_sync)
{
HashSet<SessionID> connectedSessionIDs = new HashSet<SessionID>(_connected);
foreach (SessionID sessionId in connectedSessionIDs)
SetDisconnected(Session.LookupSession(sessionId).SessionID);
foreach (SessionID sessionId in connectedSessionIDs) {
Session? session = Session.LookupSession(sessionId);
if (session is not null)
SetDisconnected(session.SessionID);
}
}

IsStopped = true;
Expand Down Expand Up @@ -218,7 +221,7 @@ public bool IsLoggedOn
{
foreach (SessionID sessionId in _connected)
{
Session session = Session.LookupSession(sessionId);
Session? session = Session.LookupSession(sessionId);
return session is not null && session.IsLoggedOn;
}
}
Expand Down Expand Up @@ -262,12 +265,13 @@ protected virtual void OnRemove(SessionID sessionId)
/// Implemented to stop a running initiator.
/// </summary>
protected abstract void OnStop();

/// <summary>
/// Implemented to connect a session to its target.
/// </summary>
/// <param name="sessionId"></param>
/// <param name="session"></param>
/// <param name="settings"></param>
protected abstract void DoConnect(SessionID sessionId, QuickFix.Dictionary settings);
protected abstract void DoConnect(Session session, QuickFix.Dictionary settings);

#endregion

Expand All @@ -280,13 +284,13 @@ protected void Connect()
HashSet<SessionID> disconnectedSessions = new HashSet<SessionID>(_disconnected);
foreach (SessionID sessionId in disconnectedSessions)
{
Session session = Session.LookupSession(sessionId);
if (session.IsEnabled)
Session? session = Session.LookupSession(sessionId);
if (session is not null && session.IsEnabled)
{
if (session.IsNewSession)
session.Reset("New session");
if (session.IsSessionTime)
DoConnect(sessionId, _settings.Get(sessionId));
DoConnect(session, _settings.Get(sessionId));
}
}
}
Expand Down
38 changes: 15 additions & 23 deletions QuickFIXn/AcceptorSocketDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace QuickFix
Expand All @@ -8,59 +8,51 @@ internal class AcceptorSocketDescriptor
{
#region Properties

public ThreadedSocketReactor SocketReactor
{
get { return socketReactor_; }
}
public ThreadedSocketReactor SocketReactor { get; }

public IPEndPoint Address
{
get { return socketEndPoint_; }
}
public IPEndPoint Address { get; }

#endregion

#region Private Members

private ThreadedSocketReactor socketReactor_;
private IPEndPoint socketEndPoint_;
private Dictionary<SessionID, Session> acceptedSessions_ = new Dictionary<SessionID, Session>();
private readonly Dictionary<SessionID, Session> _acceptedSessions = new ();

#endregion

public AcceptorSocketDescriptor(IPEndPoint socketEndPoint, SocketSettings socketSettings, QuickFix.Dictionary sessionDict)
{
socketEndPoint_ = socketEndPoint;
socketReactor_ = new ThreadedSocketReactor(socketEndPoint_, socketSettings, sessionDict, this);
Address = socketEndPoint;
SocketReactor = new ThreadedSocketReactor(Address, socketSettings, sessionDict, this);
}

public void AcceptSession(Session session)
{
lock (acceptedSessions_)
lock (_acceptedSessions)
{
acceptedSessions_[session.SessionID] = session;
_acceptedSessions[session.SessionID] = session;
}
}

/// <summary>
/// Remove a session from those tied to this socket.
/// </summary>
/// <param name="sessionID">ID of session to be removed</param>
/// <param name="sessionId">ID of session to be removed</param>
/// <returns>true if session removed, false if not found</returns>
public bool RemoveSession(SessionID sessionID)
public bool RemoveSession(SessionID sessionId)
{
lock (acceptedSessions_)
lock (_acceptedSessions)
{
return acceptedSessions_.Remove(sessionID);
return _acceptedSessions.Remove(sessionId);
}
}

public Dictionary<SessionID, Session> GetAcceptedSessions()
{
lock (acceptedSessions_)
lock (_acceptedSessions)
{
return new Dictionary<SessionID, Session>(acceptedSessions_);
return new Dictionary<SessionID, Session>(_acceptedSessions);
}
}
}
}
}
Loading

0 comments on commit f6d44ad

Please sign in to comment.