private void UpdateDCCFileProgress(DccFileStruct dcc) { if (this.InvokeRequired) { UpdateDCCFileProgressDelegate u = new UpdateDCCFileProgressDelegate(UpdateDCCFileProgress); this.Invoke(u, new object[] { dcc }); } else { foreach (ListViewItem lvi in dccFileList.Items) { if (lvi.Tag.ToString() == dcc.Thread.ManagedThreadId.ToString()) { lvi.SubItems[2].Text = dcc.TotalBytesRead + "/" + dcc.FileSize; if (dcc.TotalBytesRead.ToString() == dcc.FileSize) { lvi.SubItems[3].Text = "Completed"; } else { lvi.SubItems[3].Text = "Downloading"; } return; } } } }
/// <summary> /// Get the DCC File Data for the Specified DCC Object /// </summary> /// <param name="dcc"></param> private void GetDCCData(object dccObject) { DccFileStruct dcc = (DccFileStruct)dccObject; dcc.TotalBytesRead = 0; //add it to the Download List AddDCCFile(dcc); dccFiles.Add(dcc); while (true) { try { int buffSize = 0; byte[] buffer = new byte[8192]; NetworkStream ns = dcc.Socket.GetStream(); buffSize = dcc.Socket.ReceiveBufferSize; int bytesRead = ns.Read(buffer, 0, buffSize); //dcc file data //System.Diagnostics.Debug.WriteLine("dcc file buffer:" + bytesRead); if (bytesRead == 0) { //we have a disconnection/error break; } //write it to the file if (dcc.FileStream != null) { dcc.FileStream.Write(buffer, 0, bytesRead); dcc.FileStream.Flush(); dcc.TotalBytesRead += bytesRead; //update the UI progress bar accordingly UpdateDCCFileProgress(dcc); if (dcc.TotalBytesRead.ToString() == dcc.FileSize) { System.Diagnostics.Debug.WriteLine("should be finished"); dcc.Finished = true; break; } } else { System.Diagnostics.Debug.WriteLine("null filestream"); break; } } catch (Exception ex) { //we have an error System.Diagnostics.Debug.WriteLine("GetDCCData Error:" + ex.Message); break; } } System.Diagnostics.Debug.WriteLine("dcc file disconnected:" + dcc.TotalBytesRead + "/" + dcc.FileSize); dcc.FileStream.Flush(); dcc.FileStream.Close(); dcc.Socket.Close(); }
/// <summary> /// Add the specific Download File/Data to the DCC File List /// </summary> /// <param name="dcc"></param> private void AddDCCFile(DccFileStruct dcc) { if (this.InvokeRequired) { AddDCCFileDelegate add = new AddDCCFileDelegate(AddDCCFile); this.Invoke(add, new object[] { dcc }); } else { ListViewItem lvi = new ListViewItem(dcc.FileName); lvi.SubItems.Add(dcc.Nick); lvi.SubItems.Add(dcc.FileSize); lvi.SubItems.Add("Status"); lvi.SubItems.Add(dcc.Style); lvi.Tag = dcc.Thread.ManagedThreadId; dccFileList.Items.Add(lvi); } }
internal void StartDCCFile(IRCConnection connection, string nick, string host, string ip, string port, string file, string fileSize) { DccFileStruct dcc = new DccFileStruct(); dcc.FileName = file; dcc.FileSize = fileSize; dcc.Socket = new TcpClient(); dcc.Nick = nick; dcc.Host = host; dcc.Style = "Download"; IPAddress ipAddr = LongToIPAddress(ip); IPEndPoint ep = new IPEndPoint(ipAddr, Convert.ToInt32(port)); dcc.IPAddress = ipAddr; try { dcc.Socket.Connect(ep); if (dcc.Socket.Connected) { string dccPath = FormMain.Instance.IceChatOptions.DCCReceiveFolder; //check if the file exists if (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName)) { //file exists // set a new filename adding [#] to the end of the fielname int extPos = dcc.FileName.LastIndexOf('.'); if (extPos == -1) { int i = 0; do { i++; } while (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName + "(" + i.ToString() + ")")); dcc.FileName += "(" + i.ToString() + ")"; } else { string fileName = dcc.FileName.Substring(0, extPos); string ext = dcc.FileName.Substring(extPos + 1); int i = 0; do { i++; } while (File.Exists(dccPath + System.IO.Path.DirectorySeparatorChar + fileName + "(" + i.ToString() + ")." + ext)); dcc.FileName = fileName + "(" + i.ToString() + ")." + ext; } } dcc.FileStream = new FileStream(dccPath + System.IO.Path.DirectorySeparatorChar + dcc.FileName, FileMode.Create); dcc.Path = dccPath; //start the thread to get the data dcc.Thread = new Thread(new ParameterizedThreadStart(GetDCCData)); dcc.Thread.Start(dcc); } } catch (SocketException se) { System.Diagnostics.Debug.WriteLine("dcc file connection error:" + se.Message); } }