Esempio n. 1
0
        /// <summary>
        /// 启动连接 (与 <see cref="Shutdown"/> 方法为非完全线程安全的关系, 不过两个方法不可能同时调用)
        /// </summary>
        public static async Task <Task> Start(int id, IPEndPoint endpoint)
        {
            var clt = await LinkClient.Connect(id, endpoint, _RequestHandler);

            void _OnReceived(object sender, LinkEventArgs <LinkPacket> args) => RouteModule.Invoke(args.Object);

            void _OnDisposed(object sender, LinkEventArgs <Exception> args)
            {
                clt.Received -= _OnReceived;
                clt.Disposed -= _OnDisposed;

                // 置空
                lock (s_ins._locker)
                    s_ins._client = null;
                var obj = args.Object;

                if (obj == null || obj is OperationCanceledException)
                {
                    return;
                }
                Entrance.ShowError("连接中断", obj);
            }

            clt.Received += _OnReceived;
            clt.Disposed += _OnDisposed;

            lock (s_ins._locker)
            {
                if (s_ins._client != null)
                {
                    clt.Dispose();
                    throw new InvalidOperationException();
                }
                s_ins._client = clt;
            }

            HistoryModule.Handled += _HistoryHandled;
            ShareModule.PendingList.ListChanged += _PendingListChanged;

            ProfileModule.SetId(id);

            PostModule.UserProfile(Links.Id);
            PostModule.UserRequest();
            PostModule.UserGroups();

            return(clt.Start());
        }