/// <summary> /// This method is called when BeginAccept ha completed. /// </summary> /// <param name="ar">The result of the asynchronous operation.</param> private void AsynSocketsAcceptCompleted(IAsyncResult ar) { ListeningPoint lPoint = (ListeningPoint)ar.AsyncState; try{ ProcessConnection(lPoint.Socket.EndAccept(ar), lPoint.BindInfo); } catch { // Skip accept errors. } // Begin accepting connection. lPoint.Socket.BeginAccept(new AsyncCallback(this.AsynSocketsAcceptCompleted), lPoint); }
/// <summary> /// Starts listening incoming connections. NOTE: All active listening points will be disposed. /// </summary> private void StartListen() { try { // Dispose all old binds. foreach (ListeningPoint listeningPoint in m_pListeningPoints.ToArray()) { try { listeningPoint.Socket.Close(); } catch (Exception x) { OnError(x); } } m_pListeningPoints.Clear(); // Create new listening points and start accepting connections. bool ioCompletion_asyncSockets = Net_Utils.IsIoCompletionPortsSupported(); foreach (IPBindInfo bind in m_pBindings) { try { Socket socket = null; if (bind.IP.AddressFamily == AddressFamily.InterNetwork) { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } else if (bind.IP.AddressFamily == AddressFamily.InterNetworkV6) { socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); } else { // Invalid address family, just skip it. continue; } socket.Bind(new IPEndPoint(bind.IP, bind.Port)); socket.Listen(100); ListeningPoint listeningPoint = new ListeningPoint(socket, bind); m_pListeningPoints.Add(listeningPoint); // Begin accept. // We MUST use socket.AcceptAsync method, this consume all threading power in Windows paltform(IO completion ports). // For other platforms we need to use BeginAccept. #region IO completion ports if (ioCompletion_asyncSockets) { SocketAsyncEventArgs eArgs = new SocketAsyncEventArgs(); eArgs.Completed += delegate(object s, SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { ProcessConnection(e.AcceptSocket, bind); } // Start accepting new connection. IOCompletionBeginAccept(e, socket, bind); }; // Move processing to thread-pool, because IOCompletionBeginAccept keeps using calling thread as loang as there is work todo. ThreadPool.QueueUserWorkItem(delegate { // Start accepting new connection. IOCompletionBeginAccept(eArgs, socket, bind); }); } #endregion #region Async sockets else { // Begin accepting connection. socket.BeginAccept(new AsyncCallback(AsynSocketsAcceptCompleted), listeningPoint); } #endregion } catch (Exception x) { // The only exception what we should get there is if socket is in use. OnError(x); } } } catch (Exception x) { OnError(x); } }
/// <summary> /// Starts listening incoming connections. NOTE: All active listening points will be disposed. /// </summary> private void StartListen() { try{ // Dispose all old binds. foreach (ListeningPoint listeningPoint in m_pListeningPoints.ToArray()) { try{ listeningPoint.Socket.Close(); } catch (Exception x) { OnError(x); } } m_pListeningPoints.Clear(); // Create new listening points and start accepting connections. foreach (IPBindInfo bind in m_pBindings) { try{ Socket socket = null; if (bind.IP.AddressFamily == AddressFamily.InterNetwork) { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } else if (bind.IP.AddressFamily == AddressFamily.InterNetworkV6) { socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); } else { // Invalid address family, just skip it. continue; } socket.Bind(new IPEndPoint(bind.IP, bind.Port)); socket.Listen(100); ListeningPoint listeningPoint = new ListeningPoint(socket, bind); m_pListeningPoints.Add(listeningPoint); // Create TCP connection acceptors. for (int i = 0; i < 10; i++) { TCP_Acceptor acceptor = new TCP_Server <T> .TCP_Acceptor(socket); acceptor.Tags["bind"] = bind; acceptor.ConnectionAccepted += delegate(object s1, EventArgs <Socket> e1){ // NOTE: We may not use 'bind' variable here, foreach changes it's value before we reach here. ProcessConnection(e1.Value, (IPBindInfo)acceptor.Tags["bind"]); }; acceptor.Error += delegate(object s1, ExceptionEventArgs e1){ OnError(e1.Exception); }; m_pConnectionAcceptors.Add(acceptor); acceptor.Start(); } } catch (Exception x) { // The only exception what we should get there is if socket is in use. OnError(x); } } } catch (Exception x) { OnError(x); } }
/// <summary> /// Starts listening incoming connections. NOTE: All active listening points will be disposed. /// </summary> private void StartListen() { try { // Dispose all old acceptors. foreach (var acceptor in m_pConnectionAcceptors.ToArray()) { try { acceptor.Dispose(); } catch (Exception ex) { OnError(ex); } } m_pConnectionAcceptors.Clear(); // Dispose all old binds. foreach (var listeningPoint in m_pListeningPoints.ToArray()) { try { listeningPoint.Socket.Dispose(); } catch (Exception ex) { OnError(ex); } } m_pListeningPoints.Clear(); // Create new listening points and start accepting connections. foreach (var bindInfo in m_pBindings) { try { Socket socket = null; if (bindInfo.IP.AddressFamily == AddressFamily.InterNetwork) { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } else if (bindInfo.IP.AddressFamily == AddressFamily.InterNetworkV6) { socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); } else { // Invalid address family, just skip it. continue; } socket.Bind(bindInfo.EndPoint); socket.Listen(100); var listeningPoint = new ListeningPoint(socket, bindInfo); m_pListeningPoints.Add(listeningPoint); // Create TCP connection acceptors. for (int i = 0; i < m_AcceptorsPerSocket; i++) { var acceptor = new TCP_Acceptor(socket); acceptor.Tags["bind"] = bindInfo; acceptor.ConnectionAccepted += (s1, e1) => { // NOTE: We may not use 'bind' variable here, foreach changes it's value before we reach here. ProcessConnection(e1.Value, (IPBindInfo)acceptor.Tags["bind"]); }; acceptor.Error += (s1, e1) => { OnError(e1.Exception); }; m_pConnectionAcceptors.Add(acceptor); acceptor.Start(); } } catch (Exception ex) { // The only exception what we should get there is if socket is in use. OnError(ex); } } } catch (Exception ex) { OnError(ex); } }
private void OnPhoneCallStarted(IInternalPhoneCall phoneCall) { Check.Require(phoneCall, "phoneCall"); Check.IsTrue(InternalState == _stateProvider.GetIdle(), string.Format("Failed to start the call. The phone can only start calls while in 'IDLE' state. CurrentState: '{0}'", CurrentState)); Check.IsTrue(_isRunning, "Failed to start the call. The phone must be started first."); _pendingPhoneCall = phoneCall; if (_logger.IsInfoEnabled) { _logger.Info("Starting new phonecall..."); } if (_logger.IsDebugEnabled) { _logger.Debug("Creating 'INVITE' request..."); } var thisUri = AddressFactory.CreateUri(string.Empty, ListeningPoint.ToString()); var requestUri = phoneCall.GetToUri(); var toAddress = AddressFactory.CreateAddress(string.Empty, phoneCall.GetToUri()); var fromAddress = AddressFactory.CreateAddress(string.Empty, thisUri); var toHeader = HeaderFactory.CreateToHeader(toAddress); var fromHeader = HeaderFactory.CreateFromHeader(fromAddress, SipUtil.CreateTag()); var cseqHeader = HeaderFactory.CreateSCeqHeader(SipMethods.Invite, MessageCounter++); var callIdheader = HeaderFactory.CreateCallIdHeader(SipUtil.CreateCallId()); var viaHeader = HeaderFactory.CreateViaHeader(ListeningPoint.Address, ListeningPoint.Port, SipConstants.Udp, SipUtil.CreateBranch()); var maxForwardsHeader = HeaderFactory.CreateMaxForwardsHeader(1); var request = MessageFactory.CreateRequest( requestUri, SipMethods.Invite, callIdheader, cseqHeader, fromHeader, toHeader, viaHeader, maxForwardsHeader); /*add contactheader*/ var contactUri = AddressFactory.CreateUri(thisUri.User, viaHeader.SentBy.ToString()); var contactHeader = HeaderFactory.CreateContactHeader(contactUri); request.Contacts.Add(contactHeader); if (_logger.IsDebugEnabled) { _logger.Debug("'INVITE' request created. Sending to callee.."); } var clientTransaction = SipProvider.CreateClientTransaction(request); var dialog = SipProvider.CreateClientDialog(clientTransaction as SipInviteClientTransaction); clientTransaction.SendRequest(); if (_logger.IsDebugEnabled) { _logger.Debug("'INVITE' sent."); } if (_logger.IsInfoEnabled) { _logger.Info("Request sent. Transitioning to 'WAITPROVISIONAL' state..."); } PendingInvite = new InviteInfo() { OriginalRequest = request, From = request.From.SipUri, To = request.To.SipUri, InviteClientTransaction = (SipInviteClientTransaction)clientTransaction, IsIncomingCall = false, Dialog = dialog }; ChangeState(_stateProvider.GetWaitProvisional()); }