示例#1
0
        // if bbeginread IS false it's caller's job to call DoAsyncRead()
        public SocketClient CreateClient(string ipaddr, int nport, bool bbeginread, SocketCreator creator, bool bIsIPV6)
        {
            IPAddress  hostadd = null;
            IPEndPoint EPhost  = null;

            try
            {
                if (IsIPAddress(ipaddr))
                {
                    EPhost = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ipaddr), nport);
                }
                else if (IsIPV6Address(ipaddr) == true)
                {
                    EPhost = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ipaddr), nport);
                }
                else
                {
                    hostadd = Resolve(ipaddr);
                    EPhost  = new IPEndPoint(hostadd, nport);
                }
            }
            catch (ArgumentNullException e)
            {
                LogError(MessageImportance.Highest, "EXCEPTION", e.ToString());
                return(null);
            }
            catch (SocketException e2)
            {
                LogError(MessageImportance.Highest, "CreateClient", string.Format("Exception calling Resolve(), Winsock Native: {0}, Socket: {1}\n{2}", e2.NativeErrorCode, e2.SocketErrorCode, e2));
                return(null);
            }

            return(CreateClient(EPhost, bbeginread, creator, bIsIPV6));
        }
示例#2
0
 protected void Init()
 {
     m_AsyncConnect    = new AsyncCallback(OnClientConnected);
     m_nPort           = 0;
     m_AcceptorManager = new AcceptorManager(this);
     AcceptHandler     = null;
     ReceiveHandler    = null;
     m_SocketError     = "";
     m_socketcreator   = null;
 }
示例#3
0
        public SocketClient CreateClient(IPEndPoint ep, bool bbeginread, SocketCreator creator, bool IsIPV6)
        {
            //Creates the Socket for sending data over TCP.
            Socket s = null;

            if (IsIPV6 == false)
            {
                s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            else
            {
                s = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
            }

            // Connects to the host using IPEndPoint.
            try
            {
                s.Connect(ep);
            }
            catch (SocketException e)
            {
                LogError(MessageImportance.Highest, "CreateClient", string.Format("Exception calling Connect, Winsock Native: {0}, Socket: {1}\n{2}", e.NativeErrorCode, e.SocketErrorCode, e));
                return(null);
            }

            if (!s.Connected)
            {
                LogError(MessageImportance.Highest, "Connection", "Failed to connect to " + ep.Address.ToString() + " port " + ep.Port);
                return(null);
            }

            SocketClient client = creator.CreateSocket(s, this);

            if (bbeginread)
            {
                client.DoAsyncRead();
            }
            return(client);
        }
示例#4
0
 public SocketClient CreateClient(IPEndPoint ep, bool bbeginread, SocketCreator creator)
 {
     return(CreateClient(ep, bbeginread, creator, false));
 }
示例#5
0
        // if bbeginread IS false it's caller's job to call DoAsyncRead()
        public SocketClient CreateClient(string ipaddr, int nport, bool bbeginread, SocketCreator creator, bool bIsIPV6)
        {
            IPAddress hostadd = null;
            IPEndPoint EPhost = null;
            try
            {
                if (IsIPAddress(ipaddr))
                {
                    EPhost = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ipaddr), nport);
                }
                else if (IsIPV6Address(ipaddr) == true)
                {
                    EPhost = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ipaddr), nport);
                }
                else
                {
                    hostadd = Resolve(ipaddr);
                    EPhost = new IPEndPoint(hostadd, nport);
                }
            }
            catch (ArgumentNullException e)
            {
                LogError(MessageImportance.Highest, "EXCEPTION", e.ToString());
                return null;
            }
            catch (SocketException e2)
            {
                LogError(MessageImportance.Highest, "CreateClient", string.Format("Exception calling Resolve(), Winsock Native: {0}, Socket: {1}\n{2}", e2.NativeErrorCode, e2.SocketErrorCode, e2));
                return null;
            }

            return CreateClient(EPhost, bbeginread, creator, bIsIPV6);
        }
