예제 #1
0
        public Task Start(int playerIndex)
        {
            return(Task.Run(async() =>
            {
                var httpClientMain = new HttpClient();

                var reqLogin = new Login();
                reqLogin.request.account = $"LoadTester{playerIndex}@BNO";
                //reqLogin.request.password = "";
                var resLogin = await reqLogin.PostAsyncXXX(Global.ApiServerUri);

                if (resLogin.StatusCode != System.Net.HttpStatusCode.OK || string.IsNullOrEmpty(resLogin.Payload.token))
                {
                    Console.WriteLine(playerIndex + " : Login Error");
                    return;
                }

                Console.WriteLine(playerIndex + " : Login");

                if (resLogin.Payload.initialLevel == 0)
                {
                    var reqSetFirstOnetime = new SetFirstOnetime();
                    reqSetFirstOnetime.request.playerName = $"LoadTester{playerIndex}";
                    var resSetFirstOnetime = await reqSetFirstOnetime.PostAsyncXXX(Global.ApiServerUri, resLogin.Payload.token);
                }

                var keep = true;
                var b =
                    Task.Run(async() =>
                {
                    var httpClient = new HttpClient();

                    while (true)
                    {
                        var reqHandShake = new HandShack();
                        var resHandShake = await reqHandShake.PostAsyncXXX(Global.ApiServerUri, resLogin.Payload.token);

                        //Console.WriteLine(reqLogin.request.password + ":PushCode=\"" + resHandShake.pushCode + "\"");
                        //Console.WriteLine(resHandShake.pushCode);

                        if (resHandShake.StatusCode != System.Net.HttpStatusCode.OK ||
                            resHandShake.Payload.pushCode == new evoapi.ProtocolModels.HandShake.Close.Response().pushCode ||
                            resHandShake.Payload.pushCode == new evoapi.ProtocolModels.HandShake.Unauthorized.Response().pushCode)
                        {
                            Console.WriteLine(playerIndex + ":" + resHandShake.Payload.pushCode);
                            keep = false;
                            break;
                        }
                    }
                });


                var reqMasterDataGet = new MasterDataGet();
                var resMasterDataGet = await reqMasterDataGet.PostAsyncXXX(Global.ApiServerUri, resLogin.Payload.token);

                var reqGetFriends = new GetFriends();
                var resGetFriends = await reqGetFriends.PostAsyncXXX(Global.ApiServerUri, resLogin.Payload.token);

                //var reqEntryPlayer = new EntryPlayer();
                //reqEntryPlayer.request.matchType = evolib.Battle.MatchType.Casual;
                //var resEntryPlayer = await reqEntryPlayer.PostAsync(httpClientMain, resLogin.token);


                while (keep)
                {
                    await Task.Delay(1000);
                }
            }));
        }
예제 #2
0
        public BEncodedDictionary GetMetaData(InfoHash hash, out bool netError)
        {
            netError = false;
            WireMessage  message;
            ExtHandShack exths;
            long         metadataSize;
            int          piecesNum;
            byte         ut_metadata;

            try
            {
                //连接
                if (!client.ConnectAsync(EndPoint.Address, EndPoint.Port).Wait(5000))
                {
                    netError = true;
                    Trace.WriteLine("Connect Timeout", "Socket");
                    return(null);
                }
                stream = client.GetStream();

                //发送握手
                message = new HandShack(hash);
                SendMessage(message);

                //接受握手
                message = ReceiveMessage <HandShack>(1);
                if (!message.Legal || !(message as HandShack).SupportExtend)
                {
                    netError = true;
                    Trace.WriteLine(EndPoint, "HandShack Fail");
                    return(null);
                }

                //发送拓展握手
                message = new ExtHandShack()
                {
                    SupportUtMetadata = true
                };
                SendMessage(message);

                //接受拓展
                exths = ReceiveMessage <ExtHandShack>();
                if (!exths.Legal || !exths.CanGetMetadate || exths.MetadataSize > MaxMetadataSize || exths.MetadataSize <= 0)
                {
                    netError = true;
                    Trace.WriteLine(EndPoint, "ExtendHandShack Fail");
                    return(null);
                }
                metadataSize = exths.MetadataSize;
                ut_metadata  = exths.UtMetadata;
                piecesNum    = (int)Math.Ceiling(metadataSize / (decimal)PieceLength);

                //开始接受pieces
                var rtask = ReceivePiecesAsync(metadataSize, piecesNum);
                //开始发送piece请求
                for (int i = 0; i < piecesNum; i++)
                {
                    message = new ExtQueryPiece(ut_metadata, i);
                    SendMessage(message);
                }
                //等待pieces接收完毕
                rtask.Wait();
                var rawBytes = rtask.Result;

                if (rawBytes == null)
                {
                    return(null);
                }

                //检查hash值是否正确
                using (var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider())
                {
                    byte[] infohash = sha1.ComputeHash(rawBytes);
                    if (!infohash.SequenceEqual(hash.Hash))
                    {
                        Trace.WriteLine(EndPoint, "Hash Wrong");
                        return(null);
                    }
                }
                return(BEncodedDictionary.DecodeTorrent(rawBytes));
            }
            catch (AggregateException ex)
            {
                throw ex.InnerException;
            }
            finally
            {
                client?.Close();
            }
        }
예제 #3
0
        public async Task <(BEncodedDictionary, bool)> GetMetaDataAsync(InfoHash hash)
        {
            try
            {
                //连接
                Task connectTask = client.ConnectAsync(EndPoint.Address, EndPoint.Port), waitTask = Task.Delay(5000);
                await Task.WhenAny(waitTask, connectTask);

                if (!connectTask.IsCompleted || connectTask.Status != TaskStatus.RanToCompletion)
                {
                    return(null, true);
                }
                stream = client.GetStream();
                //发送握手
                WireMessage message = new HandShack(hash);
                await SendMessageAsync(message);

                //接受握手
                message = await ReceiveMessageAsync <HandShack>(1);

                if (!message.Legal || !((HandShack)message).SupportExtend)
                {
                    return(null, true);
                }
                //发送拓展握手
                message = new ExtHandShack()
                {
                    SupportUtMetadata = true
                };
                await SendMessageAsync(message);

                //接受拓展
                var exths = await ReceiveMessageAsync <ExtHandShack>();

                if (!exths.Legal || !exths.CanGetMetadate || exths.MetadataSize > MaxMetadataSize || exths.MetadataSize <= 0)
                {
                    return(null, true);
                }
                var metadataSize = exths.MetadataSize;
                var utMetadata   = exths.UtMetadata;
                var piecesNum    = (int)Math.Ceiling(metadataSize / (decimal)PieceLength);
                var metaTask     = ReceivePiecesAsync(metadataSize, piecesNum);
                //开始发送piece请求
                for (int i = 0; i < piecesNum; i++)
                {
                    message = new ExtQueryPiece(utMetadata, i);
                    await SendMessageAsync(message);
                }
                //等待pieces接收完毕
                var metadata = await metaTask;
                if (metadata == null)
                {
                    return(null, false);
                }
                //检查hash值是否正确
                using (var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider())
                {
                    byte[] infohash = sha1.ComputeHash(metadata);
                    if (infohash.SequenceEqual(hash.Hash))
                    {
                        return(BEncodedDictionary.DecodeTorrent(metadata), false);
                    }
                    return(null, false);
                }
            }
            catch (AggregateException ex)
            {
                throw ex.InnerException;
            }
            finally
            {
                client?.Close();
            }
        }