private void OnSocketRelayFinished(SocketRelay item) { lock (m_listSR) { if (m_listSR.Contains(item)) { m_listSR.Remove(item); } } }
private void AcceptCallback(IAsyncResult ar) { Socket sktActive = null; try { var state = (Tuple <Socket, EventWaitHandle>)ar.AsyncState; state.Item2.Set(); sktActive = state.Item1.EndAccept(ar); } catch (ObjectDisposedException) { // Accept has been cancelled. return; } try { // 安全验证 //if (!m_confirm.Confirm(sktActive)) //{ // sktActive.Close(); // return; //} Socket sktPassive = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sktPassive.Connect(m_epDest); SocketRelay relay = new SocketRelay(sktActive, sktPassive); relay.OnFinished += OnSocketRelayFinished; relay.Run(); lock (m_listSR) m_listSR.Add(relay); } catch (SocketException ex) { string strbuf = String.Format("{0}({1})", ex.Message, ex.ErrorCode); byte[] buf = Encoding.Default.GetBytes(strbuf); sktActive.Send(buf); sktActive.Shutdown(SocketShutdown.Both); sktActive.Close(); } }