Esempio n. 1
0
 /// <summary>
 /// Checks whether hashset contains the connectinfo
 /// </summary>
 /// <param name="set"></param>
 /// <param name="info"></param>
 /// <returns></returns>
 public static bool ContainsValue(this HashSet <ConnectInfo> set, ConnectInfo info)
 {
     foreach (ConnectInfo i in set)
     {
         if (ConnectInfo.Equals(i, info))
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 2
0
            /// <summary>
            /// Creates new DownClient and connects it to remote host.
            /// </summary>
            /// <param name="torrent">The torrent to download with new client</param>
            /// <param name="connectInfo">IP and port of remote endpoint from which download</param>
            /// <returns></returns>
            public async Task ConnectTorrentAsync(Torrent torrent, ConnectInfo connectInfo)
            {
                //makes no sense to connect to myself
                if (ConnectInfo.Equals(connectInfo, MyConnectInfo))
                {
                    return;
                }


                if (!torrent.ClientsInfo.ContainsValue(connectInfo))
                {
                    torrent.ClientsInfo.Add(connectInfo);
                }


                DownClient cl = new DownClient();

                Logger.WriteLine("Trying to connect to client: " + connectInfo.IPToString() + " port: " + connectInfo.Port.ToString());

                await cl.ConnectAsync(connectInfo);

                await cl.SendIntAsync(MyConnectInfo.Port); //sends listening port of this instance

                await cl.SendIdAsync(torrent.Id);          //torrent to download

                if ((Client.ERequestPartResponse) await cl.ReadByteAsync() != Client.ERequestPartResponse.OK)
                {
                    Logger.WriteLine("Torrent not available on host: " + connectInfo.IPToString() + " port: " + connectInfo.Port.ToString());
                    Logger.WriteLine("Unable to connect to any clients");
                    if (!torrent.ClientsInfo.ContainsValue(connectInfo))
                    {
                        torrent.ClientsInfo.Add(connectInfo);
                    }
                    return;
                }
                Logger.WriteLine("Requesting clients");
                await cl.SendByteAsync((byte)EConnectType.RequestClients);

                int count = await cl.ReadIntAsync();

                Logger.WriteLine("Receiving info of additional clients. Number of clients: " + count);
                for (int i = 0; i < count; ++i)
                {
                    Logger.WriteLine("Receiving IP");
                    byte[] ip = await cl.ReadBytesAsync(4);

                    Logger.WriteLine("Receiving Port");
                    int port = await cl.ReadIntAsync();

                    ConnectInfo info = new ConnectInfo(ip, port);

                    if (torrent.ClientsInfo.ContainsValue(info))
                    {
                        Logger.WriteLine("Client already in clients list");
                        continue;
                    }
                    if (ConnectInfo.Equals(info, MyConnectInfo))
                    {
                        Logger.WriteLine("Recieved info about this client.");
                        continue;
                    }
                    Logger.WriteLine("Trying to connect to client: " + info.IPToString() + " Port:" + port.ToString());

                    try
                    {
                        DownClient c = new DownClient();
                        await c.ConnectAsync(info);

                        await c.SendIntAsync(info.Port);

                        await c.SendIdAsync(torrent.Id);

                        if ((Client.ERequestPartResponse) await c.ReadByteAsync() != Client.ERequestPartResponse.OK)
                        {
                            Logger.WriteLine("Torrent not available on host: " + info.IPToString() + " port: " + info.Port.ToString());
                            continue;
                        }

                        await c.SendByteAsync((byte)EConnectType.JustConnect);

                        Logger.WriteLine("Successfully connected to client: " + c.ConnectInfo.IPToString() + " Port:" + port.ToString());

                        torrent.AddClient(c);
                        torrent.ClientsInfo.Add(info);
                    }
                    catch (SocketException ex)
                    {//if there is problem connecting to one of additional hosts, it should be no real problem.
                        Logger.WriteLine(ex.Message);
                    }
                }

                torrent.AddClient(cl);
            }