Exemplo n.º 1
0
        protected override void OnDataReceived(string data)
        {
            base.OnDataReceived(data);

            var decompressedData = CompressionHelper.Decompress(data);
            var decodedData      = Rencode.Decode(decompressedData) as Object[];

            IRPCMessage message = DecodeMessage(decodedData);

            switch (message.MessageType)
            {
            case MessageType.Response:
            case MessageType.Error:
                int requestId = (int)decodedData[1];
                if (_messageCallbacks.TryGetValue(requestId, out Action <IRPCMessage> callback))
                {
                    callback(message);
                    _messageCallbacks.Remove(requestId);
                }
                break;

            case MessageType.Event:
                Console.WriteLine("Event");
                break;

            default:
                throw new NotSupportedException("Specified MessageType is not supported.");
            }
        }
Exemplo n.º 2
0
        public async Task <bool> LoginAsync(string username, string password)
        {
            var args = new object[] { username, password };

            IRPCMessage message = await CallMethodAsync(Method.Login, args);

            return((message as RPCResponse) != null);
        }
Exemplo n.º 3
0
        public async Task <Dictionary <object, object> > GetTorrentsStatusAsync(TorrentStatusKey keyFlags)
        {
            object[] args = GetTorrentStatusKeyFlags(keyFlags).Select(key => key.GetStatusKeyName()).Cast <object>().ToArray();

            IRPCMessage message = await CallMethodAsync(Method.GetTorrentsStatus, new object[] { new Dictionary <object, object>(), args });

            RPCResponse response = message as RPCResponse;

            if (response == null)
            {
                HandleError(message as RPCError);
            }

            return(response.ReturnValues.Cast <Dictionary <object, object> >().Single());
        }
Exemplo n.º 4
0
        public async Task <object[]> GetTorrentStatusAsync(Torrent torrent, TorrentStatusKey keyFlags)
        {
            object[] args = GetTorrentStatusKeyFlags(keyFlags).Select(key => key.GetStatusKeyName()).Cast <object>().ToArray();

            IRPCMessage message = await CallMethodAsync(Method.GetTorrentStatus, new object[] { torrent.TorrentId, args });

            RPCResponse responseMessage = message as RPCResponse;

            if (responseMessage == null)
            {
                HandleError(message as RPCError);
            }

            return(responseMessage.ReturnValues);
        }
Exemplo n.º 5
0
        public async Task <Torrent> AddTorrentMagnetAsync(string magnet)
        {
            IRPCMessage message = await CallMethodAsync(Method.AddTorrentMagnet, new object[] { magnet, new object[] { } });

            RPCResponse responseMessage = message as RPCResponse;

            if (responseMessage == null)
            {
                HandleError(message as RPCError);
            }

            string torrentId = responseMessage.ReturnValues?[0] as String;

            return(torrentId != null ? new Torrent(torrentId) : null);
        }