Ejemplo n.º 1
0
        private void makeConnectionInCallerThread(int waitInMs)
        {
            if (ioSession != null)
            {
                if (ioSession.Connected)
                {
                    return;
                }
            }
            if (connecting == 1)
            {
                return;
            }
            int orgValue = Interlocked.CompareExchange(ref connecting, 1, 0);

            if (orgValue == 1)
            {
                return;
            }
            metaHolder.requestPool.Clear();
            HostPort serverConfig = serverList [rand.Next(serverList.Count)];

            connector = new AsyncSocketConnector();
            connector.FilterChain.AddLast("codec", new ProtocolCodecFilter(new CustomPackageFactory()));
            connector.FilterChain.AddLast("logger", new LoggingFilter());
            connector.MessageReceived += onMessageReceived;
            connector.SessionOpened   += (sender, eventArgs) => {
                this.ioSession = eventArgs.Session;
                Console.WriteLine("connected.");
            };
            Console.WriteLine("connecting...");
            var addresses = System.Net.Dns.GetHostAddresses(serverConfig.host);
            var endPoint  = new IPEndPoint(addresses [0], serverConfig.port);

            try {
                connector.Connect(endPoint).Await(waitInMs);
            } finally {
                connecting = 0;
            }
        }