QuoteServer.cs
上传用户:lxycoco
上传日期:2022-07-21
资源大小:38457k
文件大小:6k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Collections.Specialized;
  8. using System.Diagnostics;
  9. namespace Wrox.ProCSharp.WinServices
  10. {
  11. public class QuoteServer : System.ComponentModel.Component
  12. {
  13. private TcpListener listener;
  14. private int port;
  15. private string filename;
  16. private StringCollection quotes;
  17. private Random random;
  18. private Thread listenerThread;
  19. private int requestsPerSec;
  20. private int bytesPerSec;
  21. private System.Diagnostics.PerformanceCounter performanceCounterRequestsPerSec;
  22. private System.Diagnostics.PerformanceCounter performanceCounterBytesSentTotal;
  23. private System.Diagnostics.PerformanceCounter performanceCounterBytesSentPerSec;
  24. private System.Timers.Timer timer;
  25. private System.Diagnostics.EventLog eventLog;
  26. private System.Diagnostics.PerformanceCounter performanceCounterRequestsTotal;
  27. public QuoteServer() : this ("quotes.txt")
  28. {
  29. }
  30. public QuoteServer(string filename) : this(filename, 7890)
  31. {
  32. }
  33. public QuoteServer(string filename, int port)
  34. {
  35. this.filename = filename;
  36. this.port = port;
  37. InitializeComponent();
  38. }
  39. protected void ReadQuotes()
  40. {
  41. quotes = new StringCollection();
  42. Stream stream = File.OpenRead(filename);
  43. StreamReader streamReader = new StreamReader(stream);
  44. string quote;
  45. while ((quote = streamReader.ReadLine()) != null)
  46. {
  47. quotes.Add(quote);
  48. }
  49. streamReader.Close();
  50. stream.Close();
  51. random = new Random();
  52. }
  53. protected string GetRandomQuoteOfTheDay()
  54. {
  55. int index = random.Next(0, quotes.Count);
  56. return quotes[index];
  57. }
  58. public void Start()
  59. {
  60. ReadQuotes();
  61. listenerThread = new Thread(
  62. new ThreadStart(this.Listener));
  63. listenerThread.Start();
  64. }
  65. protected void Listener()
  66. {
  67. try
  68. {
  69. IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
  70. listener = new TcpListener(ipAddress, port);
  71. listener.Start(); 
  72. while (true)
  73. {
  74. Socket socket = listener.AcceptSocket();
  75. string message = GetRandomQuoteOfTheDay();
  76. UnicodeEncoding encoder = new UnicodeEncoding();
  77. byte[] buffer = encoder.GetBytes(message);
  78. socket.Send(buffer, buffer.Length, 0);
  79. socket.Close();
  80. performanceCounterRequestsTotal.Increment();
  81. performanceCounterBytesSentTotal.IncrementBy(buffer.Length);
  82. requestsPerSec++;
  83. bytesPerSec += buffer.Length;
  84. }
  85. }
  86. catch (SocketException e)
  87. {
  88. string message = "Quote Server failed in Listener: " 
  89. + e.Message;
  90. eventLog.WriteEntry(message, EventLogEntryType.Error);
  91. }
  92. }
  93. public void Stop()
  94. {
  95. listener.Stop();
  96. }
  97. public void Suspend()
  98. {
  99. listenerThread.Suspend();
  100. }
  101. public void Resume()
  102. {
  103. listenerThread.Resume();
  104. }
  105. private void InitializeComponent()
  106. {
  107. this.performanceCounterRequestsPerSec = new System.Diagnostics.PerformanceCounter();
  108. this.performanceCounterBytesSentTotal = new System.Diagnostics.PerformanceCounter();
  109. this.performanceCounterBytesSentPerSec = new System.Diagnostics.PerformanceCounter();
  110. this.performanceCounterRequestsTotal = new System.Diagnostics.PerformanceCounter();
  111. this.timer = new System.Timers.Timer();
  112. this.eventLog = new System.Diagnostics.EventLog();
  113. ((System.ComponentModel.ISupportInitialize)(this.performanceCounterRequestsPerSec)).BeginInit();
  114. ((System.ComponentModel.ISupportInitialize)(this.performanceCounterBytesSentTotal)).BeginInit();
  115. ((System.ComponentModel.ISupportInitialize)(this.performanceCounterBytesSentPerSec)).BeginInit();
  116. ((System.ComponentModel.ISupportInitialize)(this.performanceCounterRequestsTotal)).BeginInit();
  117. ((System.ComponentModel.ISupportInitialize)(this.timer)).BeginInit();
  118. ((System.ComponentModel.ISupportInitialize)(this.eventLog)).BeginInit();
  119. // 
  120. // performanceCounterRequestsPerSec
  121. // 
  122. this.performanceCounterRequestsPerSec.CategoryName = "Quote Service Counts";
  123. this.performanceCounterRequestsPerSec.CounterName = "# of Requests / sec";
  124. this.performanceCounterRequestsPerSec.MachineName = "NAGELC";
  125. this.performanceCounterRequestsPerSec.ReadOnly = false;
  126. // 
  127. // performanceCounterBytesSentTotal
  128. // 
  129. this.performanceCounterBytesSentTotal.CategoryName = "Quote Service Counts";
  130. this.performanceCounterBytesSentTotal.CounterName = "# of Bytes sent";
  131. this.performanceCounterBytesSentTotal.MachineName = "NAGELC";
  132. this.performanceCounterBytesSentTotal.ReadOnly = false;
  133. // 
  134. // performanceCounterBytesSentPerSec
  135. // 
  136. this.performanceCounterBytesSentPerSec.CategoryName = "Quote Service Counts";
  137. this.performanceCounterBytesSentPerSec.CounterName = "# of Bytes sent / sec";
  138. this.performanceCounterBytesSentPerSec.MachineName = "NAGELC";
  139. this.performanceCounterBytesSentPerSec.ReadOnly = false;
  140. // 
  141. // performanceCounterRequestsTotal
  142. // 
  143. this.performanceCounterRequestsTotal.CategoryName = "Quote Service Counts";
  144. this.performanceCounterRequestsTotal.CounterName = "# of Requests";
  145. this.performanceCounterRequestsTotal.MachineName = "NAGELC";
  146. this.performanceCounterRequestsTotal.ReadOnly = false;
  147. // 
  148. // timer
  149. // 
  150. this.timer.Enabled = true;
  151. this.timer.Interval = 1000;
  152. this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
  153. // 
  154. // eventLog
  155. // 
  156. this.eventLog.Log = "Application";
  157. this.eventLog.Source = "QuoteService";
  158. ((System.ComponentModel.ISupportInitialize)(this.performanceCounterRequestsPerSec)).EndInit();
  159. ((System.ComponentModel.ISupportInitialize)(this.performanceCounterBytesSentTotal)).EndInit();
  160. ((System.ComponentModel.ISupportInitialize)(this.performanceCounterBytesSentPerSec)).EndInit();
  161. ((System.ComponentModel.ISupportInitialize)(this.performanceCounterRequestsTotal)).EndInit();
  162. ((System.ComponentModel.ISupportInitialize)(this.timer)).EndInit();
  163. ((System.ComponentModel.ISupportInitialize)(this.eventLog)).EndInit();
  164. }
  165. public void RefreshQuotes()
  166. {
  167. ReadQuotes();   
  168. }
  169. private void OnTimer(object sender, System.Timers.ElapsedEventArgs e)
  170. {
  171. performanceCounterBytesSentPerSec.RawValue = bytesPerSec;
  172. performanceCounterRequestsPerSec.RawValue = requestsPerSec;
  173. bytesPerSec = 0;
  174. requestsPerSec = 0;
  175. }
  176. }
  177. }