FormNetSession.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using CommonFroms.Drawing;
  2. using CommonLang;
  3. using CommonNetwork.Net;
  4. using CommonNetwork.Sockets;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. namespace CommonFroms.Net
  14. {
  15. public partial class FormNetSession : Form
  16. {
  17. private INetSession mSession;
  18. private BytesSecondRateTracker mTrackerRecv;
  19. private BytesSecondRateTracker mTrackerSend;
  20. private StringBuilder textBuffer = new StringBuilder();
  21. public FormNetSession(INetSession session)
  22. {
  23. InitializeComponent();
  24. this.mSession = session;
  25. this.mSession.OnMessageReceived += this.onMessageReceived;
  26. this.mSession.OnMessageSent += this.onMessageSent;
  27. this.mTrackerRecv = new BytesSecondRateTracker(600, this.Font, new Pen(Color.Blue), new SolidBrush(Color.Blue));
  28. this.mTrackerRecv.Title = "Recv";
  29. this.mTrackerSend = new BytesSecondRateTracker(600, this.Font, new Pen(Color.Red), new SolidBrush(Color.Red));
  30. this.mTrackerSend.Title = "Send";
  31. }
  32. private void timer1_Tick(object sender, EventArgs e)
  33. {
  34. mTrackerRecv.Record(mSession.TotalRecvBytes);
  35. mTrackerSend.Record(mSession.TotalSentBytes);
  36. pictureBox1.Refresh();
  37. lock (textBuffer)
  38. {
  39. if (textBuffer.Length > 0)
  40. {
  41. richTextBox1.AppendText(textBuffer.ToString());
  42. textBuffer.Clear();
  43. }
  44. }
  45. }
  46. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  47. {
  48. try
  49. {
  50. float px = 0;
  51. float py = 0;
  52. float pw = pictureBox1.Width;
  53. float ph = pictureBox1.Height / 2;
  54. mTrackerRecv.DrawGrap(e.Graphics, px + 1, py + 1, pw - 2, ph - 2);
  55. py += ph;
  56. mTrackerSend.DrawGrap(e.Graphics, px + 1, py + 1, pw - 2, ph - 2);
  57. }
  58. catch (Exception err) { }
  59. }
  60. private void pictureBox1_Click(object sender, EventArgs e)
  61. {
  62. }
  63. private void onMessageReceived(INetSession session, object msg)
  64. {
  65. lock(textBuffer)
  66. {
  67. textBuffer.AppendLine(DateTime.Now + " : Recv <- " + msg);
  68. }
  69. }
  70. private void onMessageSent(INetSession session, object msg)
  71. {
  72. lock (textBuffer)
  73. {
  74. textBuffer.AppendLine(DateTime.Now + " : Send -> " + msg);
  75. }
  76. }
  77. }
  78. }