예제 #1
0
 public void OnDisconnect()
 {
     try
     {
         sck.Disconnect(false);
         FEvents.RemoveFromList(this.RemoteIp);
         Utilities.clientx.Remove(this);
     }
     catch { }
     FEvents.ConnectButton = "Connect";
     FEvents.IP            = false;
     Utilities.CreateNotification("The connection to \"" + remoteip + "\" has failed!", "Connection Failed!", ToolTipIcon.Error);
 }
예제 #2
0
        public void Listen(IAsyncResult ar)
        {
            int    received = 0;
            Socket h        = null;

            try
            {
                h        = (Socket)ar.AsyncState;
                received = h.EndReceive(ar);
            }
            catch (Exception ex)
            {
                p.OnDisconnect();
                return;
            }

            if (received <= 0)
            {
                try
                {
                    p.OnDisconnect();
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
                return;
            }

            else if (!p.Accepted)
            {
                BeginReceive();
            }

            else if (received == 4)
            {
                if (buff[0] == 0x05 && buff[1] == 0x02 && buff[2] == 0x05 && buff[3] == 0x01)
                {
                    p.Sock.Disconnect(false);
                    FEvents.ConnectButton = "Disconnect";
                    FEvents.IP            = false;
                    FEvents.RemoveFromList(p.RemoteIp);
                    Utilities.clientx.Remove(p);
                }
            }
            else if (received == 1)
            {
                switch ((ReceivedType)buff[0])
                {
                case ReceivedType.Size:
                    FileInfo file = new FileInfo(filename);
                    byte[]   size = BitConverter.GetBytes(file.Length);
                    p.Sock.Send(size);
                    break;

                case ReceivedType.ChecksumReq:
                    while (true)
                    {
                        if (checksum != "")
                        {
                            p.Sock.Send(Encoding.ASCII.GetBytes(checksum));
                            checksum = "";
                            break;
                        }
                        Thread.Sleep(10);
                    }
                    break;

                case ReceivedType.Declined:
                    FEvents.Title = "FileTransfer";
                    Utilities.CreateNotification("File Transfer for \"" + filename + "\" was declined.", "File Transfer", ToolTipIcon.Error);
                    break;

                case ReceivedType.SendFile:
                    new Thread(new ThreadStart(delegate()
                                               { checksum = Utilities.MD5(filename); })).Start();

                    FileInfo a = new FileInfo(filename);

                    System.Timers.Timer timer = new System.Timers.Timer()
                    {
                        Enabled = true, Interval = 1000
                    };
                    timer.Elapsed += this.timer_Elapsed;
                    timer.Start();

                    ulong step = 0;
                    totalsize = new BandwidthCounter();

                    using (BinaryReader bin = new BinaryReader(File.OpenRead(filename)))
                    {
                        totalsize.AddBytes((ulong)a.Length);
                        while (step < (ulong)a.Length)
                        {
                            try
                            {
                                step += (ulong)buff.Length;
                                p.Sock.Send(bin.ReadBytes(buff.Length));

                                counter.AddBytes((uint)buff.Length);

                                float af  = (float)step / (float)a.Length;
                                int   tot = (int)Math.Round(af * 100);
                                if (tot < 100)
                                {
                                    FEvents.Progress = tot;
                                }
                                else
                                {
                                    FEvents.Progress = 100;
                                }
                            }
                            catch
                            {
                                timer.Stop();

                                FEvents.Title    = "FileTransfer";
                                FEvents.Progress = 0;

                                Utilities.CreateNotification("File Transfer for \"" + filename + "\" has failed!", "File Transfer", ToolTipIcon.Error);
                                BeginReceive();
                                return;
                            }
                        }
                    }
                    timer.Stop();

                    FEvents.Title       = "FileTransfer";
                    FEvents.Status      = "Idle.";
                    FEvents.Progress    = 0;
                    FEvents.DropEnabled = true;

                    Utilities.CreateNotification("Successfully sent the file \"" + filename + "\".", "File Transfer", ToolTipIcon.Info);
                    break;
                }
            }
            else if (received > 0)
            {
                if ((ReceivedType)buff[0] == ReceivedType.Incoming && received != 1)
                {
                    try
                    {
                        byte[] buf = new byte[received - 1];
                        Array.Copy(buff, 1, buf, 0, buf.Length);
                        filename = Encoding.ASCII.GetString(buf);
                        if (MessageBox.Show("Receive the File " + filename + "?", "Incoming Filetransfer", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                        {
                            FEvents.DropEnabled = false;
                            FEvents.Title       = "FileTransfer";
                            FEvents.Status      = "Requesting File Size...";
                            h.Send(new byte[] { 0x09 });
                            int l = h.Receive(buff);
                            if (l > 0)
                            {
                                byte[] but = new byte[l];
                                Array.Copy(buff, 0, but, 0, l);

                                ulong toreceive = BitConverter.ToUInt64(but, 0);

                                h.Send(new byte[] { 0x02 });
                                FEvents.Status = "Starting Transfer...";

                                FEvents.Progress = 0;

                                System.Timers.Timer timer = new System.Timers.Timer()
                                {
                                    Interval = 1000, Enabled = true
                                };
                                timer.Elapsed += timer_Elapsed1;
                                timer.Start();

                                ulong step  = 0;
                                ulong total = toreceive;
                                totalsize = new BandwidthCounter();
                                using (BinaryWriter f = new BinaryWriter(File.Open(Environment.CurrentDirectory + "\\Received\\" + filename, FileMode.Create, FileAccess.Write, FileShare.None)))
                                {
                                    totalsize.AddBytes(total);
                                    while (toreceive > 0)
                                    {
                                        ulong recd = (ulong)h.Receive(buff);
                                        step += recd;
                                        if (recd <= 0)
                                        {
                                            timer.Stop();
                                            FEvents.Title    = "FileTransfer";
                                            FEvents.Progress = 0;

                                            Utilities.CreateNotification("File Transfer for \"" + filename + "\" has failed!", "File Transfer", ToolTipIcon.Error);
                                            BeginReceive();
                                            return;
                                        }
                                        toreceive -= recd;
                                        counter.AddBytes((uint)recd);
                                        f.Write(buff, 0, (int)recd);
                                        float af  = (float)step / (float)total;
                                        int   tot = (int)Math.Round(af * 100);
                                        if (tot < 100)
                                        {
                                            FEvents.Progress = tot;
                                        }
                                        else
                                        {
                                            FEvents.Progress = 100;
                                        }
                                    }
                                }
                                timer.Stop();

                                h.Send(new byte[] { 0x06 });
                                FEvents.Status = "Requesting Checksum...";

                                int chkc = h.Receive(buff);

                                checksum = "";
                                if (chkc > 0)
                                {
                                    checksum = Encoding.ASCII.GetString(buff).Trim('\0').Substring(0, chkc);
                                }

                                FEvents.Status = "Checking File for Corruption...";

                                if (Utilities.MD5(Environment.CurrentDirectory + "\\Received\\" + filename) == checksum)
                                {
                                    Utilities.CreateNotification("Successfully Received \"" + filename + "\"! Click to Open Folder.", "File Transfer", ToolTipIcon.Info);
                                }
                                else
                                {
                                    Utilities.CreateNotification("File was corrupted while sent. Please try again.", "File Transfer", ToolTipIcon.Error);
                                }

                                FEvents.Progress = 0;
                                FEvents.Status   = "Idle.";
                            }
                            FEvents.Title       = "FileTransfer";
                            FEvents.DropEnabled = true;
                        }
                        else
                        {
                            h.Send(new byte[] { 0x01 });
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                else if (buff[0] == 0x06)
                {
                    h.Send(new byte[] { 0x03 });
                }
            }
            BeginReceive();
        }