private void ReenterReciveCallBack(MySocket m_s) { EndPoint endPoint = m_RemoteEndPoint; m_s.BeginReceiveFrom (m_Buffer, 0, c_buffCapacity, SocketFlags.None, ref endPoint, new AsyncCallback(ReciveCallBack), m_s); return; }
private void Receive() { //Activating the call back we must be sure the buffer is empty; m_Position = int.MaxValue; m_DataLength = 0; bool exceptionHasOccured = false; Debug.Assert(m_ReceiveNotStarted.State, "CAS.Lib.CommonBus.CommunicationLayer.Net.Net_to_Serial.Receive(): Reentered Receive method"); Debug.Assert(IsConnected, "Reveive may be called only in Connected state"); try { // Reset must be called befor BeginReceiveFrom // based on experiments we have discovered that sometimes // callback is executed on the same thread as this method!! // note: that also in Exception catched we have to Set m_receiveNotStarted m_ReceiveNotStarted.Reset(); EndPoint endPoint = m_RemoteEndPoint; m_Socket.BeginReceiveFrom (m_Buffer, 0, c_buffCapacity, SocketFlags.None, ref endPoint, new AsyncCallback(ReciveCallBack), m_Socket); Debug.Assert(this.EndPointsAreEqual(m_RemoteEndPoint, endPoint), "Provider has changed the remote endpoint."); } catch (SocketException ex) { exceptionHasOccured = true; MarkSocketException(ex, 266); } catch (ObjectDisposedException ex) { exceptionHasOccured = true; MarkSocketException(ex, 269); } catch (Exception ex) { exceptionHasOccured = true; MarkException(String.Format(m_ExceptionMesage, ex.Message), TraceEventType.Error, 273, ex); } finally { if (exceptionHasOccured) { // the exception comes from m_Socket.BeginReceiveFrom only and it indicates that // callback and m_ReceiveNotStarted.Set() never occur, so we have to call it here: m_ReceiveNotStarted.Set(); } } }
void Run() { try { CreateSocket(); while (!Terminated) { try { MySocket.BeginReceiveFrom(ReceiveBuf, 0, ReceiveBuf.Length, SocketFlags.None, ref RemoteEP, new AsyncCallback(ReceiveCallback), MySocket); while (!Terminated) { Thread.Sleep(50); SSUSession[] sessions; lock ( NeedsCpu ) { sessions = NeedsCpu.ToArray(); } if (sessions.Length > 0) { RunBatchWait batchsync = new RunBatchWait(sessions.Length); foreach (var sess in sessions) { ThreadPool.QueueUserWorkItem(cb => RunSession(sess, batchsync)); } if (!batchsync.WaitOne(5000)) { Logging.LogTransport("SSUHost: Run tasks counting error."); } } if (FailedSessions.Count > 0) { SSUSession sess; while ((sess = PopFailedSession()) != null) { Logging.LogTransport($"SSUHost: Failed Session {sess.DebugId} removed."); if (sess.RemoteEP != null) { ReportEPProblem(sess.RemoteEP); } RemoveSession(sess); sess.Terminate(); } } } } catch (ThreadAbortException ex) { Logging.Log(ex); } catch (Exception ex) { Logging.Log(ex); } } } finally { Terminated = true; Worker = null; } }