void FindAndRemoveTransaction(SmppAsyncObject AsyncObject) { lock (PendingResponse.SyncRoot) { lock (PendingQueue.SyncRoot) { AsyncObject.DisposeTimer(); if (PendingResponse.Contains(AsyncObject.Request.Header.SequenceNumber)) { PendingResponse.Remove(AsyncObject.Request.Header.SequenceNumber); if ((PendingQueue.Count > 0) && TcpConnection.Connected) { for (int i = 0; i < PendingQueue.Count; i++) { var obj2 = PendingQueue[i] as SmppAsyncObject; lock (obj2.SyncRoot) { if (obj2.AsyncState == SmppAsyncObject.SmppAsyncState.Enabled) { PendingQueue.Remove(obj2); obj2.StartTimer(); PendingResponse.Add(obj2.Request.Header.SequenceNumber, obj2); SendResPdu(obj2.Request.ToByteArray()); goto Label_0142; } } } } } else { PendingQueue.Remove(AsyncObject); } } Label_0142: ; } }
internal SmppEnquireLinkRes EnquireLink(SmppEnquireLinkReq RequestPdu) { var asyncObject = new SmppAsyncObject(); SendReqPdu(RequestPdu, ref asyncObject); return(new SmppEnquireLinkRes(asyncObject.PduRes)); }
internal void InitializeAsyncTransac(ISmppPdu RequestObj, object Callback, object State) { if (RequestObj.Header.SequenceNumber == 0) { RequestObj.Header.SequenceNumber = GetSequenceNumber(); } var obj2 = new SmppAsyncObject(Callback, State, RequestObj, Settings.Timeout); obj2.CompletionCallback = ProcessAsyncPdu; lock (PendingResponse.SyncRoot) { lock (PendingQueue.SyncRoot) { obj2.StartTimer(); if (PendingResponse.Count < Settings.WindowSize) { PendingResponse.Add(RequestObj.Header.SequenceNumber, obj2); try { SendResPdu(RequestObj.ToByteArray()); goto Label_00ED; } catch { PendingResponse.Remove(RequestObj.Header.SequenceNumber); throw; } } PendingQueue.Add(obj2); } Label_00ED: ; } }
void SendReqPdu(ISmppPdu RequestPdu, ref SmppAsyncObject AsyncObject) { if (RequestPdu.Header.SequenceNumber == 0) { RequestPdu.Header.SequenceNumber = GetSequenceNumber(); } AsyncObject.mre = new ManualResetEvent(false); AddPendingResponse(RequestPdu.Header.SequenceNumber, AsyncObject); if (TcpConnection.Send(RequestPdu.ToByteArray()) <= 0) { RemovePendingResponse(RequestPdu.Header.SequenceNumber); TcpConnection.Disconnect(); throw new Exception("Invalid connection State"); } if (!AsyncObject.mre.WaitOne(Settings.Timeout, false) || (AsyncObject.PduRes == null)) { RemovePendingResponse(RequestPdu.Header.SequenceNumber); if (Settings.DisconnectOnTimeout) { TcpConnection.Disconnect(); } throw new SmppTimeOutException(); } var header = new SmppHeader(AsyncObject.PduRes); if (header.CommandId == -2147483648) { RemovePendingResponse(RequestPdu.Header.SequenceNumber); throw new SmppGenericNackException(SmppStatusCodes.GetDescription(RequestPdu.Header.CommandStatus)); } RemovePendingResponse(RequestPdu.Header.SequenceNumber); }
internal SmppSubmitSmRes SubmitSm(SmppSubmitSmReq RequestPdu) { var asyncObject = new SmppAsyncObject(); SendReqPdu(RequestPdu, ref asyncObject); return(new SmppSubmitSmRes(asyncObject.PduRes)); }
internal void UnBind() { if (TcpConnection.Connected) { var requestPdu = new SmppUnBindReq(); var asyncObject = new SmppAsyncObject(); SendReqPdu(requestPdu, ref asyncObject); TcpConnection.Disconnect(); } LastBindRes = null; }
internal SmppBindRes Bind() { SmppBindRes res = null; SmppBindReq requestPdu = null; lock (this) { try { if (Connected) { throw new Exception("You must disconnect before connect again", new SocketException(0x186a1)); } TcpConnection.RemoteHost = Settings.RemoteHost; TcpConnection.RemotePort = Settings.RemotePort; TcpConnection.LocalHost = Settings.LocalHost; TcpConnection.LocalPort = Settings.LocalPort; TcpConnection.BufferSize = Settings.SocketBufferSize; ConnectionSuccess = false; TcpConnection.Connect(); } catch (Exception exception) { _LastException = exception; res = new SmppBindRes(new SmppHeader(Settings.ConnectionMode.Value, GetSequenceNumber())); res.Header.CommandStatus = 0x15f95; return(res); } requestPdu = new SmppBindReq(Settings.ConnectionMode.Value, GetSequenceNumber(), Settings.BindParams); var asyncObject = new SmppAsyncObject(); SendReqPdu(requestPdu, ref asyncObject); res = new SmppBindRes(asyncObject.PduRes); if (res.Header.CommandStatus != 0) { TcpConnection.Disconnect(); return(res); } PendingResponse.Clear(); ConnectionSuccess = true; } return(res); }
void TcpConnection_DataReceived(object sender, SockClient.DataReceivedEventArgs e) { LastReceptionTime = DateTime.Now; while (TcpConnection.ReadSize() >= 0x10) { int bytes = SmppConverter.FromByteArrayToInt32(TcpConnection.PeekBytes(4)); if (bytes > Settings.MaxPduSize) { TcpConnection.Disconnect(); return; } if (bytes > TcpConnection.ReadSize()) { break; } var bb = new ByteBuilder(TcpConnection.ReadByteArray(bytes, 0)); var header = new SmppHeader(bb); SmppAsyncObject asyncObject = FindAsyncObject(header.SequenceNumber); if (asyncObject != null) { asyncObject.PduRes = bb; if (asyncObject.mre != null) { asyncObject.mre.Set(); } else if (asyncObject.Callback != null) { new SmppCompletionCallbackHandler(ProcessAsyncPdu).BeginInvoke(asyncObject, SmppAsyncObject.SmppAsyncCompleted.Response, null, null); } } else { ProcessPdu(bb); } } }
internal void BeginBind(SmppBindResHandler Callback, object State) { lock (this) { var req = new SmppBindReq(Settings.ConnectionMode.Value, GetSequenceNumber(), Settings.BindParams); var obj2 = new SmppAsyncObject(); obj2.Callback = Callback; obj2.Request = req; obj2.State = State; obj2.Timeout = Settings.Timeout; PendingBind.Add(obj2); if (!Connected) { if (Binding) { goto Label_0142; } TcpConnection.RemoteHost = Settings.RemoteHost; TcpConnection.RemotePort = Settings.RemotePort; TcpConnection.LocalHost = Settings.LocalHost; TcpConnection.LocalPort = Settings.LocalPort; TcpConnection.BufferSize = Settings.SocketBufferSize; ConnectionSuccess = false; Binding = true; try { ThreadPool.QueueUserWorkItem(AsyncConnect); goto Label_0142; } catch { Binding = false; PendingBind.Remove(obj2); throw; } } ThreadPool.QueueUserWorkItem(AsyncNotifyConnect); Label_0142: ; } }
internal void BeginBind(SmppBindResHandler Callback, object State) { lock (this) { var req = new SmppBindReq(Settings.ConnectionMode.Value, GetSequenceNumber(), Settings.BindParams); var obj2 = new SmppAsyncObject(); obj2.Callback = Callback; obj2.Request = req; obj2.State = State; obj2.Timeout = Settings.Timeout; PendingBind.Add(obj2); if (!Connected) { if (Binding) goto Label_0142; TcpConnection.RemoteHost = Settings.RemoteHost; TcpConnection.RemotePort = Settings.RemotePort; TcpConnection.LocalHost = Settings.LocalHost; TcpConnection.LocalPort = Settings.LocalPort; TcpConnection.BufferSize = Settings.SocketBufferSize; ConnectionSuccess = false; Binding = true; try { ThreadPool.QueueUserWorkItem(AsyncConnect); goto Label_0142; } catch { Binding = false; PendingBind.Remove(obj2); throw; } } ThreadPool.QueueUserWorkItem(AsyncNotifyConnect); Label_0142: ; } }
internal void InitializeAsyncTransac(ISmppPdu RequestObj, object Callback, object State) { if (RequestObj.Header.SequenceNumber == 0) RequestObj.Header.SequenceNumber = GetSequenceNumber(); var obj2 = new SmppAsyncObject(Callback, State, RequestObj, Settings.Timeout); obj2.CompletionCallback = ProcessAsyncPdu; lock (PendingResponse.SyncRoot) { lock (PendingQueue.SyncRoot) { obj2.StartTimer(); if (PendingResponse.Count < Settings.WindowSize) { PendingResponse.Add(RequestObj.Header.SequenceNumber, obj2); try { SendResPdu(RequestObj.ToByteArray()); goto Label_00ED; } catch { PendingResponse.Remove(RequestObj.Header.SequenceNumber); throw; } } PendingQueue.Add(obj2); } Label_00ED: ; } }
void FindAndRemoveTransaction(SmppAsyncObject AsyncObject) { lock (PendingResponse.SyncRoot) { lock (PendingQueue.SyncRoot) { AsyncObject.DisposeTimer(); if (PendingResponse.Contains(AsyncObject.Request.Header.SequenceNumber)) { PendingResponse.Remove(AsyncObject.Request.Header.SequenceNumber); if ((PendingQueue.Count > 0) && TcpConnection.Connected) for (int i = 0; i < PendingQueue.Count; i++) { var obj2 = PendingQueue[i] as SmppAsyncObject; lock (obj2.SyncRoot) { if (obj2.AsyncState == SmppAsyncObject.SmppAsyncState.Enabled) { PendingQueue.Remove(obj2); obj2.StartTimer(); PendingResponse.Add(obj2.Request.Header.SequenceNumber, obj2); SendResPdu(obj2.Request.ToByteArray()); goto Label_0142; } } } } else PendingQueue.Remove(AsyncObject); } Label_0142: ; } }
void ProcessAsyncPdu(SmppAsyncObject AsyncObject, SmppAsyncObject.SmppAsyncCompleted CompletionReason) { try { Exception exception = null; object obj2 = null; FindAndRemoveTransaction(AsyncObject); switch (CompletionReason) { case SmppAsyncObject.SmppAsyncCompleted.Response: { var header = new SmppHeader(AsyncObject.PduRes); if (header.CommandId != -2147483648) break; exception = new SmppGenericNackException(); goto Label_008E; } case SmppAsyncObject.SmppAsyncCompleted.Timeout: exception = new SmppTimeOutException(); Interlocked.Increment(ref ConsecutiveTimeouts); if (ConsecutiveTimeouts > Settings.WindowSize) BeginUnBind(); goto Label_008E; case SmppAsyncObject.SmppAsyncCompleted.Disconnection: exception = new SmppInvalidConnectionStateException(); goto Label_008E; default: goto Label_008E; } obj2 = new SmppSubmitSmRes(AsyncObject.PduRes); ConsecutiveTimeouts = 0; Label_008E: switch (AsyncObject.Request.Header.CommandId) { case 3: { var args3 = new SmppAsyncQuerySmResEventArgs(); args3._State = AsyncObject.State; args3._ProcessException = exception; args3._RequestPdu = AsyncObject.Request as SmppQuerySmReq; args3._ResponsePdu = obj2 as SmppQuerySmRes; var handler3 = AsyncObject.Callback as SmppQuerySmResHandler; handler3(this, args3); return; } case 4: { var args = new SmppAsyncSubmitSmResEventArgs(); args._State = AsyncObject.State; args._ProcessException = exception; args._RequestPdu = AsyncObject.Request as SmppSubmitSmReq; args._ResponsePdu = obj2 as SmppSubmitSmRes; var handler = AsyncObject.Callback as SmppSubmitSmResHandler; handler(this, args); return; } case 0x15: break; default: return; } var e = new SmppAsyncEnquireLinkResEventArgs(); e._State = AsyncObject.State; e._ProcessException = exception; e._RequestPdu = AsyncObject.Request as SmppEnquireLinkReq; e._ResponsePdu = obj2 as SmppEnquireLinkRes; var callback = AsyncObject.Callback as SmppAsyncEnquireLinkResHandler; callback(this, e); } catch (Exception exception2) { log.Error("Devshock: " + exception2); } }
void ProcessAsyncPdu(SmppAsyncObject AsyncObject, SmppAsyncObject.SmppAsyncCompleted CompletionReason) { try { Exception exception = null; object obj2 = null; FindAndRemoveTransaction(AsyncObject); switch (CompletionReason) { case SmppAsyncObject.SmppAsyncCompleted.Response: { var header = new SmppHeader(AsyncObject.PduRes); if (header.CommandId != -2147483648) { break; } exception = new SmppGenericNackException(); goto Label_008E; } case SmppAsyncObject.SmppAsyncCompleted.Timeout: exception = new SmppTimeOutException(); Interlocked.Increment(ref ConsecutiveTimeouts); if (ConsecutiveTimeouts > Settings.WindowSize) { BeginUnBind(); } goto Label_008E; case SmppAsyncObject.SmppAsyncCompleted.Disconnection: exception = new SmppInvalidConnectionStateException(); goto Label_008E; default: goto Label_008E; } obj2 = new SmppSubmitSmRes(AsyncObject.PduRes); ConsecutiveTimeouts = 0; Label_008E: switch (AsyncObject.Request.Header.CommandId) { case 3: { var args3 = new SmppAsyncQuerySmResEventArgs(); args3._State = AsyncObject.State; args3._ProcessException = exception; args3._RequestPdu = AsyncObject.Request as SmppQuerySmReq; args3._ResponsePdu = obj2 as SmppQuerySmRes; var handler3 = AsyncObject.Callback as SmppQuerySmResHandler; handler3(this, args3); return; } case 4: { var args = new SmppAsyncSubmitSmResEventArgs(); args._State = AsyncObject.State; args._ProcessException = exception; args._RequestPdu = AsyncObject.Request as SmppSubmitSmReq; args._ResponsePdu = obj2 as SmppSubmitSmRes; var handler = AsyncObject.Callback as SmppSubmitSmResHandler; handler(this, args); return; } case 0x15: break; default: return; } var e = new SmppAsyncEnquireLinkResEventArgs(); e._State = AsyncObject.State; e._ProcessException = exception; e._RequestPdu = AsyncObject.Request as SmppEnquireLinkReq; e._ResponsePdu = obj2 as SmppEnquireLinkRes; var callback = AsyncObject.Callback as SmppAsyncEnquireLinkResHandler; callback(this, e); } catch (Exception exception2) { log.Error("Devshock: " + exception2); } }
void SendReqPdu(ISmppPdu RequestPdu, ref SmppAsyncObject AsyncObject) { if (RequestPdu.Header.SequenceNumber == 0) RequestPdu.Header.SequenceNumber = GetSequenceNumber(); AsyncObject.mre = new ManualResetEvent(false); AddPendingResponse(RequestPdu.Header.SequenceNumber, AsyncObject); if (TcpConnection.Send(RequestPdu.ToByteArray()) <= 0) { RemovePendingResponse(RequestPdu.Header.SequenceNumber); TcpConnection.Disconnect(); throw new Exception("Invalid connection State"); } if (!AsyncObject.mre.WaitOne(Settings.Timeout, false) || (AsyncObject.PduRes == null)) { RemovePendingResponse(RequestPdu.Header.SequenceNumber); if (Settings.DisconnectOnTimeout) TcpConnection.Disconnect(); throw new SmppTimeOutException(); } var header = new SmppHeader(AsyncObject.PduRes); if (header.CommandId == -2147483648) { RemovePendingResponse(RequestPdu.Header.SequenceNumber); throw new SmppGenericNackException(SmppStatusCodes.GetDescription(RequestPdu.Header.CommandStatus)); } RemovePendingResponse(RequestPdu.Header.SequenceNumber); }
internal SmppSubmitSmRes SubmitSm(SmppSubmitSmReq RequestPdu) { var asyncObject = new SmppAsyncObject(); SendReqPdu(RequestPdu, ref asyncObject); return new SmppSubmitSmRes(asyncObject.PduRes); }
internal SmppEnquireLinkRes EnquireLink(SmppEnquireLinkReq RequestPdu) { var asyncObject = new SmppAsyncObject(); SendReqPdu(RequestPdu, ref asyncObject); return new SmppEnquireLinkRes(asyncObject.PduRes); }
void AddPendingResponse(int TransactionKey, SmppAsyncObject AsyncObject) { lock (PendingResponse.SyncRoot) { PendingResponse.Add(TransactionKey, AsyncObject); } }
internal SmppBindRes Bind() { SmppBindRes res = null; SmppBindReq requestPdu = null; lock (this) { try { if (Connected) throw new Exception("You must disconnect before connect again", new SocketException(0x186a1)); TcpConnection.RemoteHost = Settings.RemoteHost; TcpConnection.RemotePort = Settings.RemotePort; TcpConnection.LocalHost = Settings.LocalHost; TcpConnection.LocalPort = Settings.LocalPort; TcpConnection.BufferSize = Settings.SocketBufferSize; ConnectionSuccess = false; TcpConnection.Connect(); } catch (Exception exception) { _LastException = exception; res = new SmppBindRes(new SmppHeader(Settings.ConnectionMode.Value, GetSequenceNumber())); res.Header.CommandStatus = 0x15f95; return res; } requestPdu = new SmppBindReq(Settings.ConnectionMode.Value, GetSequenceNumber(), Settings.BindParams); var asyncObject = new SmppAsyncObject(); SendReqPdu(requestPdu, ref asyncObject); res = new SmppBindRes(asyncObject.PduRes); if (res.Header.CommandStatus != 0) { TcpConnection.Disconnect(); return res; } PendingResponse.Clear(); ConnectionSuccess = true; } return res; }