示例#6
0
        internal void OnAcceptReceived(IAsyncResult ar)
        {
            System.Net.Sockets.Socket sman = (System.Net.Sockets.Socket)ar.AsyncState;

            if (sman != null)
            {
                System.Net.Sockets.Socket newsocket = null;
                try
                {
                    newsocket = sman.EndAccept(ar);
                }
                catch (System.ObjectDisposedException e)
                {
                    string strError = string.Format("Exception calling EndAccept {0}", e);
                    LogError(MessageImportance.Highest, "EXCEPTION", strError);
                    return;
                }
                catch (System.Exception eall)
                {
                    string strError = string.Format("Exception calling EndAccept - continueing {0}", eall);
                    LogError(MessageImportance.Highest, "EXCEPTION", strError);
                }

                /// Start a new accept
                try
                {
                    sman.BeginAccept(AcceptCallback, sman);
                }
                catch (SocketException e3) /// winso
                {
                    string strError = string.Format("Exception calling BeginAccept2 {0}", e3);
                    LogError(MessageImportance.Highest, "EXCEPTION", strError);
                }
                catch (ObjectDisposedException e4) // socket was closed
                {
                    string strError = string.Format("Exception calling BeginAccept2 {0}", e4);
                    LogError(MessageImportance.Highest, "EXCEPTION", strError);
                }
                catch (System.Exception eall)
                {
                    string strError = string.Format("Exception calling BeginAccept2 {0}", eall);
                    LogError(MessageImportance.Highest, "EXCEPTION", strError);
                }


                if (newsocket == null)
                {
                    return;
                }

                LogMessage(MessageImportance.Medium, "NEWCON", string.Format("Accepted new socket {0}", newsocket.Handle));
                /// look up the creator for this socket
                ///
                SocketCreator creator = (SocketCreator)this.m_SocketSet[sman];
                if (creator != null)
                {
                    SocketClient newclient = creator.AcceptSocket(newsocket, this.m_ConMgrParent);

                    try
                    {
                        m_ConMgrParent.FireAcceptHandler(newclient);
                    }
                    catch (System.Exception efire)
                    {
                        string strError = string.Format("Exception Fireing Acception Handler -{0}", efire);
                        LogError(MessageImportance.Highest, "EXCEPTION", strError);
                    }

                    newclient.DoAsyncRead();
                }
            }
        }
