Пример #1
0
        public static void Shutdown()
        {
            lock (s_ins._locker)
            {
                s_ins._client?.Dispose();
                s_ins._client = null;
            }

            ShareModule.Shutdown();
            ProfileModule.Clear();
            HistoryModule.Handled -= _HistoryHandled;
            ShareModule.PendingList.ListChanged -= _PendingListChanged;
        }
Пример #2
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());
        }