protected override void RunNow() { while (CanContinue()) { try { PDU pdu = WaitPDU(); if (pdu is RequestPDU) { vProcessorCallback.BeginInvoke( (RequestPDU)pdu, AsyncCallBackProcessPduRequest, null); } else if (pdu is ResponsePDU) { vResponseHandler.Handle(pdu as ResponsePDU); } } catch (PDUException) { /*Silent catch*/ } catch (TcpIpException) { /*Silent catch*/ } catch (Exception ex) { _Log.ErrorFormat("200015:Unanticipated stream parser exception: {0}", ex, ex.Message); if (vTraceSwitch.TraceError) { Trace.WriteLine(string.Format( "200015:Unanticipated stream parser exception:{0};", ex)); } throw; } } }
private void Open(int timeOut) { try { if (Monitor.TryEnter(vConnSyncRoot)) { //No thread is in a connecting or reconnecting state if (vState != SmppConnectionState.Closed) { vLastException = new InvalidOperationException("You cannot open while the instance is already connected"); throw vLastException; } // SessionBindInfo bindInfo = null; bool useSepConn = false; lock (vProperties.SyncRoot) { bindInfo = vProperties.GetBindInfo(); useSepConn = vProperties.InterfaceVersion == InterfaceVersion.v33; } try { OpenSession(bindInfo, useSepConn, timeOut); } catch (Exception ex) { _Log.ErrorFormat("OpenSession: {0}", ex, ex.Message); if (vTraceSwitch.TraceError) { Trace.TraceError(ex.ToString()); } vLastException = ex; throw; } vLastException = null; } else { //Another thread is already in either a connecting or reconnecting state //Wait until the thread finishes Monitor.Enter(vConnSyncRoot); //Now, the thread has finished connecting, //Check on the result if the thread encountered any problem during connection if (vLastException != null) { throw vLastException; } } } finally { Monitor.Exit(vConnSyncRoot); } }
public virtual void GetMessageText(out string message, out Udh udh) { message = null; udh = null; byte[] msgBytes = GetMessageBytes(); if (msgBytes == null) { return; } ByteBuffer buffer = new ByteBuffer(msgBytes); //Check if the UDH is set in the esm_class field if ((EsmClass & EsmClass.UdhiIndicator) == EsmClass.UdhiIndicator) { _Log.Info("200020:UDH field presense detected;"); if (vTraceSwitch.TraceInfo) { Trace.WriteLine("200020:UDH field presense detected;"); } try { udh = Udh.Parse(buffer, vSmppEncodingService); } catch (Exception ex) { _Log.ErrorFormat("20023:UDH field parsing error - {0}", ex, new ByteBuffer(msgBytes).DumpString()); if (vTraceSwitch.TraceError) { Trace.WriteLine(string.Format( "20023:UDH field parsing error - {0} {1};", new ByteBuffer(msgBytes).DumpString(), ex.Message)); } throw; } } //Check if we have something remaining in the buffer if (buffer.Length == 0) { return; } try { message = vSmppEncodingService.GetStringFromBytes(buffer.ToBytes(), DataCoding); } catch (Exception ex1) { _Log.ErrorFormat("200019:SMS message decoding failure - {0}", ex1, new ByteBuffer(msgBytes).DumpString()); if (vTraceSwitch.TraceError) { Trace.WriteLine(string.Format( "200019:SMS message decoding failure - {0} {1};", new ByteBuffer(msgBytes).DumpString(), ex1.Message)); } throw; } }
private void SendPduBase(PDU pdu) { if (pdu == null) { throw new ArgumentNullException("pdu"); } if (!(CheckState(pdu) && (pdu.AllowedSource & SmppEntityType.ESME) == SmppEntityType.ESME)) { throw new SmppException(SmppErrorCode.ESME_RINVBNDSTS, "Incorrect bind status for given command"); } try { vTrans.Send(pdu); } catch (Exception ex) { ByteBuffer buffer = new ByteBuffer(pdu.GetBytes()); _Log.ErrorFormat("200022:PDU send operation failed - {0}", ex, buffer.DumpString()); if (vTraceSwitch.TraceInfo) { Trace.WriteLine(string.Format( "200022:PDU send operation failed - {0} {1};", buffer.DumpString(), ex.Message)); } } }
private async Task ExecuteAsyncInternal(CancellationToken stoppingToken) { string line = null; try { line = _reader.ReadLine(); } catch (Exception ex) { _Log.ErrorFormat("Error while reading lines from {0}.", ex, _reader.FilePath); } if (line == null) { _monitoringStep++; var delay = GetDelayForStep(_monitoringStep, _monitoringDelays); await Task.Delay(delay, stoppingToken); return; } _monitoringStep = 0; try { var pdu = _parser.Parse(line); if (pdu != null) { _collection.Add(pdu); } } catch (Exception ex) { _Log.ErrorFormat("Error while parsing PDU from {0}: {1}", ex, _reader.FilePath, line); } }
private static void SendMessage(string command) { var parts = command.Split(' '); var dest = parts[1]; var msgTxt = string.Join(" ", parts, 2, parts.Length - 2); if (string.IsNullOrEmpty(msgTxt)) { msgTxt = @"السلام عليكم ورحمة الله وبركاته هذه رسالة عربية متعددة الاسطر"; } TextMessage msg = new TextMessage(); msg.DestinationAddress = dest; //Receipient number msg.SourceAddress = smppConfig.SourceAddress; //Originating number //msg.Text = "Hello, this is my test message!"; msg.Text = msgTxt; msg.RegisterDeliveryNotification = true; //I want delivery notification for this message msg.UserMessageReference = GenerateUserMessageReference(smppConfig.UserMessageReferenceType); _Log.DebugFormat($"msg.UserMessageReference: {msg.UserMessageReference}"); try { client.SendMessage(msg); } catch (SmppException smppEx) { _Log.ErrorFormat("smppEx.ErrorCode:({0}) {1} ", (int)smppEx.ErrorCode, smppEx.ErrorCode); _Log.Error(smppEx); } catch (Exception e) { _Log.Error("SendMessage:" + e.Message, e); } //client.BeginSendMessage(msg, SendMessageCompleteCallback, client); }
/// <summary> /// Callback method called by the NetworkStream's thread when a message /// is sent. /// </summary> /// <param name="state">The state object holding information about /// the connection.</param> private void SendComplete(IAsyncResult state) { #if HARDCORE_LOGGING _Log.DebugFormat("Instance {0} - {1} => Send completed.", this.GetHashCode(), _ThreadId); #endif try { using (new ReadOnlyLock(_socketLock)) { #if HARDCORE_LOGGING _Log.DebugFormat("Instance {0} - {1} => Finishing sent operation..", this.GetHashCode(), _ThreadId); #endif if (!_SendPending) { _Log.ErrorFormat("Instance {0} - {1} => SendComplete called while SendPending=False?!?!?", this.GetHashCode(), _ThreadId); } _SendPending = false; if (_TcpClient != null) { _TcpClient.GetStream().EndWrite(state); } } } catch (Exception ex) { _Log.Warn(string.Format("Instance {0} - {1} => Async send failed.", this.GetHashCode(), _ThreadId), ex); } // If there are more packets to send.. lock (_SendQueue) { #if HARDCORE_LOGGING _Log.DebugFormat("Instance {0} - {1} => Processing send queue.", this.GetHashCode(), _ThreadId); #endif if (_SendQueue.Count == 0) { #if HARDCORE_LOGGING _Log.DebugFormat("Instance {0} - {1} => No packets on queue..", this.GetHashCode(), _ThreadId); #endif // Reduce queue internal space.. _SendQueue.TrimExcess(); return; } #if HARDCORE_LOGGING _Log.DebugFormat("Instance {0} - {1} => Sending queued packet.", this.GetHashCode(), _ThreadId); #endif // Send another packet.. using (new ReadOnlyLock(_socketLock)) { _SendPending = true; var packet = _SendQueue.Dequeue(); _TcpClient.GetStream().BeginWrite(packet, 0, packet.Length, _CallbackWriteMethod, null); } } }
public void ErrorFormat(IFormatProvider provider, String format, params Object[] args) { _log.ErrorFormat(provider, format, args); }