示例#7
0
        /// <summary>
        /// Starts listening for new connection son this socket
        /// </summary>
        /// <param name="ep"></param>
        /// <param name="creator"></param>
        internal bool EnableAccept( IPEndPoint ep, SocketCreator creator)
        {
            /// start listening on thisport
            /// Start the accept process
            ///

            /// First make sure no one we have is already listening on this port
            ///

            System.Net.EndPoint epBind  = (EndPoint)ep;

            System.Net.Sockets.Socket sman = new System.Net.Sockets.Socket(ep.AddressFamily/*AddressFamily.InterNetwork*/, SocketType.Stream, ProtocolType.IP);
            try
            {
                sman.Bind(epBind);
            }
             catch(SocketException e) /// winso
             {
            string strError = string.Format("Exception calling Bind {0}", e);
            LogError(MessageImportance.Highest, "EXCEPTION", strError );
            return false;
             }
             catch(ObjectDisposedException e2) // socket was closed
             {
            string strError = string.Format("Exception calling Bind {0}", e2);
            LogError(MessageImportance.Highest, "EXCEPTION", strError);
            return false;
             }
             catch(System.Exception ebind) // socket was closed
             {
            string strError = string.Format("Exception calling Bind {0}", ebind);
            LogError(MessageImportance.Highest, "EXCEPTION", strError);
            return false;
             }

            m_SocketSet.Add(sman, creator);

            try
            {
            //int nCon = Convert.ToInt32(sman.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.MaxConnections));
            //this.LogMessage(MessageImportance.High, "socket", string.Format("Max listen queue is {0}", nCon));
            sman.Listen((int)SocketOptionName.MaxConnections);
            }
             catch(SocketException e3) /// winso
             {
            string strError = string.Format("Exception calling Listen {0}", e3);
            LogError(MessageImportance.Highest, "EXCEPTION", strError );
            return false;
             }
             catch(ObjectDisposedException e4) // socket was closed
             {
            string strError = string.Format("Exception calling Listen {0}", e4);
            LogError(MessageImportance.Highest, "EXCEPTION", strError);
            return false;
             }
             catch(System.Exception eit) // socket was closed
             {
            string strError = string.Format("Exception calling Listen {0}", eit);
            LogError(MessageImportance.Highest, "EXCEPTION", strError);
            return false;
             }

             try
             {
            /// Multiple Accepts commented out by B.B. 3-23-2007.
            /// These cause 100% CPU on Windows 2003 Server whenever the first incoming
            /// connection is received - but works fine on Windows XP
            ///
            sman.BeginAccept(AcceptCallback, sman);

            /// 5.2 = windows 2003
            if (System.Environment.OSVersion.Version.Major >= 6)
            {
               //sman.BeginAccept(AcceptCallback, sman);
               //sman.BeginAccept(AcceptCallback, sman);
            }
            //sman.BeginAccept(AcceptCallback, sman);
            //sman.BeginAccept(AcceptCallback, sman);
            //sman.BeginAccept(AcceptCallback, sman);
             }
             catch(SocketException e5) /// winso
             {
            string strError = string.Format("Exception calling BeginAccept {0}", e5);
            LogError(MessageImportance.Highest, "EXCEPTION", strError );
            return false;
             }
             catch(ObjectDisposedException e6) // socket was closed
             {
            string strError = string.Format("Exception calling BeginAccept {0}", e6);
            LogError(MessageImportance.Highest, "EXCEPTION", strError);
            return false;
             }
             catch(System.Exception eall)
             {
            string strError = string.Format("Exception calling BeginAccept {0}", eall);
            LogError(MessageImportance.Highest, "EXCEPTION", strError);
            return false;
             }

            return true;
        }
示例#8
0
 public ConnectMgr() // default construct assumes is for Listening
 {
     Init();
     m_socketcreator = new SocketCreator();
 }
示例#9
0
 public bool ListenIPV6(int nPort, SocketCreator screator)
 {
     return m_AcceptorManager.EnableAcceptIPV6(nPort, screator);
 }
示例#10
0
 // default construct assumes is for Listening
 public ConnectMgr()
 {
     Init();
     m_socketcreator = new SocketCreator();
 }
示例#11
0
 public bool Listen(IPEndPoint ep, SocketCreator screator)
 {
     return m_AcceptorManager.EnableAccept(ep, screator);
 }
示例#12
0
        public SocketClient CreateClient(IPEndPoint localep, IPEndPoint remoteep, bool bbeginread, SocketCreator creator)
        {
            //Creates the Socket for sending data over TCP.
            Socket s = null;
            if (remoteep.Address.AddressFamily != AddressFamily.InterNetworkV6)
                s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            else
                s = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);

            // Connects to the host using IPEndPoint.
            try
            {
                LogMessage(MessageImportance.Highest, "EXCEPTION", string.Format("Attempting to create TCP connection from {0} to {1}", localep, remoteep));
                s.Bind(localep);
                s.Connect(remoteep);
            }
            catch (SocketException e)
            {
                LogError(MessageImportance.Highest, "CreateClient", string.Format("Exception calling Bind or Connect, Winsock Native: {0}, Socket: {1}\n{2}", e.NativeErrorCode, e.SocketErrorCode, e));
                return null;
            }

            if (!s.Connected)
            {
                LogError(MessageImportance.Highest, "Connection", "Failed to connect to " + remoteep);
                return null;
            }

            SocketClient client = creator.CreateSocket(s, this);
            if (bbeginread)
                client.DoAsyncRead();
            return client;
        }
