/// <summary> /// This method sends a deliver_sm packet synchronously over to the peer. /// </summary> /// <param name="pdu">deliver_sm packet</param> /// <returns>deliver_sm response</returns> public deliver_sm_resp DeliverSm(deliver_sm pdu) { deliver_sm_resp rpdu = null; PduSyncronizer evt = AddWaitingPdu(pdu); if (IsBound && SendPdu(pdu)) { SmppPdu pduR = evt.PduResponse; if ((pduR as deliver_sm_resp) != null) { rpdu = (deliver_sm_resp)pduR; } else { rpdu = new deliver_sm_resp(); rpdu.Status = pduR.Status; } } else { FindAndRemoveWaitingPdu(pdu.SequenceNumber); rpdu = new deliver_sm_resp(); rpdu.Status = StatusCodes.ESME_RDELIVERYFAILURE; } return(rpdu); }
/// <summary> /// This method sends a PDU on the socket session /// </summary> /// <param name="pdu"></param> /// <returns></returns> internal bool SendPdu(SmppPdu pdu) { if (sock_.IsConnected) { try { FireEvent(EventType.PostProcessPdu, new SmppEventArgs(this, pdu)); } catch { } try { SmppByteStream bs = new SmppByteStream(); pdu.Serialize(new SmppWriter(bs, this.version_)); sock_.Send(bs.GetBuffer()); return(true); } catch (System.Net.Sockets.SocketException ex) { sock_.Close(true); FireEvent(EventType.SessionDisconnected, new SmppDisconnectEventArgs(this, ex)); } } return(false); }
/// <summary> /// This method is used to process a PDU /// </summary> /// <param name="pdu">PDU</param> protected void ProcessPdu(SmppPdu pdu) { // Dispatch using the intermediate PduHandler class which will correctly // cast the base Pdu into a concrete type and invoke the proper method for // the passed state object. if (!PduHandler.Dispatch(state_, pdu)) { // Unrecognized PDU received from partner; notify handler. FireEvent(EventType.ReceivedUnknownPdu, new SmppEventArgs(this, pdu)); } }
/// <summary> /// This adds a PDU to our waiting list. /// </summary> /// <param name="pdu">PDU</param> /// <returns>True/False</returns> internal PduSyncronizer AddWaitingPdu(SmppPdu pdu) { lock (this.pendingRequests) { if (this.pendingRequests.Count < DefaultPendingRequestLimit) { PduSyncronizer sync = new PduSyncronizer(pdu, ResponseTimeout); this.pendingRequests.Add(pdu.SequenceNumber, sync); return(sync); } } return(null); }
private void OnError(object sender, SmppEventArgs ea) { SmppErrorEventArgs eea = (SmppErrorEventArgs)ea; SmppPdu pdu = eea.PDU; if (pdu != null) { AddLogText(StatusCode.Error, string.Format("{0} PDU: {1}", eea.Session, pdu.CommandId)); } else { AddLogText(StatusCode.Error, string.Format("{0}", eea.Session)); } Exception ex = eea.Exception; while (ex != null) { AddLogText(StatusCode.Error, ex.ToString()); ex = ex.InnerException; } }
private void OnBind(object sender, SmppEventArgs ea) { SmppPdu pdu = ea.PDU; if ((pdu as bind_receiver) != null || (pdu as bind_transmitter) != null) { ea.ResponsePDU.Status = StatusCodes.ESME_RINVCMDID; } else { bind_transceiver btpdu = (bind_transceiver)pdu; if (btpdu.Password != password_) { ea.ResponsePDU.Status = StatusCodes.ESME_RINVPASWD; return; } bind_transceiver_resp resp = (bind_transceiver_resp)ea.ResponsePDU; resp.InterfaceVersion = (byte)supportedVersion_; } // Update the listview. new System.Threading.Timer(new TimerCallback(UpdateListItem), ea.Session.Tag, 100, Timeout.Infinite); }
/// <summary> /// Constructor for the Smpp EventHandler class. /// </summary> /// <param name="session">The SMPP session this event corresponds to.</param> /// <param name="pdu">PDU this event will represent.</param> /// <param name="pduOut">PDU this event will return.</param> internal SmppEventArgs(SmppSession session, SmppPdu pdu, SmppPdu pduOut) { session_ = session; pduIn_ = pdu; pduOut_ = pduOut; }
/// <summary> /// Constructor for the Smpp EventHandler class. /// </summary> /// <param name="session">The SMPP session this event corresponds to.</param> /// <param name="pdu">PDU this event will represent.</param> internal SmppEventArgs(SmppSession session, SmppPdu pdu) { session_ = session; pduIn_ = pdu; }
internal SmppPduReceivedEventArgs(SmppSession session, SmppPdu pdu) : base(session, pdu) { }
internal SmppErrorEventArgs(SmppSession session, Exception ex, SmppPdu pdu) : base(session, pdu) { ex_ = ex; }
/// <summary> /// Primary constructor for the PduException class /// </summary> /// <param name="pdu">PDU</param> /// <param name="s">message</param> /// <param name="innerException">Inner exception caught</param> public PduException(SmppPdu pdu, string s, System.Exception innerException) : base(s, innerException) { pdu_ = pdu; }
/// <summary> /// Primary constructor for the PduException class /// </summary> /// <param name="pdu">PDU</param> /// <param name="innerException">Inner exception caught</param> public PduException(SmppPdu pdu, System.Exception innerException) : base("", innerException) { pdu_ = pdu; }
/// <summary> /// Primary constructor for the PduException class /// </summary> /// <param name="pdu">PDU</param> /// <param name="s">message</param> public PduException(SmppPdu pdu, string s) : base(s) { pdu_ = pdu; }
/// <summary> /// Primary constructor for the IncompletePduException /// </summary> public IncompletePduException(SmppPdu pdu, Exception e) : base(pdu, "Not enough data in buffer for PDU.", e) { }
/// <summary> /// Constructor for the PduSyncronizer /// </summary> /// <param name="pdu">PDU to wait on</param> /// <param name="responseTimeout">Time before request is droped</param> public PduSyncronizer(SmppPdu pdu, TimeSpan responseTimeout) { this.pduRequest = pdu; this.responseTimeout = responseTimeout; }
/// <summary> /// This method is called when data is received from our socket. /// </summary> /// <param name="sender">The socket which sent the data</param> /// <param name="args">Socket data</param> protected virtual void OnReceiveData(object sender, SocketEventArgs args) { SocketClient socket = (SocketClient)sender; ReadEventArgs re = (ReadEventArgs)args; int packetLength = SmppPdu.REQUIRED_SIZE; if (re.Length >= packetLength) { // Get the first DWORD from the buffer; this is the total size of the packet. packetLength = BitConverter.ToInt32(new byte[] { re.Buffer[3], re.Buffer[2], re.Buffer[1], re.Buffer[0] }, 0); if (re.Length >= packetLength) { try { // Have the entire buffer; parse it out. SmppPdu pdu = SmppPdu.Create(new SmppByteStream(re.Buffer)); SmppPduReceivedEventArgs ea = new SmppPduReceivedEventArgs(this, pdu); FireEvent(EventType.PreProcessPdu, ea); PduSyncronizer sync = null; if (pdu is SmppResponse) { if ((sync = FindAndRemoveWaitingPdu(pdu.SequenceNumber)) != null) { ((SmppResponse)pdu).OriginalRequest = (SmppRequest)sync.PduRequest; } else { throw new SmppException("Invalid pdu response received with no pending request: " + pdu.ToString()); } } if (!ea.Handled) { try { ProcessPdu(pdu); } catch (InvalidSmppStateException) { if (pdu.RequiresResponse) { SendPdu(new generic_nack(StatusCodes.ESME_RINVCMDID, pdu.SequenceNumber)); } } } if (sync != null) { sync.PduResponse = pdu; } } catch (PduException pduex) { SmppPdu pdu = (pduex.HasPDU) ? pduex.PDU : null; if (pdu != null && pdu.RequiresResponse) { SendPdu(new generic_nack(StatusCodes.ESME_RINVCMDID, pdu.SequenceNumber)); } FireEvent(EventType.Error, new SmppErrorEventArgs(this, pduex, pdu)); } catch (Exception ex) { socket.Close(true); FireEvent(EventType.SessionDisconnected, new SmppDisconnectEventArgs(this, ex)); } // Reset the buffer re.AppendToBuffer = false; re.NextReadSize = SmppPdu.REQUIRED_SIZE; return; } } // Wait for more data to show up on the socket. re.AppendToBuffer = true; re.NextReadSize = packetLength - re.Length; }