Beispiel #1
0
        /// <summary>
        /// Questo metodo conferma o rifiuta la ricezione di un file da parte di un host remoto.
        /// Seguito dalla conferma ci sarà il nome del file su cui è stata fatta la scelta
        /// </summary>
        /// <param name="ip">Ip dell'host su cui si è eseguita la scelta</param>
        /// <param name="type">La scela effettuata</param>
        public async Task SendResponse(string ip, string nameFile, PacketType type)
        {
            if (type != PacketType.YFILE && type != PacketType.NFILE)
            {
                return;
            }

            if (!_referenceData.UpdateStatusRecvFileForUser(ip, nameFile, type == PacketType.YFILE ? FileRecvStatus.YSEND : FileRecvStatus.NSEND))
            {
                throw new Exception("File don't exists in collection");
            }

            int attempts = 0;

            // In case of exception resend the packet three times
            do
            {
                TcpClient     client = null;
                NetworkStream stream = null;

                try {
                    attempts++;
                    client = new TcpClient();
                    await client.ConnectAsync(ip, SharedInfo.TCPPort).ConfigureAwait(false);

                    byte[] bytes = new byte[1 + 256];
                    bytes[0] = (byte)type;
                    UTF8Encoding encorder = new UTF8Encoding();
                    encorder.GetBytes(nameFile).CopyTo(bytes, 1);

                    stream = client.GetStream();
                    await stream.WriteAsync(bytes, 0, 257).ConfigureAwait(false);

                    break;
                }
                catch (SocketException e) {
                    Console.WriteLine($"{DateTime.Now.ToString()}\t - SocketException on SendResponse - {e.Message}");

                    // If the remote host was offline, try to resend it for three times
                    if (_referenceData.GetUserStatus(ip).Equals("offline"))
                    {
                        break;
                    }
                    else if (attempts == 3)
                    {
                        break;
                    }
                    else
                    {
                        await Task.Delay(10).ConfigureAwait(false);
                    }
                }
                catch (Exception e) {
                    Console.WriteLine($"{DateTime.Now.ToString()}\t - Exception on SendResponse - {e.Message}");
                    if (attempts == 3)
                    {
                        break;
                    }
                    else
                    {
                        await Task.Delay(10).ConfigureAwait(false);
                    }
                }
                finally {
                    client.Close();
                    stream.Close();
                }
                if (attempts == 3 && _referenceData.GetUserStatus(ip).Equals("offline"))
                {
                    break;
                }
            } while (true);
        }