示例#13
0
        public SocketClient CreateClient(IPEndPoint ep, bool bbeginread, SocketCreator creator, bool IsIPV6)
        {
            //Creates the Socket for sending data over TCP.
            Socket s = null;
            if (IsIPV6 == false)
                s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            else
                s = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);

            // Connects to the host using IPEndPoint.
            try
            {
                s.Connect(ep);
            }
            catch (SocketException e)
            {
                LogError(MessageImportance.Highest, "CreateClient", string.Format("Exception calling Connect, Winsock Native: {0}, Socket: {1}\n{2}", e.NativeErrorCode, e.SocketErrorCode, e));
                return null;
            }

            if (!s.Connected)
            {
                LogError(MessageImportance.Highest, "Connection", "Failed to connect to " + ep.Address.ToString() + " port " + ep.Port);
                return null;
            }

            SocketClient client = creator.CreateSocket(s, this);
            if (bbeginread)
                client.DoAsyncRead();
            return client;
        }
示例#14
0
 public SocketClient CreateClient(IPEndPoint ep, bool bbeginread, SocketCreator creator)
 {
     return CreateClient(ep, bbeginread, creator, false);
 }
示例#15
0
 protected void Init()
 {
     m_AsyncConnect = new AsyncCallback(OnClientConnected);
     m_nPort = 0;
     m_AcceptorManager = new AcceptorManager(this);
     AcceptHandler = null;
     ReceiveHandler = null;
     m_SocketError = "";
     m_socketcreator = null;
 }
