IFSFSockets.cs 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Xml.Linq;
  9. using System.Xml.Serialization;
  10. using Wayne.FDCPOSLibrary;
  11. using Wayne.FDCPOSLibrary.Configuration;
  12. using Wayne.Lib;
  13. using Wayne.Lib.Log;
  14. namespace Wayne.ForecourtControl.Fusion
  15. {
  16. public class TCPListenerThread : DisposableBase
  17. {
  18. public bool bRunning = false;
  19. private bool bTerminate = false;
  20. TcpListener tcpListener = null;
  21. IFSFManager ifsfManager = null;
  22. string name = "";
  23. private readonly DebugLogger debugLogger;
  24. public TCPListenerThread(TcpListener _listener, IFSFManager _ifsfManager, string _name)
  25. {
  26. tcpListener = _listener;
  27. ifsfManager = _ifsfManager;
  28. name = _name;
  29. debugLogger = ifsfManager.DebugLogger;
  30. }
  31. private void DebugLog(string s)
  32. {
  33. if (debugLogger.IsActive())
  34. debugLogger.Add(s);
  35. }
  36. public void TCPListenerThreadProc()
  37. {
  38. DebugLog(string.Format("TCPListenerThreadProc Id={0}, WId={1}: init", this.ifsfManager.clientSocket.Id, this.ifsfManager.clientSocket.workstationID));
  39. TcpClient tcpclient = null;
  40. bRunning = true;
  41. try
  42. {
  43. if (tcpListener != null)
  44. {
  45. DebugLog("tcpListener starting");
  46. tcpListener.Start();
  47. DebugLog("tcpListener started");
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. DebugLog("TCPListenerThreadProc: Exception tcpListener.Start! " + ex.ToString());
  53. bRunning = false;
  54. return;
  55. }
  56. bTerminate = false;
  57. string sbuffer = "";
  58. while (!bTerminate)
  59. {
  60. try
  61. {
  62. if (tcpclient == null && tcpListener != null)
  63. {
  64. DebugLog("TCPListenerThreadProc: TcpClient Accepting");
  65. tcpclient = tcpListener.AcceptTcpClient();
  66. DebugLog("TcpClient Accepted");
  67. }
  68. else
  69. {
  70. NetworkStream networkStream;
  71. if (tcpListener != null)
  72. networkStream = tcpclient.GetStream();
  73. else
  74. networkStream = ifsfManager.clientSocket.socketReqChannelA.GetStream();
  75. DebugLog(string.Format("TCPListenerThreadProc Id={0}, WId={1}: {2} got stream ...", this.ifsfManager.clientSocket.Id, this.ifsfManager.clientSocket.workstationID, this.name));
  76. StringBuilder sb = new StringBuilder();
  77. // read header
  78. int headerLength, encrStart;
  79. if (FDCGlobal.ProtocolVersion <= FDCVersion.V0007)
  80. {
  81. headerLength = Define.HeaderLength + Define.MD5EncriptionLength;
  82. encrStart = Define.HeaderLength;
  83. }
  84. else if (this.ifsfManager.clientSocket.headerEncryption != 0)
  85. {
  86. headerLength = Define.HeaderLength + Define.EncriptionTypeLength + Define.MD5EncriptionLength;
  87. encrStart = Define.HeaderLength + Define.EncriptionTypeLength;
  88. }
  89. else
  90. {
  91. headerLength = Define.HeaderLength + Define.EncriptionTypeLength;
  92. encrStart = Define.HeaderLength + Define.EncriptionTypeLength;
  93. }
  94. var header = new byte[headerLength];
  95. DebugLog(string.Format("TCPListenerThreadProc: header length={0}", header.GetLength(0)));
  96. int nread, nreadtot = 0;
  97. do
  98. {
  99. DebugLog("TCPListenerThreadProc: reading ...");
  100. nread = networkStream.Read(header, nreadtot, (headerLength) - nreadtot);
  101. DebugLog(string.Format("TCPListenerThreadProc Id={0}, WId={1}: read '{2}' bytes", this.ifsfManager.clientSocket.Id, this.ifsfManager.clientSocket.workstationID, nread));
  102. nreadtot += nread;
  103. if (nread == 0)
  104. Thread.Sleep(500);
  105. }
  106. while (nreadtot < headerLength);
  107. int msglength = getMsgLength(header);
  108. // read message
  109. var buffer = new byte[msglength];
  110. DebugLog(string.Format("TCPListenerThreadProc: buffer msg length={0}", buffer.GetLength(0)));
  111. nreadtot = 0;
  112. do
  113. {
  114. DebugLog("TCPListenerThreadProc: reading ...");
  115. nread = networkStream.Read(buffer, nreadtot, msglength - nreadtot);
  116. DebugLog(string.Format("TCPListenerThreadProc Id={0}, WId={1}: read '{2}' bytes", this.ifsfManager.clientSocket.Id, this.ifsfManager.clientSocket.workstationID, nread));
  117. nreadtot += nread;
  118. if (nread == 0)
  119. Thread.Sleep(500);
  120. }
  121. while (nreadtot < msglength);
  122. sb.Remove(0, sb.Length);
  123. sb.Append(Encoding.UTF8.GetString(buffer, 0, nreadtot));
  124. string myString = Encoding.ASCII.GetString(buffer, 0, msglength);
  125. string logString;
  126. try
  127. {
  128. logString = XElement.Parse(myString).ToString();
  129. }
  130. catch
  131. {
  132. logString = myString;
  133. }
  134. DebugLog(string.Format("TCPListenerThreadProc Id={0}, WId={1}: read '{2}' bytes:\r\n{3}", this.ifsfManager.clientSocket.Id, this.ifsfManager.clientSocket.workstationID, msglength, logString));
  135. sbuffer += sb.ToString();
  136. bool bEnd = false;
  137. do
  138. {
  139. var indexEndMsgSR = sbuffer.IndexOf("</ServiceResponse>");
  140. var indexEndFDCMsg = sbuffer.IndexOf("</FDCMessage>");
  141. string sEndmsg;
  142. int indexEnd;
  143. if (indexEndMsgSR >= 0 && indexEndFDCMsg >= 0)
  144. {
  145. indexEnd = System.Math.Min(indexEndMsgSR, indexEndFDCMsg);
  146. if (indexEndMsgSR < indexEndFDCMsg)
  147. {
  148. sEndmsg = "</ServiceResponse>";
  149. }
  150. else
  151. {
  152. sEndmsg = "</FDCMessage>";
  153. }
  154. }
  155. else if (indexEndMsgSR >= 0 && indexEndFDCMsg < 0)
  156. {
  157. indexEnd = indexEndMsgSR;
  158. sEndmsg = "</ServiceResponse>";
  159. }
  160. else if (indexEndFDCMsg > 0)
  161. {
  162. indexEnd = indexEndFDCMsg;
  163. sEndmsg = "</FDCMessage>";
  164. }
  165. else
  166. {
  167. indexEnd = sbuffer.IndexOf("/>");
  168. sEndmsg = "/>";
  169. }
  170. if (indexEnd >= 0)
  171. {
  172. var smsg = sbuffer.Substring(0, indexEnd + sEndmsg.Length);
  173. sbuffer = sbuffer.Substring(indexEnd + sEndmsg.Length);
  174. var result = OverallResult.Success;
  175. if (this.ifsfManager.clientSocket.headerEncryption != 0)
  176. {
  177. bool error = checkHash(smsg, encrStart, header);
  178. if (error)
  179. {
  180. DebugLog(error ? "ValidationError" : "check OK");
  181. int i = 0;
  182. while (i < sbuffer.Length && (sbuffer[i] == '\n' || sbuffer[i] == ' ' || sb[i] == '\t'))
  183. i++;
  184. if (i > 0)
  185. {
  186. DebugLog(string.Format("try to verify msg with '\\n' or ' ' ending chars ('{0}' found)", i));
  187. smsg += sbuffer.Substring(0, i);
  188. sbuffer = sbuffer.Substring(0, sbuffer.Length - i);
  189. error = checkHash(smsg, encrStart, header);
  190. DebugLog(error ? "ValidationError" : "check OK");
  191. if (error)
  192. result = OverallResult.ValidationError;
  193. }
  194. else
  195. result = OverallResult.ValidationError;
  196. }
  197. }
  198. if (smsg.Length > 0)
  199. {
  200. int i = 0;
  201. while (i < sbuffer.Length && (sbuffer[i] == '\n' || sbuffer[i] == ' ' || sb[i] == '\t'))
  202. i++;
  203. if (i > 0)
  204. {
  205. DebugLog(string.Format("try to verify msg with '\\n' or ' ' ending chars ('{0}' found)", i));
  206. smsg += sbuffer.Substring(0, i);
  207. sbuffer = sbuffer.Substring(0, sbuffer.Length - i);
  208. DebugLog(string.Format("smsg={0}", smsg));
  209. DebugLog(string.Format("sbuffer={0}", sbuffer));
  210. }
  211. if (smsg.IndexOf("<ServiceResponse") >= 0)
  212. ifsfManager.ReadResponse(smsg, msglength, ref result);
  213. else
  214. ifsfManager.ReadMessage(smsg, msglength, ref result);
  215. }
  216. }
  217. else bEnd = true;
  218. } while (!bEnd);
  219. }
  220. }
  221. catch (SocketException sex)
  222. {
  223. DebugLog("Exception TCPListenerThreadProc: " + sex.ToString());
  224. tcpclient = null;
  225. if (sex.ErrorCode == 10049)
  226. ifsfManager.Disconnect();
  227. Thread.Sleep(1000);
  228. }
  229. catch (Exception ex)
  230. {
  231. DebugLog("Exception TCPListenerThreadProc! " + ex.ToString());
  232. tcpclient = null;
  233. ifsfManager.Disconnect();
  234. Thread.Sleep(1000);
  235. }
  236. }
  237. if (tcpListener != null)
  238. tcpListener.Stop();
  239. bRunning = false;
  240. DebugLog(string.Format("TCPListenerThreadProc Id={0}, WId={1}: end", this.ifsfManager.clientSocket.Id, this.ifsfManager.clientSocket.workstationID));
  241. }
  242. private bool checkHash(string smsg, int encrStart, byte[] header)
  243. {
  244. MD5Crypter crypter = new MD5Crypter();
  245. byte[] datahash = crypter.ComputeHash(System.Text.Encoding.UTF8.GetBytes(smsg + MD5Crypter.passphrase));
  246. var inputhash = new byte[Define.MD5EncriptionLength];
  247. for (int i = encrStart; i < Define.MD5EncriptionLength + encrStart; i++)
  248. inputhash[i - encrStart] = header[i];
  249. bool error = false;
  250. for (int i = 0; i < Define.MD5EncriptionLength; i++)
  251. if (inputhash[i] != datahash[i])
  252. {
  253. DebugLog(string.Format("ValidationError: inputhash={0}, datahash={1}", System.Text.Encoding.UTF8.GetString(inputhash, 0, inputhash.GetLength(0)), System.Text.Encoding.UTF8.GetString(datahash, 0, datahash.GetLength(0))));
  254. DebugLog(string.Format("ValidationError: inputhash[{0}]={1} != datahash[{2}]={3}", i, inputhash[i], i, datahash[i]));
  255. error = true;
  256. break;
  257. }
  258. return error;
  259. }
  260. private int getMsgLength(byte[] header)
  261. {
  262. int length = 0;
  263. for (int pos = 1; pos <= Define.HeaderLength; pos++)
  264. {
  265. length |= header[pos - 1] << (Define.HeaderLength - pos) * 8;
  266. }
  267. return length;
  268. }
  269. protected override void DoDispose()
  270. {
  271. bTerminate = true;
  272. }
  273. }
  274. public class IFSFSockets : IIdentifiableEntity, IDisposable//, IConnectable
  275. {
  276. private TcpClient _socketReqChannelA;
  277. public TcpClient socketReqChannelA
  278. {
  279. get { return _socketReqChannelA; }
  280. }
  281. private TcpListener _socketRespChannelB;
  282. public TcpListener socketRespChannelB
  283. {
  284. get { return _socketRespChannelB; }
  285. }
  286. private TcpListener _socketUnsolicitedChannelC;
  287. public TcpListener socketUnsolicitedChannelC
  288. {
  289. get { return _socketUnsolicitedChannelC; }
  290. }
  291. private TcpClient _socketReqChannelConfig;
  292. public TcpClient socketReqChannelConfig
  293. {
  294. get { return _socketReqChannelConfig; }
  295. }
  296. public TCPListenerThread respChannelBThreadObj;
  297. public Thread respChannelBThread;
  298. public TCPListenerThread unsolicitedChannelCThreadObj;
  299. public Thread unsolicitedChannelCThread;
  300. public TCPListenerThread respChannelConfigThreadObj;
  301. public Thread respChannelConfigThread;
  302. IFSFManager ifsfManager;
  303. IFSFMessages _ifsfMessages;
  304. public IFSFMessages ifsfMessages
  305. {
  306. get { return _ifsfMessages; }
  307. }
  308. private string _applicationSender;
  309. public string applicationSender
  310. {
  311. get { return _applicationSender; }
  312. }
  313. private string _workstationID;
  314. public string workstationID
  315. {
  316. get { return _workstationID; }
  317. }
  318. private string _sIPAddress;
  319. public string sIPAddress
  320. {
  321. get { return _sIPAddress; }
  322. }
  323. private string _sIPAddress2;
  324. public string sIPAddress2
  325. {
  326. get { return _sIPAddress2; }
  327. }
  328. private int _iIPPortA;
  329. public int iIPPortA
  330. {
  331. get { return _iIPPortA; }
  332. }
  333. private int _iIPPortB;
  334. public int iIPPortB
  335. {
  336. get { return _iIPPortB; }
  337. }
  338. private int _iIPPortC;
  339. public int iIPPortC
  340. {
  341. get { return _iIPPortC; }
  342. }
  343. private long _heartbeatInterval;
  344. public long heartbeatInterval
  345. {
  346. get { return _heartbeatInterval; }
  347. }
  348. private long _heartbeatTimeout;
  349. public long heartbeatTimeout
  350. {
  351. get { return _heartbeatTimeout; }
  352. }
  353. private long _runningFuellingTimeout = 2000;
  354. public long runningFuellingTimeout
  355. {
  356. get { return _runningFuellingTimeout; }
  357. }
  358. private long _headerEncryption;
  359. public long headerEncryption
  360. {
  361. get { return _headerEncryption; }
  362. set { _headerEncryption = value; }
  363. }
  364. public bool logOnSendCalled = false;
  365. private byte[][] _validationInfo;
  366. public byte[][] validationInfo
  367. {
  368. get { return _validationInfo; }
  369. set { _validationInfo = value; }
  370. }
  371. private string _posInfo = "";
  372. public string posInfo
  373. {
  374. get { return _posInfo; }
  375. set { _posInfo = value; }
  376. }
  377. private bool disposed;
  378. private int id;
  379. private IIdentifiableEntity parentEntity;
  380. private DeviceConnectionState _mainConnectionState = DeviceConnectionState.Disconnected;
  381. public DeviceConnectionState mainConnectionState
  382. {
  383. get { return _mainConnectionState; }
  384. set
  385. {
  386. if (_mainConnectionState != value)
  387. {
  388. _mainConnectionState = value;
  389. if (OnConnectionStateChange != null)
  390. {
  391. OnConnectionStateChange.Invoke(this, new ConnectionChangedEventArgs(mainConnectionState));
  392. }
  393. }
  394. }
  395. }
  396. public event EventHandler<ConnectionChangedEventArgs> OnConnectionStateChange;
  397. public event EventHandler OnResponseTimeout;
  398. private readonly DebugLogger debugLogger;
  399. public IFSFSockets(int id, IFSFManager _ifsfManager)
  400. {
  401. this.id = id;
  402. this.parentEntity = (IIdentifiableEntity)ifsfManager;
  403. ifsfManager = _ifsfManager;
  404. debugLogger = ifsfManager.DebugLogger;
  405. ConfigurationParams configParam = new ConfigurationParams(debugLogger);
  406. this._applicationSender = configParam.applicationSender;
  407. this._workstationID = configParam.workstationID;
  408. this._sIPAddress = configParam.sIPAddress;
  409. this._sIPAddress2 = configParam.sIPAddress2;
  410. this._iIPPortA = configParam.iIPPortA;
  411. this._iIPPortB = configParam.iIPPortB;
  412. this._iIPPortC = configParam.iIPPortC;
  413. this._heartbeatTimeout = configParam.heartbeatTimeout;
  414. this._heartbeatInterval = configParam.heartbeatInterval;
  415. this._headerEncryption = configParam.headerEncryption;
  416. _ifsfMessages = new IFSFMessages(this, debugLogger);
  417. _ifsfMessages.OnIFSFSMessageEnqueued += new EventHandler(ifsfmessages_OnMessageEnqueued);
  418. _ifsfMessages.OnResponseTimeout += new EventHandler(ifsfmessages_OnResponseTimeout);
  419. }
  420. private void DebugLog(string s)
  421. {
  422. if (debugLogger.IsActive())
  423. debugLogger.Add(s);
  424. }
  425. private bool getConnectionParams(string connectionString)
  426. {
  427. Dictionary<string, string> connectionStringParamDict;
  428. connectionStringParamDict = Strings.ParseConnectionString(connectionString);
  429. string key = "";
  430. key = "IdShift";
  431. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  432. {
  433. try
  434. {
  435. ((FUSIONForecourtControl)(FUSIONFactory.fusionForecourtControlList[this.Id])).manager.IdShift = Convert.ToInt32(connectionStringParamDict[key]);
  436. }
  437. catch (Exception ex)
  438. {
  439. DebugLog(string.Format("Exception reading IdShift: {0}", ex.ToString()));
  440. }
  441. }
  442. key = "IdPumpShift";
  443. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  444. {
  445. try
  446. {
  447. ((FUSIONForecourtControl)(FUSIONFactory.fusionForecourtControlList[this.Id])).manager.IdPumpShift = Convert.ToInt32(connectionStringParamDict[key]);
  448. }
  449. catch (Exception ex)
  450. {
  451. DebugLog(string.Format("Exception reading IdPumpShift: {0}", ex.ToString()));
  452. }
  453. }
  454. key = "IdNozzleShift";
  455. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  456. {
  457. try
  458. {
  459. ((FUSIONForecourtControl)(FUSIONFactory.fusionForecourtControlList[this.Id])).manager.IdNozzleShift = Convert.ToInt32(connectionStringParamDict[key]);
  460. }
  461. catch (Exception ex)
  462. {
  463. DebugLog(string.Format("Exception reading IdNozzleShift: {0}", ex.ToString()));
  464. }
  465. }
  466. key = "IdTankShift";
  467. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  468. {
  469. try
  470. {
  471. ((FUSIONForecourtControl)(FUSIONFactory.fusionForecourtControlList[this.Id])).manager.IdTankShift = Convert.ToInt32(connectionStringParamDict[key]);
  472. }
  473. catch (Exception ex)
  474. {
  475. DebugLog(string.Format("Exception reading IdTankShift: {0}", ex.ToString()));
  476. }
  477. }
  478. key = "FuelGradeShift";
  479. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  480. {
  481. try
  482. {
  483. ((FUSIONForecourtControl)(FUSIONFactory.fusionForecourtControlList[this.Id])).manager.FuelGradeShift = Convert.ToInt32(connectionStringParamDict[key]);
  484. }
  485. catch (Exception ex)
  486. {
  487. DebugLog(string.Format("Exception reading FuelGradeShift: {0}", ex.ToString()));
  488. }
  489. }
  490. key = "Host";
  491. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  492. this._sIPAddress = connectionStringParamDict[key];
  493. key = "Host2";
  494. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  495. this._sIPAddress2 = connectionStringParamDict[key];
  496. key = "Port";
  497. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  498. {
  499. try
  500. {
  501. this._iIPPortA = Convert.ToInt32(connectionStringParamDict[key]);
  502. }
  503. catch (Exception ex)
  504. {
  505. DebugLog(string.Format("Exception reading IPPortA: {0}", ex.ToString()));
  506. }
  507. }
  508. key = "PortB";
  509. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  510. {
  511. try
  512. {
  513. this._iIPPortB = Convert.ToInt32(connectionStringParamDict[key]);
  514. }
  515. catch (Exception ex)
  516. {
  517. DebugLog(string.Format("Exception reading IPPortB: {0}", ex.ToString()));
  518. }
  519. }
  520. key = "PortC";
  521. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  522. {
  523. try
  524. {
  525. this._iIPPortC = Convert.ToInt32(connectionStringParamDict[key]);
  526. }
  527. catch (Exception ex)
  528. {
  529. DebugLog(string.Format("Exception reading IPPortC: {0}", ex.ToString()));
  530. }
  531. }
  532. //key = "workstationID";
  533. //if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  534. // this._applicationSender = connectionStringParamDict[key];
  535. //else
  536. //{
  537. // key = "ClientName";
  538. // if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  539. // this._applicationSender = connectionStringParamDict[key];
  540. //}
  541. //key = "applicationSender";
  542. //if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  543. // this._workstationID = connectionStringParamDict[key];
  544. //else
  545. //{
  546. // key = "ClientId";
  547. // if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  548. // this._workstationID = connectionStringParamDict[key];
  549. //}
  550. key = "ClientId";
  551. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  552. {
  553. this._workstationID = connectionStringParamDict[key];
  554. this._applicationSender = connectionStringParamDict[key];
  555. }
  556. else
  557. {
  558. key = "applicationSender";
  559. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  560. {
  561. this._workstationID = connectionStringParamDict[key];
  562. this._applicationSender = connectionStringParamDict[key];
  563. }
  564. }
  565. key = "heartbeatInterval";
  566. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  567. {
  568. try
  569. {
  570. this._heartbeatInterval = Convert.ToInt64(connectionStringParamDict[key]);
  571. }
  572. catch (Exception ex)
  573. {
  574. DebugLog(string.Format("Exception reading heartbeatInterval: {0}", ex.ToString()));
  575. }
  576. }
  577. key = "heartbeatTimeout";
  578. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  579. {
  580. try
  581. {
  582. this._heartbeatTimeout = Convert.ToInt64(connectionStringParamDict[key]);
  583. }
  584. catch (Exception ex)
  585. {
  586. DebugLog(string.Format("Exception reading _heartbeatTimeout: {0}", ex.ToString()));
  587. }
  588. }
  589. key = "runningFuellingTimeout";
  590. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  591. {
  592. try
  593. {
  594. this._runningFuellingTimeout = Convert.ToInt64(connectionStringParamDict[key]);
  595. }
  596. catch (Exception ex)
  597. {
  598. DebugLog(string.Format("Exception reading _runningFuellingTimeout: {0}", ex.ToString()));
  599. }
  600. }
  601. key = "headerEncryption";
  602. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  603. {
  604. try
  605. {
  606. this._headerEncryption = Convert.ToInt16(connectionStringParamDict[key]);
  607. }
  608. catch (Exception ex)
  609. {
  610. DebugLog(string.Format("Exception reading _headerEncryption: {0}", ex.ToString()));
  611. }
  612. }
  613. key = "posInfo";
  614. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  615. this._posInfo = connectionStringParamDict[key];
  616. key = "validationInfo";
  617. if (connectionStringParamDict.ContainsKey(key) && connectionStringParamDict[key] != null && connectionStringParamDict[key] != "")
  618. {
  619. string[] validationInfoArray;
  620. try
  621. {
  622. //validationInfoArray = connectionStringParamDict[key].Split('@');
  623. //this._validationInfo = new byte[validationInfoArray.Length][];
  624. //int i = 0;
  625. //foreach(string s in validationInfoArray)
  626. //{
  627. string s = connectionStringParamDict[key];
  628. string elem;
  629. while (s.Length > 0)
  630. {
  631. if (s.IndexOf("@@@") >= 0)
  632. {
  633. elem = s.Substring(0, s.IndexOf("@@@"));
  634. s = s.Substring(s.IndexOf("@@@") + 3);
  635. }
  636. else
  637. {
  638. elem = s;
  639. s = "";
  640. }
  641. //DESCrypter descrypter = new DESCrypter();
  642. //byte[] dataencrypted = descrypter.Encrypt(elem);
  643. byte[] b = System.Text.Encoding.Unicode.GetBytes(elem);
  644. this.ifsfManager.LogOnAddValidationInfo(b);
  645. //IFSFManager.ArrayResize<byte[]>(ref _validationInfo, (_validationInfo != null) ? _validationInfo.Length + 1 : 1);
  646. //this._validationInfo[_validationInfo.Length-1] = new byte[dataencrypted.Length];
  647. //this._validationInfo[_validationInfo.Length-1] = dataencrypted;
  648. }
  649. }
  650. catch (Exception ex)
  651. {
  652. DebugLog(string.Format("Exception reading _validationInfo: {0}", ex.ToString()));
  653. }
  654. }
  655. return true;
  656. }
  657. public void SendConfig(string commandFileName)
  658. {
  659. if (this.mainConnectionState == DeviceConnectionState.Connected)
  660. {
  661. int iIPPortConfig = Convert.ToInt32(IniFile.IniReadValue(ConfigurationParams.getSINPPath("ini\\") + @"ForecourtServer.ini", "FUSION-Connection", "IPPortConfig"));
  662. try
  663. {
  664. IPAddress serverIPAddress = null;
  665. IPEndPoint endPoint = null;
  666. if (iIPPortConfig != this._iIPPortA)
  667. {
  668. int socketTimeout = 3000;
  669. bool noDelay = true;
  670. try
  671. {
  672. string sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "FUSION-Connection", "SocketTimeout");
  673. if (sValue.Length > 0)
  674. socketTimeout = Convert.ToInt32(sValue);
  675. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "FUSION-Connection", "NoDelay");
  676. if (sValue.Length > 0 && sValue == "0")
  677. noDelay = false;
  678. }
  679. catch (Exception ex)
  680. {
  681. DebugLog("EXCEPTION! " + ex.ToString());
  682. }
  683. DebugLog(string.Format("socketTimeout={0}", socketTimeout));
  684. _socketReqChannelConfig = new TcpClient();
  685. _socketReqChannelConfig.SendTimeout = socketTimeout;
  686. _socketReqChannelConfig.NoDelay = noDelay;
  687. int retrycount = 0;
  688. bool bConnected = false;
  689. string ipAddress = this._sIPAddress;
  690. string error = "";
  691. while (!bConnected)
  692. {
  693. if (retrycount == 5)
  694. {
  695. if (this._sIPAddress2 != "") ipAddress = this._sIPAddress2;
  696. DebugLog(error);
  697. }
  698. else if (retrycount == 10)
  699. {
  700. retrycount = 0;
  701. ipAddress = this._sIPAddress;
  702. DebugLog(error);
  703. Thread.Sleep(5000);
  704. }
  705. retrycount++;
  706. try
  707. {
  708. socketReqChannelConfig.Connect(ipAddress, iIPPortConfig);
  709. bConnected = true;
  710. }
  711. catch (SocketException sex)
  712. {
  713. error = sex.ToString();
  714. Thread.Sleep(1000);
  715. }
  716. }
  717. respChannelConfigThreadObj = new TCPListenerThread(null, ifsfManager, "ChannelConfig");
  718. respChannelConfigThread = new Thread(new ThreadStart(respChannelConfigThreadObj.TCPListenerThreadProc));
  719. respChannelConfigThread.Start();
  720. }
  721. else
  722. _socketReqChannelConfig = this._socketReqChannelA;
  723. Thread.Sleep(2000);
  724. // start reading file with config messages
  725. string line, xmlconfigfile = ConfigurationParams.getSINPPath("ini\\") + commandFileName;
  726. StringBuilder sb = new StringBuilder(); ;
  727. StreamReader sr = new StreamReader(xmlconfigfile);
  728. while (sr.Peek() != -1)
  729. {
  730. line = sr.ReadLine();
  731. sb.Append(line);
  732. }
  733. sr.Close();
  734. string allconfig = sb.ToString();
  735. //string allconfig = sr.ReadToEnd();
  736. string[] messages;
  737. string msg2;
  738. messages = allconfig.Split('#');
  739. foreach (string msg in messages)
  740. {
  741. msg2 = msg;
  742. //msg2 = msg.Replace("\r", "");
  743. //msg2 = msg2.Replace("\n", "");
  744. //msg2 = msg2.Replace("\t", "");
  745. this.SendConfigMessage(msg2);
  746. }
  747. }
  748. catch (Exception ex)
  749. {
  750. DebugLog(string.Format("SendConfig end: EXCEPTION! {0}", ex.ToString()));
  751. }
  752. if (iIPPortConfig != this._iIPPortA)
  753. {
  754. if (_socketReqChannelConfig != null)
  755. this._socketReqChannelConfig.Close();
  756. this._socketReqChannelConfig = null;
  757. }
  758. }
  759. else
  760. DebugLog("SendConfig ERROR: NOT Connected!");
  761. }
  762. public bool Connect(string connectionString)
  763. {
  764. DebugLog(string.Format("Connect init: status='{0}'", this.mainConnectionState));
  765. if (this.mainConnectionState == DeviceConnectionState.Disconnected
  766. || this.mainConnectionState == DeviceConnectionState.Disconnecting)
  767. {
  768. //this.mainConnectionState = DeviceConnectionState.Connecting;
  769. try
  770. {
  771. getConnectionParams(connectionString);
  772. FDCGlobal.ProtocolVersion = FDCGlobal.VersionFromString(IniFile.IniReadValue(ConfigurationParams.inifile, "FUSION-Connection", "FDCPOSInterfaceVersion"));
  773. FDCGlobal.ConfigVersion = FDCGlobal.ConfigVersionFromString(IniFile.IniReadValue(ConfigurationParams.inifile, "FUSION-Connection", "FDCPOSConfigVersion"));
  774. DebugLog(string.Format("id={0}, -connectionString:'{1}', version:'{2}', configVersion:'{3}'", this.id, connectionString, FDCGlobal.ProtocolVersion, FDCGlobal.ConfigVersion));
  775. this.ifsfManager.heartbeat.Stop();
  776. this.ifsfManager.heartbeat.heartbeatInterval = this.heartbeatInterval;
  777. this.ifsfManager.heartbeat.heartbeatTimeout = this.heartbeatTimeout;
  778. int socketTimeout = 3000;
  779. bool noDelay = true;
  780. try
  781. {
  782. string sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "FUSION-Connection", "SocketTimeout");
  783. if (sValue.Length > 0)
  784. socketTimeout = Convert.ToInt32(sValue);
  785. sValue = IniFile.IniReadValue(ConfigurationParams.inifile, "FUSION-Connection", "NoDelay");
  786. if (sValue.Length > 0 && sValue == "0")
  787. noDelay = false;
  788. }
  789. catch (Exception ex)
  790. {
  791. DebugLog("EXCEPTION! " + ex.ToString());
  792. }
  793. DebugLog(string.Format("socketTimeout={0}", socketTimeout));
  794. IPAddress serverIPAddress = null;
  795. IPEndPoint endPoint = null;
  796. _socketReqChannelA = new TcpClient();
  797. _socketReqChannelA.SendTimeout = socketTimeout;
  798. _socketReqChannelA.NoDelay = noDelay;
  799. int retrycount = 0;
  800. bool bConnected = false;
  801. string ipAddress = this._sIPAddress;
  802. string error = "";
  803. while (!bConnected)
  804. {
  805. if (retrycount == 5)
  806. {
  807. if (this._sIPAddress2 != "") ipAddress = this._sIPAddress2;
  808. DebugLog(error);
  809. }
  810. else if (retrycount == 10)
  811. {
  812. retrycount = 0;
  813. ipAddress = this._sIPAddress;
  814. DebugLog(error);
  815. Thread.Sleep(5000);
  816. }
  817. retrycount++;
  818. try
  819. {
  820. DebugLog(string.Format("connecting to ip={0}, port={1}", ipAddress, this.iIPPortA));
  821. this.socketReqChannelA.Connect(ipAddress, this.iIPPortA);
  822. bConnected = true;
  823. }
  824. catch (SocketException sex)
  825. {
  826. error = sex.ToString();
  827. Thread.Sleep(1000);
  828. }
  829. }
  830. if (iIPPortB != iIPPortA /*&& _socketRespChannelB == null*/)
  831. _socketRespChannelB = new TcpListener(iIPPortB);
  832. else
  833. _socketRespChannelB = null;
  834. if (iIPPortC != iIPPortA /*&& _socketUnsolicitedChannelC == null*/)
  835. _socketUnsolicitedChannelC = new TcpListener(iIPPortC);
  836. else
  837. _socketUnsolicitedChannelC = null;
  838. DebugLog(string.Format("Channel A Connected: Id={0}, WId={1}, AId={2}", this.id, this.workstationID, this.applicationSender));
  839. ifsfManager.authentificationErrorRetry = true;
  840. if (this.validationInfo != null)
  841. ifsfManager.LogOn(validationInfo, posInfo, "");
  842. else
  843. ifsfManager.LogOnSend(posInfo);
  844. ifsfManager.heartbeat.ResetDisconnectionTimeout();
  845. if (iIPPortB != iIPPortA /*&& respChannelBThreadObj == null*/)
  846. {
  847. respChannelBThreadObj = new TCPListenerThread(socketRespChannelB, ifsfManager, "ChannelB");
  848. respChannelBThread = new Thread(new ThreadStart(respChannelBThreadObj.TCPListenerThreadProc));
  849. respChannelBThread.Start();
  850. }
  851. else /*if (respChannelBThreadObj == null)*/
  852. {
  853. respChannelBThreadObj = new TCPListenerThread(null, ifsfManager, "ChannelA");
  854. respChannelBThread = new Thread(new ThreadStart(respChannelBThreadObj.TCPListenerThreadProc));
  855. respChannelBThread.Start();
  856. }
  857. if (iIPPortC != iIPPortA /*&& unsolicitedChannelCThreadObj == null*/)
  858. {
  859. unsolicitedChannelCThreadObj = new TCPListenerThread(socketUnsolicitedChannelC, ifsfManager, "ChannelC");
  860. unsolicitedChannelCThread = new Thread(new ThreadStart(unsolicitedChannelCThreadObj.TCPListenerThreadProc));
  861. unsolicitedChannelCThread.Start();
  862. }
  863. }
  864. catch (Exception ex)
  865. {
  866. DebugLog(string.Format("Connect end: EXCEPTION! {0}", ex.ToString()));
  867. return false;
  868. }
  869. }
  870. DebugLog("Connect end: ok");
  871. return true;
  872. }
  873. public void Disconnect()
  874. {
  875. DebugLog("terminating respThreadObjs");
  876. if (respChannelBThreadObj != null)
  877. respChannelBThreadObj.Dispose();
  878. DebugLog("terminating respChannelCThreadObj");
  879. if (unsolicitedChannelCThreadObj != null)
  880. unsolicitedChannelCThreadObj.Dispose();
  881. if (respChannelConfigThreadObj != null)
  882. respChannelConfigThreadObj.Dispose();
  883. if (_socketReqChannelA != null)
  884. this._socketReqChannelA.Close();
  885. this._socketReqChannelA = null;
  886. if (_socketReqChannelConfig != null)
  887. this._socketReqChannelConfig.Close();
  888. this._socketReqChannelConfig = null;
  889. if (this._socketRespChannelB != null)
  890. this._socketRespChannelB.Stop();
  891. _socketRespChannelB = null;
  892. if (this._socketUnsolicitedChannelC != null)
  893. this._socketUnsolicitedChannelC.Stop();
  894. _socketUnsolicitedChannelC = null;
  895. }
  896. public virtual void Dispose()
  897. {
  898. this.Dispose(true);
  899. GC.SuppressFinalize(this);
  900. }
  901. private void Dispose(bool disposing)
  902. {
  903. if (!this.disposed)
  904. {
  905. this.disposed = true;
  906. if (disposing)
  907. {
  908. }
  909. }
  910. }
  911. public DeviceConnectionState ConnectionState
  912. {
  913. get
  914. {
  915. return this.mainConnectionState;
  916. }
  917. }
  918. public string EntitySubType
  919. {
  920. get
  921. {
  922. return "RPC";
  923. }
  924. }
  925. public string EntityType
  926. {
  927. get
  928. {
  929. return "ClientSocket";
  930. }
  931. }
  932. /// <summary>
  933. /// This is used by the logger and should never be set by inheriting classes
  934. /// </summary>
  935. public string FullEntityName { get; set; }
  936. public int Id
  937. {
  938. get
  939. {
  940. return this.id;
  941. }
  942. }
  943. public IIdentifiableEntity ParentEntity
  944. {
  945. get
  946. {
  947. return this.parentEntity;
  948. }
  949. }
  950. public void Serialize<T>(T sr)
  951. {
  952. NetworkStream netstream = socketReqChannelA.GetStream();
  953. byte[] payload;
  954. using (var memstream = new MemoryStream())
  955. {
  956. XmlSerializer serializer = ifsfMessages.GetXmlSerializer(typeof(T));
  957. serializer.Serialize(memstream, (T)sr);
  958. payload = memstream.ToArray();
  959. }
  960. // writes header and encription string
  961. // writes message length
  962. using (var memstream = new MemoryStream())
  963. {
  964. using (var writer = new BinaryWriter(memstream))
  965. {
  966. writer.Write(new[]
  967. {
  968. (byte)(payload.Length >> 24),
  969. (byte)(payload.Length >> 16),
  970. (byte)(payload.Length >> 8),
  971. (byte)(payload.Length)
  972. });
  973. // if V01 then writes algorithm type (0: no encription, 1: MD5 encryption
  974. if (FDCGlobal.ProtocolVersion >= FDCVersion.V0100)
  975. {
  976. writer.Write(new[]
  977. {
  978. (byte)(0),
  979. this.ifsfManager.clientSocket.headerEncryption != 0 ? (byte)(1) : (byte)(0)
  980. });
  981. }
  982. // writes encryption
  983. if (FDCGlobal.ProtocolVersion <= FDCVersion.V0007 ||
  984. (FDCGlobal.ProtocolVersion >= FDCVersion.V0100 && this.ifsfManager.clientSocket.headerEncryption != 0))
  985. {
  986. byte[] bytes;
  987. if (this.ifsfManager.clientSocket.headerEncryption != 0)
  988. {
  989. var crypter = new MD5Crypter();
  990. var passPhrase = crypter.getPassphrase();
  991. var hashingbytes = new byte[payload.Length + passPhrase.Length];
  992. Array.Copy(payload, hashingbytes, payload.Length);
  993. Array.Copy(passPhrase, 0, hashingbytes, payload.Length, passPhrase.Length);
  994. bytes = crypter.ComputeHash(hashingbytes);
  995. }
  996. else
  997. {
  998. bytes = new byte[Define.MD5EncriptionLength];
  999. for (int i = 0; i < Define.MD5EncriptionLength; i++)
  1000. bytes[i] = 0;
  1001. }
  1002. writer.Write(bytes);
  1003. }
  1004. writer.Write(payload);
  1005. memstream.WriteTo(netstream);
  1006. DebugLog(string.Format("Serialize: memstream={0}", Encoding.UTF8.GetString(payload)));
  1007. }
  1008. }
  1009. }
  1010. public void SendConfigMessage(string configmessage)
  1011. {
  1012. NetworkStream netstream = socketReqChannelConfig.GetStream();
  1013. byte[] payload = Encoding.UTF8.GetBytes(configmessage);
  1014. // writes header and encription string
  1015. using (MemoryStream memstream = new MemoryStream())
  1016. {
  1017. using (BinaryWriter writer = new BinaryWriter(memstream))
  1018. {
  1019. writer.Write(new[]
  1020. {
  1021. (byte)(payload.Length >> 24),
  1022. (byte)(payload.Length >> 16),
  1023. (byte)(payload.Length >> 8),
  1024. (byte)(payload.Length)
  1025. });
  1026. // if V01 then writes algorithm type (0: no encription, 1: MD5 encryption
  1027. if (FDCGlobal.ProtocolVersion >= FDCVersion.V0100)
  1028. {
  1029. writer.Write(new[]
  1030. {
  1031. (byte)(0),
  1032. this.ifsfManager.clientSocket.headerEncryption != 0 ? (byte)(1) : (byte)(0)
  1033. });
  1034. }
  1035. // writes encryption
  1036. if (FDCGlobal.ProtocolVersion <= FDCVersion.V0007 ||
  1037. (FDCGlobal.ProtocolVersion >= FDCVersion.V0100 && this.ifsfManager.clientSocket.headerEncryption != 0))
  1038. {
  1039. byte[] bytes;
  1040. if (this.ifsfManager.clientSocket.headerEncryption != 0)
  1041. {
  1042. var crypter = new MD5Crypter();
  1043. var passPhrase = crypter.getPassphrase();
  1044. var hashingbytes = new byte[payload.Length + passPhrase.Length];
  1045. Array.Copy(payload, hashingbytes, payload.Length);
  1046. Array.Copy(passPhrase, 0, hashingbytes, payload.Length, passPhrase.Length);
  1047. bytes = crypter.ComputeHash(hashingbytes);
  1048. }
  1049. else
  1050. {
  1051. bytes = new byte[Define.MD5EncriptionLength];
  1052. for (int i = 0; i < Define.MD5EncriptionLength; i++)
  1053. bytes[i] = 0;
  1054. }
  1055. writer.Write(bytes);
  1056. }
  1057. writer.Write(payload);
  1058. memstream.WriteTo(netstream);
  1059. DebugLog(string.Format("SendConfigMessage: memstream={0}", configmessage));
  1060. }
  1061. }
  1062. }
  1063. private void ifsfmessages_OnResponseTimeout(object sender, EventArgs e)
  1064. {
  1065. if (OnResponseTimeout != null)
  1066. OnResponseTimeout.Invoke(this, null);
  1067. }
  1068. private void ifsfmessages_OnMessageEnqueued(object sender, EventArgs e)
  1069. {
  1070. DebugLog("ifsfmessages_OnMessageEnqueued init:");
  1071. string requestType = "";
  1072. int requestId = 0;
  1073. try
  1074. {
  1075. while (ifsfMessages.serviceRequestChannelA.Count > 0)
  1076. {
  1077. BasePOSRequest sr = ifsfMessages.serviceRequestChannelA.Dequeue();
  1078. //DebugLog(string.Format("sr.GetType: {0}", sr.GetType().FullName));
  1079. if (sr.GetType() == typeof(POSMessagePOSReady))
  1080. {
  1081. requestType = ((FDCMessage)sr).MessageType;
  1082. requestId = Convert.ToInt32(((FDCMessage)sr).MessageID);
  1083. }
  1084. else
  1085. {
  1086. requestType = ((ServiceRequest)sr).RequestType;
  1087. requestId = Convert.ToInt32(((ServiceRequest)sr).RequestID);
  1088. }
  1089. DebugLog(string.Format("SendRequest RequestType: {0}, requestId={1}", requestType, requestId));
  1090. if (requestType == "POSHeartBeat")
  1091. {
  1092. Serialize<ServiceRequestHeartbeat>((ServiceRequestHeartbeat)sr);
  1093. }
  1094. else if (requestType == "POS_Ready")
  1095. {
  1096. //Serialize<ServiceRequestPOSReady>((ServiceRequestPOSReady)sr);
  1097. Serialize<POSMessagePOSReady>((POSMessagePOSReady)sr);
  1098. }
  1099. else if (requestType == "LogOn")
  1100. {
  1101. if (FDCGlobal.ProtocolVersion <= FDCVersion.V0007)
  1102. Serialize<ServiceRequestLogOnV07>((ServiceRequestLogOnV07)sr);
  1103. else
  1104. Serialize<ServiceRequestLogOn>((ServiceRequestLogOn)sr);
  1105. }
  1106. else if (requestType == "LogOff")
  1107. {
  1108. Serialize<ServiceRequestLogOff>((ServiceRequestLogOff)sr);
  1109. }
  1110. else if (requestType == "VersionInfo")
  1111. {
  1112. Serialize<ServiceRequestVersionInfo>((ServiceRequestVersionInfo)sr);
  1113. }
  1114. else if (requestType == "StartForecourt")
  1115. {
  1116. Serialize<ServiceRequestStartForecourt>((ServiceRequestStartForecourt)sr);
  1117. }
  1118. else if (requestType == "TwinOpenMaster")
  1119. {
  1120. Serialize<ServiceRequestTwinOpenMaster>((ServiceRequestTwinOpenMaster)sr);
  1121. }
  1122. else if (requestType == "StopForecourt")
  1123. {
  1124. Serialize<ServiceRequestStopForecourt>((ServiceRequestStopForecourt)sr);
  1125. }
  1126. else if (requestType == "GetCurrentFuellingStatus")
  1127. {
  1128. Serialize<ServiceRequestGetCurrentFuellingStatus>((ServiceRequestGetCurrentFuellingStatus)sr);
  1129. }
  1130. else if (requestType == "GetTotals")
  1131. {
  1132. Serialize<ServiceRequestGetFuelPointTotals>((ServiceRequestGetFuelPointTotals)sr);
  1133. }
  1134. else if (requestType == "GetDeviceState")
  1135. {
  1136. Serialize<ServiceRequestGetDeviceState>((ServiceRequestGetDeviceState)sr);
  1137. }
  1138. else if (requestType == "GetFPState")
  1139. {
  1140. Serialize<ServiceRequestGetFPState>((ServiceRequestGetFPState)sr);
  1141. }
  1142. else if (requestType == "GetTPState")
  1143. {
  1144. Serialize<ServiceRequestGetTPState>((ServiceRequestGetTPState)sr);
  1145. }
  1146. else if (requestType == "GetPPState")
  1147. {
  1148. Serialize<ServiceRequestGetPPState>((ServiceRequestGetPPState)sr);
  1149. }
  1150. else if (requestType == "GetVIRState")
  1151. {
  1152. Serialize<ServiceRequestGetVIRState>((ServiceRequestGetVIRState)sr);
  1153. }
  1154. else if (requestType == "TerminateFuelling")
  1155. {
  1156. Serialize<ServiceRequestTerminateFuelling>((ServiceRequestTerminateFuelling)sr);
  1157. }
  1158. else if (requestType == "AuthoriseFuelPoint")
  1159. {
  1160. Serialize<ServiceRequestAuthoriseFuelPoint>((ServiceRequestAuthoriseFuelPoint)sr);
  1161. }
  1162. else if (requestType == "ChangeFuelMode")
  1163. {
  1164. Serialize<ServiceRequestChangeFuelMode>((ServiceRequestChangeFuelMode)sr);
  1165. }
  1166. else if (requestType == "ChangeFPFuelMode")
  1167. {
  1168. Serialize<ServiceRequestChangeFuelMode>((ServiceRequestChangeFuelMode)sr);
  1169. }
  1170. else if (requestType == "ChangeFuelPrice")
  1171. {
  1172. Serialize<ServiceRequestChangeFuelPrice>((ServiceRequestChangeFuelPrice)sr);
  1173. }
  1174. else if (requestType == "LockFuelSaleTrx")
  1175. {
  1176. Serialize<ServiceRequestLockFuelSaleTrx>((ServiceRequestLockFuelSaleTrx)sr);
  1177. }
  1178. else if (requestType == "UnlockFuelSaleTrx")
  1179. {
  1180. Serialize<ServiceRequestUnlockFuelSaleTrx>((ServiceRequestUnlockFuelSaleTrx)sr);
  1181. }
  1182. else if (requestType == "ClearFuelSaleTrx")
  1183. {
  1184. Serialize<ServiceRequestClearFuelSaleTrx>((ServiceRequestClearFuelSaleTrx)sr);
  1185. }
  1186. else if (requestType == "GetAvailableFuelSaleTrxs")
  1187. {
  1188. Serialize<ServiceRequestGetAvailableFuelSaleTrxs>((ServiceRequestGetAvailableFuelSaleTrxs)sr);
  1189. }
  1190. else if (requestType == "GetFuelSaleTrxDetails")
  1191. {
  1192. Serialize<ServiceRequestGetFuelSaleTrxDetails>((ServiceRequestGetFuelSaleTrxDetails)sr);
  1193. }
  1194. else if (requestType == "GetProductTable")
  1195. {
  1196. Serialize<ServiceRequestGetProductTable>((ServiceRequestGetProductTable)sr);
  1197. }
  1198. else if (requestType == "GetModeTable")
  1199. {
  1200. Serialize<ServiceRequestGetModeTable>((ServiceRequestGetModeTable)sr);
  1201. }
  1202. else if (requestType == "GetFuelMode")
  1203. {
  1204. Serialize<ServiceRequestGetFuelMode>((ServiceRequestGetFuelMode)sr);
  1205. }
  1206. else if (requestType == "GetFPFuelMode")
  1207. {
  1208. Serialize<ServiceRequestGetFPFuelMode>((ServiceRequestGetFPFuelMode)sr);
  1209. }
  1210. else if (requestType == "GetConfiguration")
  1211. {
  1212. Serialize<ServiceRequestGetConfiguration>((ServiceRequestGetConfiguration)sr);
  1213. }
  1214. else if (requestType == "GetDSPConfiguration")
  1215. {
  1216. Serialize<ServiceRequestGetDSPConfiguration>((ServiceRequestGetDSPConfiguration)sr);
  1217. }
  1218. else if (requestType == "GetTLGConfiguration")
  1219. {
  1220. Serialize<ServiceRequestGetTLGConfiguration>((ServiceRequestGetTLGConfiguration)sr);
  1221. }
  1222. else if (requestType == "GetPPConfiguration")
  1223. {
  1224. Serialize<ServiceRequestGetPPConfiguration>((ServiceRequestGetPPConfiguration)sr);
  1225. }
  1226. else if (requestType == "SetConfiguration")
  1227. {
  1228. Serialize<ServiceRequestSetConfiguration>((ServiceRequestSetConfiguration)sr);
  1229. }
  1230. else if (requestType == "LockNozzle")
  1231. {
  1232. Serialize<ServiceRequestLockNozzle>((ServiceRequestLockNozzle)sr);
  1233. }
  1234. else if (requestType == "UnlockNozzle")
  1235. {
  1236. Serialize<ServiceRequestUnlockNozzle>((ServiceRequestUnlockNozzle)sr);
  1237. }
  1238. else if (requestType == "OpenDevice")
  1239. {
  1240. Serialize<ServiceRequestOpenDevice>((ServiceRequestOpenDevice)sr);
  1241. }
  1242. else if (requestType == "CloseDevice")
  1243. {
  1244. Serialize<ServiceRequestCloseDevice>((ServiceRequestCloseDevice)sr);
  1245. }
  1246. else if (requestType == "GetCountrySettings")
  1247. {
  1248. Serialize<ServiceRequestGetCountrySettings>((ServiceRequestGetCountrySettings)sr);
  1249. }
  1250. else if (requestType == "GetDSPLimits")
  1251. {
  1252. Serialize<ServiceRequestGetDSPLimits>((ServiceRequestGetDSPLimits)sr);
  1253. }
  1254. else if (requestType == "ChangeDSPLimits")
  1255. {
  1256. Serialize<ServiceRequestChangeDSPLimits>((ServiceRequestChangeDSPLimits)sr);
  1257. }
  1258. else if (requestType == "SuspendFuelling")
  1259. {
  1260. Serialize<ServiceRequestSuspendFuelling>((ServiceRequestSuspendFuelling)sr);
  1261. }
  1262. else if (requestType == "ResumeFuelling")
  1263. {
  1264. Serialize<ServiceRequestResumeFuelling>((ServiceRequestResumeFuelling)sr);
  1265. }
  1266. else if (requestType == "LockTank")
  1267. {
  1268. Serialize<ServiceRequestLockTank>((ServiceRequestLockTank)sr);
  1269. }
  1270. else if (requestType == "UnlockTank")
  1271. {
  1272. Serialize<ServiceRequestUnlockTank>((ServiceRequestUnlockTank)sr);
  1273. }
  1274. else if (requestType == "GetTankData")
  1275. {
  1276. Serialize<ServiceRequestGetTankData>((ServiceRequestGetTankData)sr);
  1277. }
  1278. else if (requestType == "ReserveFuelPoint")
  1279. {
  1280. Serialize<ServiceRequestReserveFuelPoint>((ServiceRequestReserveFuelPoint)sr);
  1281. }
  1282. else if (requestType == "FreeFuelPoint")
  1283. {
  1284. Serialize<ServiceRequestFreeFuelPoint>((ServiceRequestFreeFuelPoint)sr);
  1285. }
  1286. else if (requestType == "StartFuelPointTest")
  1287. {
  1288. Serialize<ServiceRequestStartFuelPointTest>((ServiceRequestStartFuelPointTest)sr);
  1289. }
  1290. else if (requestType == "EndFuelPointTest")
  1291. {
  1292. Serialize<ServiceRequestEndFuelPointTest>((ServiceRequestEndFuelPointTest)sr);
  1293. }
  1294. else if (requestType == "OpenFuelPoint")
  1295. {
  1296. Serialize<ServiceRequestOpenFuelPoint>((ServiceRequestOpenFuelPoint)sr);
  1297. }
  1298. else if (requestType == "CloseFuelPoint")
  1299. {
  1300. Serialize<ServiceRequestCloseFuelPoint>((ServiceRequestCloseFuelPoint)sr);
  1301. }
  1302. else if (requestType == "SetDeviceAlarm")
  1303. {
  1304. Serialize<ServiceRequestSetDeviceAlarm>((ServiceRequestSetDeviceAlarm)sr);
  1305. }
  1306. else if (requestType == "OPTAdd")
  1307. {
  1308. Serialize<ServiceRequestOPTAdd>((ServiceRequestOPTAdd)sr);
  1309. }
  1310. else if (requestType == "OPTRemove")
  1311. {
  1312. Serialize<ServiceRequestOPTRemove>((ServiceRequestOPTRemove)sr);
  1313. }
  1314. else if (requestType == "OPTWrite")
  1315. {
  1316. if (FDCGlobal.ProtocolVersion <= FDCVersion.V0007)
  1317. Serialize<ServiceRequestOPTWriteV07>((ServiceRequestOPTWriteV07)sr);
  1318. else
  1319. Serialize<ServiceRequestOPTWrite>((ServiceRequestOPTWrite)sr);
  1320. }
  1321. else if (requestType == "ConfigStart")
  1322. {
  1323. Serialize<ServiceRequestConfigStart>((ServiceRequestConfigStart)sr);
  1324. }
  1325. else if (requestType == "ConfigEnd")
  1326. {
  1327. Serialize<ServiceRequestConfigEnd>((ServiceRequestConfigEnd)sr);
  1328. }
  1329. else if (requestType == "DefProducts")
  1330. {
  1331. Serialize<ServiceRequestDefProducts>((ServiceRequestDefProducts)sr);
  1332. }
  1333. else if (requestType == "DefGrades" || requestType == "DefGrade")
  1334. {
  1335. Serialize<ServiceRequestDefGrades>((ServiceRequestDefGrades)sr);
  1336. }
  1337. else if (requestType == "AssignGradePars")
  1338. {
  1339. Serialize<ServiceRequestAssignGradePars>((ServiceRequestAssignGradePars)sr);
  1340. }
  1341. else if (requestType == "DefTanks")
  1342. {
  1343. Serialize<ServiceRequestDefTanks>((ServiceRequestDefTanks)sr);
  1344. }
  1345. else if (requestType == "DefTankSuctions")
  1346. {
  1347. Serialize<ServiceRequestDefTankSuctions>((ServiceRequestDefTankSuctions)sr);
  1348. }
  1349. else if (requestType == "DefFuelMode" || requestType == "DefFPFuelModes")
  1350. {
  1351. Serialize<ServiceRequestDefFuelMode>((ServiceRequestDefFuelMode)sr);
  1352. }
  1353. else if (requestType == "DefFuellingMode" || requestType == "DefFCFuelMode")
  1354. {
  1355. Serialize<ServiceRequestDefFuellingMode>((ServiceRequestDefFuellingMode)sr);
  1356. }
  1357. else if (requestType == "DefFuelPoint")
  1358. {
  1359. Serialize<ServiceRequestDefFuelPoint>((ServiceRequestDefFuelPoint)sr);
  1360. }
  1361. else if (requestType == "DefFuelPoints")
  1362. {
  1363. Serialize<ServiceRequestDefFuelPoints>((ServiceRequestDefFuelPoints)sr);
  1364. }
  1365. else if (requestType == "AssignGrades")
  1366. {
  1367. Serialize<ServiceRequestAssignGrades>((ServiceRequestAssignGrades)sr);
  1368. }
  1369. else if (requestType == "AssignMeters")
  1370. {
  1371. Serialize<ServiceRequestAssignMeters>((ServiceRequestAssignMeters)sr);
  1372. }
  1373. else if (requestType == "AssignTanks")
  1374. {
  1375. Serialize<ServiceRequestAssignTanks>((ServiceRequestAssignTanks)sr);
  1376. }
  1377. else if (requestType == "DefPricePole" | requestType == "DefPricePoles")
  1378. {
  1379. Serialize<ServiceRequestDefPricePole>((ServiceRequestDefPricePole)sr);
  1380. }
  1381. else if (requestType == "LoadPriceset")
  1382. {
  1383. Serialize<ServiceRequestLoadPriceset>((ServiceRequestLoadPriceset)sr);
  1384. }
  1385. else if (requestType == "DefTankMonitor" || requestType == "DefTankMonitors")
  1386. {
  1387. Serialize<ServiceRequestDefTankMonitor>((ServiceRequestDefTankMonitor)sr);
  1388. }
  1389. else if (requestType == "GetFuelPrice")
  1390. {
  1391. Serialize<ServiceRequestGetFuelPrices>((ServiceRequestGetFuelPrices)sr);
  1392. }
  1393. else if (requestType == "DefFPOperationModes")
  1394. {
  1395. Serialize<ServiceRequestDefFPOperationModes>((ServiceRequestDefFPOperationModes)sr);
  1396. }
  1397. else if (requestType == "CloseReconciliationPeriod")
  1398. {
  1399. Serialize<ServiceRequestCloseReconciliationPeriod>((ServiceRequestCloseReconciliationPeriod)sr);
  1400. }
  1401. else if (requestType == "GetTankReconciliation")
  1402. {
  1403. Serialize<ServiceRequestGetTankReconciliation>((ServiceRequestGetTankReconciliation)sr);
  1404. }
  1405. else if (requestType == "GetTankDelivery")
  1406. {
  1407. Serialize<ServiceRequestGetTankDelivery>((ServiceRequestGetTankDelivery)sr);
  1408. }
  1409. else if (requestType == "StartFuelPoint")
  1410. {
  1411. Serialize((ServiceRequestStartFuelPoint)sr);
  1412. }
  1413. else if (requestType == "StopFuelPoint")
  1414. {
  1415. Serialize((ServiceRequestStopFuelPoint)sr);
  1416. }
  1417. else
  1418. {
  1419. DebugLog(string.Format("!!!!! Unhandled SendRequest RequestType: {0}, requestId={1}", requestType, requestId));
  1420. }
  1421. }
  1422. }
  1423. catch (Exception ex)
  1424. {
  1425. DebugLog("ifsfmessages_OnMessageEnqueued RequestType:" + requestType + " RequestId:" + requestId + " Exception! " + ex.ToString());
  1426. if (mainConnectionState != DeviceConnectionState.Disconnected)
  1427. {
  1428. this.ifsfManager.Disconnect();
  1429. mainConnectionState = DeviceConnectionState.Disconnected;
  1430. }
  1431. }
  1432. DebugLog(string.Format("ifsfmessages_OnMessageEnqueued end RequestType: {0}", requestType));
  1433. }
  1434. }
  1435. }