示例#1
0
        /// <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>
 /// Default constructor.
 /// </summary>
 /// <param name="server">TCP server.</param>
 /// <param name="session">TCP server session.</param>
 internal TCP_ServerSessionEventArgs(TCP_Server <T> server, T session)
 {
     m_pServer  = server;
     m_pSession = session;
 }