[Quickfix-developers] Checksum / Charset encoding problem
Brought to you by:
orenmnero
|
From: Lalonde, F. <fra...@cg...> - 2006-07-10 16:29:24
|
I haven't found references to this problem in the wiki or elsewhere, so
I just wanted to share my thoughts.=20
=20
I've been having checksum problems when trying to send FIX messages
having an XML payload. The FIX server I was communicating with (an
instance of the Cameron FIX engine) would report bad checksums ont those
messages containing XML, but not on other messages (Logon, Heartbeat,
etc)
=20
I wrote a parallel program to double-check the checksums of the messages
waiting in the QuickFIX queue (by parsing the .body file) just to make
sure that the problem was really on my side, and it was.=20
=20
I traced the problem down to QuickFIX way of handling the charset
encoding of the String fields. I fixed the problem by changing the
Field.getTotal() method from =20
=20
int getTotal() {
calculate();
int sum =3D 0;
for (int i =3D 0; i < data.length(); i++) {
sum +=3D data.charAt(i);
}
return sum + 1;
}
to
int getTotal() {
calculate();
int sum =3D 1;
byte[] bytes =3D data.getBytes();
for (int i =3D 0; i < bytes.length; i++) {
sum +=3D bytes[i];
}
return sum;
}=20
=20
It seems that charAt(i) and getBytes()[i] dont always return the same
byte value, and since the MINA message encoder uses getBytes() to
prepare the wire data, this method should also be used when computing
the checksum to ensure accurate results. Also, I think the checksumming
operation should probably moved to the encoding stage, to eliminate the
redundancy of the relatively expensive getByte() which gets actually get
called twice for each character in the message, but this required more
extensive modifications so I left at this for now.
=20
Francis Lalonde
Treasury Services
CGI
<blocked::mailto:fra...@cg...> =20
=20
|