예제 #1
0
        // The bulk of the clean-up code
        override public void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            if (disposing)
            {
                if (this.eventManager != null)
                {
                    this.eventManager.Dispose();
                    this.eventManager = null;
                }

                if (this.protocol != null)
                {
                    this.protocol.close();
                    this.protocol = null;
                }

                try
                {
                    if (_socket != null)
                    {
                        _socket.Close();
                        _socket = null;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                this.disposed = true;
            }
        }
예제 #2
0
        /// <summary>
        /// initialize socket client
        /// </summary>
        /// <param name="host">server name or server ip (www.xxx.com/127.0.0.1/::1/localhost etc.)</param>
        /// <param name="port">server port</param>
        /// <param name="callback">socket successfully connected callback(in network thread)</param>
        public void initClient(string host, int port, Action callback = null)
        {
            disposed = false;

            timeoutEvent.Reset();
            eventManager = new OdaoEventManager();
            NetWorkChanged(NetWorkState.CONNECTING);

            IPAddress ipAddress = null;

            ipAddress = GetIPv6(host);

            if (ipAddress != null)
            {
                _socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
            }
            else
            {
                ipAddress = GetIPv4(host);
                if (ipAddress != null)
                {
                    _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                }
            }

            if (ipAddress == null)
            {
                CloseClient();
                return;
            }

            IPEndPoint ie = new IPEndPoint(ipAddress, port);

            _socket.BeginConnect(ie, new AsyncCallback((result) =>
            {
                try
                {
                    _socket.EndConnect(result);
                    this.protocol = new OdaoProtocol(this, _socket);
                    NetWorkChanged(NetWorkState.CONNECTED);

                    if (callback != null)
                    {
                        callback();
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.ToString());
                    NetWorkChanged(NetWorkState.ERROR_CONNECT);
                }
                catch (System.Exception e)
                {
                    Console.WriteLine(e.ToString());
                    NetWorkChanged(NetWorkState.ERROR_CONNECT);
                }
                finally
                {
                    timeoutEvent.Set();
                }
            }), _socket);

            if (timeoutEvent.WaitOne(timeoutMSec, false))
            {
                if (_netWorkState != NetWorkState.CONNECTED && _netWorkState != NetWorkState.ERROR && _netWorkState != NetWorkState.ERROR_CONNECT)
                {
                    NetWorkChanged(NetWorkState.TIMEOUT);
                }
            }
        }