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."); } }
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); }
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()); }
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); }
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); }