protected override void Run() { while (this.running) { try { if (this.mQueueConvert.Count >= 1) { SECSBlock mHSMSItem = null; lock (this.syncObject) { mHSMSItem = this.mQueueConvert.Dequeue(); } if (mHSMSItem != null) { this.Parse(mHSMSItem); } else { this.logger.Debug("Parser#Run Invoked. block ==null"); } continue; } } catch (Exception exception) { this.logger.Error("Parser#Run", exception); if (this.OnParseError != null) { this.OnParseError(string.Format("{0}: {1}", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ParseError), exception.Message)); } } Thread.Sleep(50); } }
public void Parse(SECSBlock mHSMSItem) { try { SECSDecoding decoding = new SECSDecoding(); string str = ByteStringBuilder.ToLogString(mHSMSItem.Header); string str2 = ByteStringBuilder.ToLogString(mHSMSItem.DataItem); SECSMessage msg = decoding.Byte_TO_SecsMessage(mHSMSItem.Header); msg.Root = decoding.Byte_TO_SecsItem(mHSMSItem.DataItem); msg.Header = mHSMSItem.Header; //this.logger.Info(string.Format("[RECV] S{0}F{1} {2} System Bytes={3} {4} {5}", new object[] //{ // msg.Stream, // msg.Function, // msg.WBit ? "W" : "", // msg.SystemBytes, // str, // str2 //})); //this.logger.Warn("[RECV] " + SecsItem2Str.GetSecsMessageStr(msg)); if (this.OnReceived != null) { this.OnReceived(msg); } } catch (Exception exception) { this.logger.Error("Parser#Parse", exception); if (this.OnParseError != null) { this.OnParseError(string.Format("{0}: {1}", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ParseError), exception.Message)); } } }
protected override void Run() { while (this.running) { byte[] bs = new byte[4]; try { this.ReadLength(bs); int aLength = (int)Byte2SecsValue.GetInt(bs); if (aLength > 0) { SECSBlock block = this.ByteToBlock(this.ReadBody(aLength)); if (this.OnReadCompleted != null) { this.OnReadCompleted(block); } } else { Thread.Sleep(50); } } catch (OutOfMemoryException exception) { GC.Collect(); GC.WaitForPendingFinalizers(); this.logger.Error("Reader#Run", exception); if (this.OnReadError != null) { this.OnReadError(string.Format("{0}: Out Of Memory.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError))); } this.Disconnect(exception.Message); } catch (IOException exception2) { this.logger.Error("Reader#Run", exception2); if (this.OnReadError != null) { this.OnReadError(string.Format("{0}: Socket Error.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError))); } this.Disconnect(exception2.Message); } catch (Exception exception3) { this.logger.Error("Reader#Run", exception3); if (this.OnReadError != null) { this.OnReadError(string.Format("{0}: {1}.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError), exception3.Message)); } this.Disconnect(exception3.Message); } } }
protected override void Run() { while (this.running) { try { if (this.Reconnect) { Thread.Sleep(this.socketInfo.ConnectInterval); if (this.socketInfo.IsActiveMode) { this.OpenActiveConnection(); } else { this.OpenPassiveConnection(); } } else { if (this.socketInfo.IsActiveMode) { this.OpenActiveConnection(); } else { this.OpenPassiveConnection(); } if (this.running) { Thread.Sleep(this.socketInfo.ConnectInterval); } } } catch (ThreadInterruptedException exception) { this.logger.Error("Connect Trace : " + exception.ToString()); if (this.OnReadError != null) { this.OnReadError(string.Format("{0}: {1}.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError), exception.Message)); } break; } catch (Exception exception2) { this.logger.Error("CONNECT Error", exception2); if (this.OnReadError != null) { this.OnReadError(string.Format("{0}: {1}.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError), exception2.Message)); } } } }
internal void NotifyEVENT(SECSEventType type, SECSTransaction trans, SECSErrors err) { lock (this.syncObject) { SECS1EventArgs args = new SECS1EventArgs { EventType = type, ErrorCode = err, ErrorMsg = SECSErrorsMessage.GetSECSErrorMessage(err), Trans = trans }; this.eventQueue.Enqueue(args); } }
private void HandleRcvdPrimaryMessage(SECSMessage rcvd) { if (this.OnHSMSEvent != null) { SECSTransaction trans = new SECSTransaction { Primary = rcvd, SystemBytes = rcvd.SystemBytes, Secondary = null, ExpectReply = rcvd.WBit, DeviceID = rcvd.DeviceIdID }; this.OnHSMSEvent(SECSEventType.PrimaryRcvd, trans, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } }
private void OpenPassiveConnection() { try { TcpListener listener = new TcpListener(IPAddress.Parse(this.socketInfo.IpAddress), int.Parse(this.socketInfo.Port)); listener.Start(); TcpClient socket = listener.AcceptTcpClient(); listener.Stop(); this.OnConnected(socket); this.running = false; } catch (Exception exception) { this.logger.Error(exception.Message); if (this.OnReadError != null) { this.OnReadError(string.Format("{0}: {1}.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError), exception.Message)); } } }
internal void NotifyERROR(SECSErrors err, SECSMessage msg) { lock (this.syncObject) { SECS1EventArgs args = new SECS1EventArgs { EventType = SECSEventType.Error, ErrorCode = err, ErrorMsg = SECSErrorsMessage.GetSECSErrorMessage(err) }; if (msg != null) { args.Trans = msg.Transaction; } else { args.Trans = null; } this.eventQueue.Enqueue(args); } }
public void OpenActiveConnection() { try { IPEndPoint point = new IPEndPoint(IPAddress.Parse(this.socketInfo.IpAddress), int.Parse(this.socketInfo.Port)); TcpClient socket = new TcpClient(); socket.Connect(point); if (this.socketInfo.Timeout == 0) { socket.ReceiveTimeout = 6000; socket.SendTimeout = 6000; } else { socket.ReceiveTimeout = this.socketInfo.Timeout + 1000; socket.SendTimeout = this.socketInfo.Timeout + 1000; } if (this.OnConnected == null || !this.running) { socket.Close(); } else { this.running = false; this.OnConnected(socket); } this.running = false; } catch (Exception exception) { if (this.running) { this.logger.Error(exception.Message, exception); if (this.OnReadError != null) { this.OnReadError(string.Format("{0}: {1}.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError), exception.Message)); } } } }
private void OnTimeout(TimerPara aPara) { try { if (aPara.Msg != null && this.mNeedReplyMsg.ContainsKey(aPara.Msg.SystemBytes)) { lock (this.syncNeedReplyObject) { this.mNeedReplyMsg.Remove(aPara.Msg.SystemBytes); } } eTimeout type = aPara.Type; if (type != eTimeout.LinkTest) { switch (type) { case eTimeout.T8: if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.Error, null, SECSErrors.T8TimeOut, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.T8TimeOut)); } this.OnDisconnect(SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.T8TimeOut)); break; case eTimeout.T7: if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.Error, null, SECSErrors.T7TimeOut, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.T7TimeOut)); } this.OnDisconnect(SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.T7TimeOut)); break; case eTimeout.T6: if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.Error, null, SECSErrors.T6TimeOut, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.T6TimeOut)); } this.OnDisconnect(SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.T6TimeOut)); break; case eTimeout.T5: if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.Error, null, SECSErrors.T5TimeOut, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.T5TimeOut)); } break; case eTimeout.T3: if (this.OnHSMSEvent != null) { SECSTransaction trans = aPara.Msg.Transaction; trans.Secondary = null; this.OnHSMSEvent(SECSEventType.Error, trans, SECSErrors.T3TimeOut, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.T3TimeOut)); } break; } } else { long ctrlSystemBytes = this.GetCtrlSystemBytes(); this.mHsmsWriter.WriteControlMessage(ctrlSystemBytes, 0, eControlMessage.LINKTEST_REQ, 255, 255); this.mHsmsTimer.StartLinkTestTimer(); } } catch (Exception exception) { this.logger.Debug("Port#OnTimeout", exception); } }
private void OnSocketWriteComplete(bool IsReply, SECSMessage msg) { try { SECSTransaction trans = msg.Transaction; if (this.OnHSMSEvent != null) { if (IsReply) { this.OnHSMSEvent(SECSEventType.SecondarySent, trans, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } else { trans.DeviceID = msg.DeviceIdID; trans.SystemBytes = msg.SystemBytes; trans.ExpectReply = msg.WBit; this.OnHSMSEvent(SECSEventType.PrimarySent, trans, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } } } catch (Exception exception) { this.logger.Error("Port#Sending Error", exception); } }
private void OnDisconnect(string errmsg) { try { if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.HSMSDisconnected, null, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } lock (this.syncObject) { if (this.NowStatus >= eHSMS_PORT_STATUS.CONNECT) { this.logger.Error("HSMSPort::OnDisconnect Reconnect"); this.UpdateStatus(eHSMS_PORT_STATUS.DISCONNECT); this.StopHsmsReader(); this.StopHsmsWriter(); this.mHsmsTimer.StopLinkTestTimer(); this.TerminateSocket(); this.StartHsmsConnector(); } } } catch (Exception exception) { this.logger.Error("HSMSPort::OnDisconnect Error. ", exception); } }
private void OnConnected(TcpClient client) { try { lock (this.syncObject) { this.logger.Debug(string.Format("HSMSPort::OnConnected Status={0}", this.NowStatus)); this.UpdateStatus(eHSMS_PORT_STATUS.CONNECT); this.mSECSSocket = client; this.mSECSSocket.ReceiveTimeout = 0; this.reader = new BinaryReader(this.mSECSSocket.GetStream()); this.writer = new BinaryWriter(this.mSECSSocket.GetStream()); this.StartHsmsReader(); this.StartHsmsWriter(); this.mHsmsTimer.StartLinkTestTimer(); this.StopHsmsConnector(); if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.HSMSConnected, null, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } } if (this.portCfg.ConnectionMode == eHSMS_CONNECT_MODE.ACTIVE) { long ctrlSystemBytes = this.GetCtrlSystemBytes(); this.mHsmsWriter.WriteControlMessage(ctrlSystemBytes, 0, eControlMessage.SELECT_REQ, 255, 255); } } catch (Exception exception) { this.logger.Error("HSMSPort::OnConnected", exception); } }
private void HandleRcvdUnknownMessage(SECSMessage rcvd) { SECSTransaction trans = new SECSTransaction { Primary = null, Secondary = rcvd }; if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.Error, trans, SECSErrors.RcvdUnknownMessage, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.RcvdUnknownMessage)); } }
private void HandleRcvdSecondaryMessage(SECSMessage rcvd, SECSMessage sent) { if (this.OnHSMSEvent != null) { SECSTransaction trans = sent.Transaction; trans.Secondary = rcvd; this.OnHSMSEvent(SECSEventType.SecondaryRcvd, trans, SECSErrors.None, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.None)); } }
internal void NofityRECV(SECSMessage msg) { lock (this.syncObject) { SECS1EventArgs args = new SECS1EventArgs { ErrorCode = SECSErrors.None, ErrorMsg = "" }; if (msg.Function % 2 == 0) { if (this.msgWaitingReply.ContainsKey(msg.SystemBytes)) { SECSMessage message = this.msgWaitingReply[msg.SystemBytes]; this.secs1.StopTimer(message); this.msgWaitingReply.Remove(msg.SystemBytes); if (msg.Stream < 1 || msg.Function < 1) { SECSTransaction transaction = message.Transaction; transaction.Secondary = msg; args.EventType = SECSEventType.Error; args.ErrorCode = SECSErrors.RcvdAbortMessage; args.ErrorMsg = SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.RcvdAbortMessage); args.Trans = transaction; } else { SECSTransaction transaction = message.Transaction; transaction.Secondary = msg; args.EventType = SECSEventType.SecondaryRcvd; args.Trans = transaction; } } else { SECSTransaction transaction = new SECSTransaction { Primary = null, Secondary = msg }; args.EventType = SECSEventType.Error; args.ErrorCode = SECSErrors.RcvdUnknownMessage; args.ErrorMsg = SECSErrorsMessage.GetSECSErrorMessage(args.ErrorCode); args.Trans = transaction; } } else { SECSTransaction transaction = new SECSTransaction { Primary = msg, SystemBytes = msg.SystemBytes, Secondary = null, ExpectReply = msg.WBit, DeviceID = msg.DeviceIdID }; args.EventType = SECSEventType.PrimaryRcvd; args.Trans = transaction; } this.eventQueue.Enqueue(args); } }
private void HandleRcvdAbortMessage(SECSMessage sent, SECSMessage rcvd) { SECSTransaction trans = sent.Transaction; trans.Secondary = rcvd; if (this.OnHSMSEvent != null) { this.OnHSMSEvent(SECSEventType.Error, trans, SECSErrors.RcvdAbortMessage, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.RcvdAbortMessage)); } }
protected override void Run() { while (this.running) { try { if (this.mQueueReply.Count > 0) { SECSMessage msg = null; lock (this.syncReplyObject) { msg = this.mQueueReply.Dequeue(); } if (msg != null) { this.logger.Debug("Writer#Run Send Secondary Message " + msg.SystemBytes.ToString()); this.WriteReplyMessage(msg); } } if (this.mQueueSend.Count > 0) { SECSMessage message2 = null; lock (this.syncSendObject) { message2 = this.mQueueSend.Dequeue(); } if (message2 != null) { this.logger.Debug("Writer#Run Send Primary Message " + message2.SystemBytes.ToString()); this.WriteSendMessage(message2); } } if (this.mQueueSend.Count > 0 || this.mQueueReply.Count > 0) { continue; } } catch (Exception exception) { this.logger.Error("Writer#Run: ", exception); if (this.OnWriteError != null) { this.OnWriteError(SECSEventType.Error, null, string.Format("{0}: {1}", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.WriteError), exception.Message)); } this.FireDisconnect(exception.Message); } Thread.Sleep(50); } }
public void WriteSendMessage(SECSMessage msg) { if (msg == null) { this.logger.Error("WriteSendMessage msg == null"); return; } byte[] bs = null; try { bs = new SECSEncoding(msg).GetEncodingData((int)msg.DeviceIdID, msg.WBit, msg.SystemBytes); if (bs.Length > 0 && msg.WBit) { this.mHsmsTimer.StartT3Timer(msg); this.logger.Debug(string.Format("WriteSendMessage: StartT3Timer {0}", msg.SystemBytes)); } } catch (Exception exception) { this.logger.Error("WriteSendMessage: encoder", exception); SECSTransaction t = msg.Transaction; if (this.OnWriteError != null) { this.OnWriteError(SECSEventType.PrimarySent, t, string.Format("{0}: Invalid SECS Message Format or Data.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.WriteError))); } } if (bs != null && bs.Length > 0) { try { this.writer.Write(bs); this.writer.Flush(); //this.logger.Info(string.Format("[SEND] S{0}F{1} {2} SystemBytes={3}\n{4}", new object[] //{ // msg.Stream, // msg.Function, // msg.WBit ? "W" : "", // msg.SystemBytes, // ByteStringBuilder.ToLogString(bs) //})); //this.logger.Warn("[SEND] " + SecsItem2Str.GetSecsMessageStr(msg)); if (this.OnWriteCompleted != null) { this.OnWriteCompleted(false, msg); } } catch (Exception exception2) { this.logger.Error("WriteSendMessage", exception2); SECSTransaction transaction = msg.Transaction; if (this.OnWriteError != null) { this.OnWriteError(SECSEventType.PrimarySent, transaction, string.Format("{0}: Socket Error.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.WriteError))); } this.FireDisconnect(exception2.Message); } } }
public void WriteControlMessage(long mSystemBytes, byte rspcode, eControlMessage stype, byte high, byte low) { try { byte[] bs = new byte[14]; bs[0] = 0; bs[1] = 0; bs[2] = 0; bs[3] = 10; byte[] intBytes = SecsValue2Byte.GetIntBytes((int)mSystemBytes, 4); bs[10] = intBytes[0]; bs[11] = intBytes[1]; bs[12] = intBytes[2]; bs[13] = intBytes[3]; bs[4] = high; bs[5] = low; bs[7] = rspcode; bs[9] = (byte)stype; if (stype == eControlMessage.SELECT_REQ) { this.mHsmsTimer.StartT6Timer(); } else if (stype == eControlMessage.LINKTEST_REQ) { this.mHsmsTimer.StartT6Timer(); } this.logger.Debug(string.Format("[WriteControlMessage] [{0}-{2}] -- {1}", mSystemBytes, ByteStringBuilder.ToLogString(bs), stype)); this.writer.Write(bs); this.writer.Flush(); } catch (Exception exception) { this.logger.Error("WriteControlMessage", exception); if (this.OnWriteError != null) { this.OnWriteError(SECSEventType.Error, null, string.Format("{0}: Socket Error.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.WriteError))); } } }
public void Send(SECSTransaction trans) { if (trans.Primary != null) { trans.Primary.Transaction = trans; } if (trans.Secondary != null) { trans.Secondary.Transaction = trans; } if (this.porttype != eSECS_PORT_TYPE.HSMS) { this.secs1Port.SendMessage(trans.Primary); return; } if (!this.PortIsOpen) { this.CallSECSEvent(SECSEventType.PrimarySent, trans, SECSErrors.PortNotOpen, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.PortNotOpen)); return; } if (!this.Connected) { this.CallSECSEvent(SECSEventType.PrimarySent, trans, SECSErrors.PortNotConnected, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.PortNotConnected)); return; } this.hsmsPort.SendMessages(trans.Primary); }
private void CheckConversationTimeout() { List <int> list = new List <int>(); for (int i = 0; i < this.conversationList.Count; i++) { if (this.conversationList[i].DeadLine < DateTime.Now) { list.Add(i); } } foreach (int num2 in list) { SECSS9FxMonitor.ConversactionItem item = this.conversationList[num2]; this.conversationList.RemoveAt(num2); SECSTransaction trans = this.secsPort.Library.FindTransaction("S9F13"); trans.Primary.Root.Item(1).Value = item.Transaction2Str; trans.Primary.Root.Item(2).Value = item.EDID; this.secsPort.Send(trans); this.secsPort.CallSECSEvent(SECSEventType.Error, item.Transaction, SECSErrors.ConversationTimeout, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ConversationTimeout)); } }
private bool CheckS9F357Exception(SECSEventType eventtype, SECSTransaction trans) { SECSMessage msg = null; if (eventtype == SECSEventType.PrimaryRcvd) { msg = trans.Primary; } else if (eventtype == SECSEventType.SecondaryRcvd) { msg = trans.Secondary; } if (this.secsPort.Library != null && msg != null) { if (!this.secsPort.Library.FindStream(msg.Stream)) { this.SendS9Fx(3, msg.Header); this.secsPort.CallSECSEvent(SECSEventType.Error, trans, SECSErrors.UnrecognizedStreamType, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.UnrecognizedStreamType)); return(false); } if (!this.secsPort.Library.FindFunction(msg.Stream, msg.Function)) { this.SendS9Fx(5, msg.Header); this.secsPort.CallSECSEvent(SECSEventType.Error, trans, SECSErrors.UnrecognizedFunctionType, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.UnrecognizedFunctionType)); return(false); } List <SECSMessage> list = this.secsPort.Library.FindMessage(msg.Stream, msg.Function); if (!this.CheckMsgFormat(msg, list)) { this.SendS9Fx(7, msg.Header); this.secsPort.CallSECSEvent(SECSEventType.Error, trans, SECSErrors.IllegalData, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.IllegalData)); return(false); } } return(true); }
private bool CheckS9F1Exception(SECSEventType eventtype, SECSTransaction trans) { if (eventtype == SECSEventType.PrimaryRcvd && trans != null && trans.Primary != null && trans.Primary.DeviceIdID != this.secsPort.DeviceID) { this.SendS9Fx(1, trans.Primary.Header); this.secsPort.CallSECSEvent(SECSEventType.Error, trans, SECSErrors.UnrecognizedDeviceID, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.UnrecognizedDeviceID)); return(false); } if (eventtype == SECSEventType.SecondaryRcvd && trans != null && trans.Secondary != null && trans.Secondary.DeviceIdID != this.secsPort.DeviceID) { this.SendS9Fx(1, trans.Secondary.Header); this.secsPort.CallSECSEvent(SECSEventType.Error, trans, SECSErrors.UnrecognizedDeviceID, SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.UnrecognizedDeviceID)); return(false); } return(true); }