示例#16
0
        public SocketClient CreateClient(IPEndPoint localep, IPEndPoint remoteep, bool bbeginread, SocketCreator creator)
        {
            //Creates the Socket for sending data over TCP.
            Socket s = null;

            if (remoteep.Address.AddressFamily != AddressFamily.InterNetworkV6)
            {
                s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            else
            {
                s = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
            }

            // Connects to the host using IPEndPoint.
            try
            {
                LogMessage(MessageImportance.Highest, "EXCEPTION", string.Format("Attempting to create TCP connection from {0} to {1}", localep, remoteep));
                s.Bind(localep);
                s.Connect(remoteep);
            }
            catch (SocketException e)
            {
                LogError(MessageImportance.Highest, "CreateClient", string.Format("Exception calling Bind or Connect, Winsock Native: {0}, Socket: {1}\n{2}", e.NativeErrorCode, e.SocketErrorCode, e));
                return(null);
            }

            if (!s.Connected)
            {
                LogError(MessageImportance.Highest, "Connection", "Failed to connect to " + remoteep);
                return(null);
            }

            SocketClient client = creator.CreateSocket(s, this);

            if (bbeginread)
            {
                client.DoAsyncRead();
            }
            return(client);
        }
示例#17
0
 // out bound interface
 public ConnectMgr(SocketCreator screator)
 {
     Init();
     m_socketcreator = screator;
 }
示例#18
0
 public ConnectMgr(SocketCreator screator) // out bound interface
 {
     Init();
     m_socketcreator = screator;
 }
示例#19
0
 public bool ListenIPV6(int nPort, SocketCreator screator)
 {
     return(m_AcceptorManager.EnableAcceptIPV6(nPort, screator));
 }
示例#20
0
 internal bool EnableAccept( int nPort, SocketCreator creator)
 {
     IPEndPoint ep = new IPEndPoint(System.Net.IPAddress.Any, nPort);
     return EnableAccept(ep, creator);
 }
示例#21
0
 public bool Listen(IPEndPoint ep, SocketCreator screator)
 {
     return(m_AcceptorManager.EnableAccept(ep, screator));
 }
示例#22
0
        /// <summary>
        /// Starts listening for new connection son this socket
        /// </summary>
        /// <param name="ep"></param>
        /// <param name="creator"></param>
        internal bool EnableAccept(IPEndPoint ep, SocketCreator creator)
        {
            /// start listening on thisport
            /// Start the accept process
            ///

            /// First make sure no one we have is already listening on this port
            ///

            System.Net.EndPoint epBind = (EndPoint)ep;

            System.Net.Sockets.Socket sman = new System.Net.Sockets.Socket(ep.AddressFamily /*AddressFamily.InterNetwork*/, SocketType.Stream, ProtocolType.IP);
            try
            {
                sman.Bind(epBind);
            }
            catch (SocketException e) /// winso
            {
                string strError = string.Format("Exception calling Bind {0}", e);
                LogError(MessageImportance.Highest, "EXCEPTION", strError);
                return(false);
            }
            catch (ObjectDisposedException e2) // socket was closed
            {
                string strError = string.Format("Exception calling Bind {0}", e2);
                LogError(MessageImportance.Highest, "EXCEPTION", strError);
                return(false);
            }
            catch (System.Exception ebind) // socket was closed
            {
                string strError = string.Format("Exception calling Bind {0}", ebind);
                LogError(MessageImportance.Highest, "EXCEPTION", strError);
                return(false);
            }

            m_SocketSet.Add(sman, creator);

            try
            {
                //int nCon = Convert.ToInt32(sman.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.MaxConnections));
                //this.LogMessage(MessageImportance.High, "socket", string.Format("Max listen queue is {0}", nCon));
                sman.Listen((int)SocketOptionName.MaxConnections);
            }
            catch (SocketException e3) /// winso
            {
                string strError = string.Format("Exception calling Listen {0}", e3);
                LogError(MessageImportance.Highest, "EXCEPTION", strError);
                return(false);
            }
            catch (ObjectDisposedException e4) // socket was closed
            {
                string strError = string.Format("Exception calling Listen {0}", e4);
                LogError(MessageImportance.Highest, "EXCEPTION", strError);
                return(false);
            }
            catch (System.Exception eit) // socket was closed
            {
                string strError = string.Format("Exception calling Listen {0}", eit);
                LogError(MessageImportance.Highest, "EXCEPTION", strError);
                return(false);
            }


            try
            {
                /// Multiple Accepts commented out by B.B. 3-23-2007.
                /// These cause 100% CPU on Windows 2003 Server whenever the first incoming
                /// connection is received - but works fine on Windows XP
                ///
                sman.BeginAccept(AcceptCallback, sman);

                /// 5.2 = windows 2003
                if (System.Environment.OSVersion.Version.Major >= 6)
                {
                    //sman.BeginAccept(AcceptCallback, sman);
                    //sman.BeginAccept(AcceptCallback, sman);
                }
                //sman.BeginAccept(AcceptCallback, sman);
                //sman.BeginAccept(AcceptCallback, sman);
                //sman.BeginAccept(AcceptCallback, sman);
            }
            catch (SocketException e5) /// winso
            {
                string strError = string.Format("Exception calling BeginAccept {0}", e5);
                LogError(MessageImportance.Highest, "EXCEPTION", strError);
                return(false);
            }
            catch (ObjectDisposedException e6) // socket was closed
            {
                string strError = string.Format("Exception calling BeginAccept {0}", e6);
                LogError(MessageImportance.Highest, "EXCEPTION", strError);
                return(false);
            }
            catch (System.Exception eall)
            {
                string strError = string.Format("Exception calling BeginAccept {0}", eall);
                LogError(MessageImportance.Highest, "EXCEPTION", strError);
                return(false);
            }

            return(true);
        }
示例#23
0
 public SocketClient CreateClient(string ipaddr, int nport, bool bbeginread, SocketCreator creator)
 {
     return(CreateClient(ipaddr, nport, bbeginread, creator, false));
 }
示例#24
0
        internal bool EnableAccept(int nPort, SocketCreator creator)
        {
            IPEndPoint ep = new IPEndPoint(System.Net.IPAddress.Any, nPort);

            return(EnableAccept(ep, creator));
        }
示例#25
0
 public SocketClient CreateClient(string ipaddr, int nport, bool bbeginread, SocketCreator creator)
 {
     return CreateClient(ipaddr, nport, bbeginread, creator, false);
 }