forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketReader.cs
executable file
·218 lines (199 loc) · 7.1 KB
/
SocketReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System.Net.Sockets;
namespace QuickFix
{
/// <summary>
/// TODO merge with SocketInitiatorThread
/// </summary>
public class SocketReader
{
public const int BUF_SIZE = 4096;
private byte[] readBuffer_ = new byte[BUF_SIZE];
private Parser parser_ = new Parser();
private Session qfSession_ = null;
private TcpClient tcpClient_;
private ClientHandlerThread responder_;
public SocketReader(TcpClient tcpClient, ClientHandlerThread responder)
{
tcpClient_ = tcpClient;
responder_ = responder;
}
/// <summary> FIXME </summary>
public void Read()
{
try
{
if (tcpClient_.Client.Poll(1000000, SelectMode.SelectRead)) // one-second timeout
{
int bytesRead = tcpClient_.Client.Receive(readBuffer_);
if (bytesRead < 1)
throw new SocketException(System.Convert.ToInt32(SocketError.ConnectionReset));
parser_.AddToStream(System.Text.Encoding.UTF8.GetString(readBuffer_, 0, bytesRead));
}
else if (null != qfSession_)
{
qfSession_.Next();
}
ProcessStream();
}
catch (MessageParseError e)
{
HandleException(qfSession_, e, tcpClient_);
}
catch (System.Exception e)
{
HandleException(qfSession_, e, tcpClient_);
throw e;
}
}
public void OnMessageFound(string msg)
{
///Message fixMessage;
try
{
if (null == qfSession_)
{
qfSession_ = Session.LookupSession(Message.GetReverseSessionID(msg));
if (null == qfSession_)
{
this.Log("ERROR: Disconnecting; received message for unknown session: " + msg);
DisconnectClient();
return;
}
else
{
if (!HandleNewSession(msg))
return;
}
}
try
{
qfSession_.Next(msg);
}
catch (System.Exception e)
{
this.Log("Error on Session '" + qfSession_.SessionID + "': " + e.ToString());
}
}
catch (InvalidMessage e)
{
HandleBadMessage(msg, e);
}
catch (MessageParseError e)
{
HandleBadMessage(msg, e);
}
}
protected void HandleBadMessage(string msg, System.Exception e)
{
try
{
if (Fields.MsgType.LOGON.Equals(Message.GetMsgType(msg)))
{
this.Log("ERROR: Invalid LOGON message, disconnecting: " + e.Message);
DisconnectClient();
}
else
{
this.Log("ERROR: Invalid message: " + e.Message);
}
}
catch (InvalidMessage)
{ }
}
protected bool ReadMessage(out string msg)
{
try
{
return parser_.ReadFixMessage(out msg);
}
catch(MessageParseError e)
{
this.Log("SocketReader: " + e.Message);
}
msg = "";
return true;
}
protected void ProcessStream()
{
string msg;
while (ReadMessage(out msg))
OnMessageFound(msg);
}
protected static void DisconnectClient(TcpClient client)
{
client.Client.Close();
client.Close();
}
protected void DisconnectClient()
{
DisconnectClient(tcpClient_);
}
protected bool HandleNewSession(string msg)
{
if(qfSession_.HasResponder)
{
qfSession_.Log.OnIncoming(msg);
qfSession_.Log.OnEvent("Multiple logons/connections for this session are not allowed (" + tcpClient_.Client.RemoteEndPoint + ")");
qfSession_ = null;
DisconnectClient();
return false;
}
qfSession_.Log.OnEvent(qfSession_.SessionID + " Socket Reader " + GetHashCode() + " accepting session " + qfSession_.SessionID + " from " + tcpClient_.Client.RemoteEndPoint);
/// FIXME do this here? qfSession_.HeartBtInt = QuickFix.Fields.Converters.IntConverter.Convert(message.GetField(Fields.Tags.HeartBtInt)); /// FIXME
qfSession_.Log.OnEvent(qfSession_.SessionID +" Acceptor heartbeat set to " + qfSession_.HeartBtInt + " seconds");
qfSession_.SetResponder(responder_);
return true;
}
public void HandleException(Session quickFixSession, System.Exception cause, TcpClient client)
{
bool disconnectNeeded = true;
string reason = cause.Message;
System.Exception realCause = cause;
/** TODO
if(cause is FIXMessageDecoder.DecodeError && cause.InnerException != null)
realCause = cause.getCause();
*/
if (realCause is System.Net.Sockets.SocketException)
{
if (quickFixSession != null && quickFixSession.IsEnabled)
reason = "Socket exception (" + client.Client.RemoteEndPoint + "): " + cause.Message;
else
reason = "Socket (" + client.Client.RemoteEndPoint + "): " + cause.Message;
disconnectNeeded = true;
}
/** TODO
else if(realCause is FIXMessageDecoder.CriticalDecodeError)
{
reason = "Critical protocol codec error: " + cause;
disconnectNeeded = true;
}
*/
else if(realCause is MessageParseError)
{
reason = "Protocol handler exception: " + cause;
disconnectNeeded = false;
}
else
{
reason = cause.ToString();
disconnectNeeded = false;
}
this.Log("SocketReader Error: " + reason);
if (disconnectNeeded)
{
if (null != quickFixSession && quickFixSession.HasResponder)
quickFixSession.Disconnect(reason);
else
DisconnectClient(client);
}
}
/// <summary>
/// FIXME do proper logging
/// </summary>
/// <param name="s"></param>
private void Log(string s)
{
responder_.Log(s);
}
}
}