I am getting delays on IPC on a single machine that has 10 cores and is running 47 instances of my ClientApp which are all communicating with the MasterApp.
I appear to get severe latency on occasion. Here is part of my log. The DateTime on the left is the log DateTime(a high perf logger). The DateTimes within the [] are the times the message was sent from the MasterApp. Each message terminates with an @.
So the first message is only 1ms behind, but the last is 71ms behind.
Any ideas what might cause this and what I might do to get rid of the latency
20141030T120401.015 [--------*MD|USD/JPY 109.032 109.034 1000000.00 1000000.00 20141030T120401014@]
20141030T120401.084 [--------*MD|EUR/CHF 1.20580 1.20588 3000000.00 2000000.00 20141030T120401019@]
20141030T120401.163 [--------*MD|USD/JPY 109.031 109.034 1000000.00 1000000.00 20141030T120401088@*MD|EUR/CHF 1.20580 1.20588 3000000.00 1000000.00 20141030T120401092@]
Code excerpt:
public void Connect(int port)
{
IPAddress[] aryLocalAddr = null;
String strHostName = "";
try
{
// NOTE: DNS lookups are nice and all but quite time consuming.
strHostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
aryLocalAddr = ipEntry.AddressList;
}
catch (Exception ex)
{
OutputWriteLine("Error trying to get local address: " + ex.Message);
}
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Blocking = false;
IPEndPoint epServer = new IPEndPoint(aryLocalAddr[0], port);
socket.BeginConnect(epServer, new AsyncCallback(ConnectCallBack), socket);
}
public void ConnectCallBack(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;
NewConnection(socket);
}
public void NewConnection(Socket socket)
{
Connection mc = new Connection(socket);
connections.Add(mc);
//OutputWriteLine("Client " + mc.SessionID() + " joined");
DateTime now = DateTime.Now;
String intraMessage = "*IDENT|" + modelInitiatorApp.G.SLOTNAME;
modelInitiatorApp.SetConnected();
SendMessage(mc, intraMessage);
socket.BeginReceive(mc.stateObject.buffer, 0, mc.stateObject.buffer.Length, SocketFlags.None, new AsyncCallback(ReceivedCallBack), mc.stateObject);
}
public void ReceivedCallBack(IAsyncResult ar)
{
//
StateObject state = (StateObject)ar.AsyncState;
Socket socket = state.socket;
try
{
int bytesRead = socket.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
OutputWriteLine("[--------"+state.sb.ToString()+"]");
string[] contents = state.sb.ToString().Split('@');
int delimCount = state.sb.ToString().Count(x => x == '@');
for (int d = 0; d < delimCount; d++)
{
if (contents[d] != "")
OnMessage(state, contents[d]);
}
if (!state.sb.ToString().EndsWith("@"))
{
state.sb.Clear();
state.sb.Append(contents[contents.Count() - 1]);
}
else
{
state.sb.Clear();
}
socket.BeginReceive(state.buffer, 0, state.buffer.Length, SocketFlags.None, new AsyncCallback(ReceivedCallBack), state);
}
else
{
// If no data was received then the connection is probably dead
OutputWriteLine("Client " + state.SessionID() + " disconnected");
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Unusual error during Receive!");
}
}