PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/System/System.Net.Sockets/NetworkStream.cs

https://bitbucket.org/cosi2/dotnetanywhere-wb
C# | 403 lines | 231 code | 34 blank | 138 comment | 30 complexity | 7132e7b6277e2c4c376f848fffe49a3f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Timers;
  7. namespace System.Net.Sockets
  8. {
  9. public class NetworkStream : Stream, IDisposable
  10. {
  11. // Fields
  12. private FileAccess access;
  13. private bool disposed;
  14. private bool owns_socket;
  15. private bool readable;
  16. private Socket socket;
  17. private bool writeable;
  18. // Methods
  19. public NetworkStream(Socket socket)
  20. : this(socket, FileAccess.ReadWrite, false)
  21. {
  22. }
  23. public NetworkStream(Socket socket, bool owns_socket)
  24. : this(socket, FileAccess.ReadWrite, owns_socket)
  25. {
  26. }
  27. public NetworkStream(Socket socket, FileAccess access)
  28. : this(socket, access, false)
  29. {
  30. }
  31. public NetworkStream(Socket socket, FileAccess access, bool owns_socket)
  32. {
  33. if (socket == null)
  34. {
  35. throw new ArgumentNullException("socket is null");
  36. }
  37. if (socket.SocketType != SocketType.Stream)
  38. {
  39. throw new ArgumentException("Socket is not of type Stream", "socket");
  40. }
  41. //if (!socket.Connected)
  42. //{
  43. // throw new IOException("Not connected");
  44. //}
  45. //if (!socket.Blocking)
  46. //{
  47. // throw new IOException("Operation not allowed on a non-blocking socket.");
  48. //}
  49. this.socket = socket;
  50. this.owns_socket = owns_socket;
  51. this.access = access;
  52. this.readable = this.CanRead;
  53. this.writeable = this.CanWrite;
  54. }
  55. //public override IAsyncResult BeginRead(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
  56. //{
  57. // IAsyncResult result;
  58. // this.CheckDisposed();
  59. // if (buffer == null)
  60. // {
  61. // throw new ArgumentNullException("buffer is null");
  62. // }
  63. // int length = buffer.Length;
  64. // if ((offset < 0) || (offset > length))
  65. // {
  66. // throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  67. // }
  68. // if ((size < 0) || ((offset + size) > length))
  69. // {
  70. // throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  71. // }
  72. // try
  73. // {
  74. // result = this.socket.BeginReceive(buffer, offset, size, SocketFlags.None, callback, state);
  75. // }
  76. // catch (Exception exception)
  77. // {
  78. // throw new IOException("BeginReceive failure", exception);
  79. // }
  80. // return result;
  81. //}
  82. //public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
  83. //{
  84. // IAsyncResult result;
  85. // this.CheckDisposed();
  86. // if (buffer == null)
  87. // {
  88. // throw new ArgumentNullException("buffer is null");
  89. // }
  90. // int length = buffer.Length;
  91. // if ((offset < 0) || (offset > length))
  92. // {
  93. // throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  94. // }
  95. // if ((size < 0) || ((offset + size) > length))
  96. // {
  97. // throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  98. // }
  99. // try
  100. // {
  101. // result = this.socket.BeginSend(buffer, offset, size, SocketFlags.None, callback, state);
  102. // }
  103. // catch
  104. // {
  105. // throw new IOException("BeginWrite failure");
  106. // }
  107. // return result;
  108. //}
  109. private void CheckDisposed()
  110. {
  111. if (this.disposed)
  112. {
  113. throw new ObjectDisposedException(base.GetType().FullName);
  114. }
  115. }
  116. public void Close(int timeout)
  117. {
  118. if (timeout < -1)
  119. {
  120. throw new ArgumentOutOfRangeException("timeout", "timeout is less than -1");
  121. }
  122. //Timer timer = new Timer();
  123. //timer.Elapsed += new ElapsedEventHandler(this.OnTimeoutClose);
  124. //timer.Interval = timeout;
  125. //timer.AutoReset = false;
  126. //timer.Enabled = true;
  127. }
  128. protected override void Dispose(bool disposing)
  129. {
  130. if (!this.disposed)
  131. {
  132. this.disposed = true;
  133. if (this.owns_socket)
  134. {
  135. Socket socket = this.socket;
  136. if (socket != null)
  137. {
  138. socket.Close();
  139. }
  140. }
  141. this.socket = null;
  142. this.access = 0;
  143. if (disposing)
  144. {
  145. GC.SuppressFinalize(this);
  146. }
  147. }
  148. }
  149. //public override int EndRead(IAsyncResult ar)
  150. //{
  151. // int num;
  152. // this.CheckDisposed();
  153. // if (ar == null)
  154. // {
  155. // throw new ArgumentNullException("async result is null");
  156. // }
  157. // try
  158. // {
  159. // num = this.socket.EndReceive(ar);
  160. // }
  161. // catch (Exception exception)
  162. // {
  163. // throw new IOException("EndRead failure", exception);
  164. // }
  165. // return num;
  166. //}
  167. //public override void EndWrite(IAsyncResult ar)
  168. //{
  169. // this.CheckDisposed();
  170. // if (ar == null)
  171. // {
  172. // throw new ArgumentNullException("async result is null");
  173. // }
  174. // try
  175. // {
  176. // this.socket.EndSend(ar);
  177. // }
  178. // catch (Exception exception)
  179. // {
  180. // throw new IOException("EndWrite failure", exception);
  181. // }
  182. //}
  183. ~NetworkStream()
  184. {
  185. this.Dispose(false);
  186. }
  187. public override void Flush()
  188. {
  189. }
  190. //private void OnTimeoutClose(object source, ElapsedEventArgs e)
  191. //{
  192. // this.Close();
  193. //}
  194. public override int Read([In, Out] byte[] buffer, int offset, int size)
  195. {
  196. int num;
  197. this.CheckDisposed();
  198. if (buffer == null)
  199. {
  200. throw new ArgumentNullException("buffer is null");
  201. }
  202. if ((offset < 0) || (offset > buffer.Length))
  203. {
  204. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  205. }
  206. if ((size < 0) || ((offset + size) > buffer.Length))
  207. {
  208. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  209. }
  210. try
  211. {
  212. num = this.socket.Receive(buffer, offset, size, SocketFlags.None);
  213. }
  214. catch (Exception exception)
  215. {
  216. throw new IOException("Read failure", exception);
  217. }
  218. return num;
  219. }
  220. public override long Seek(long offset, SeekOrigin origin)
  221. {
  222. throw new NotSupportedException();
  223. }
  224. public override void SetLength(long value)
  225. {
  226. throw new NotSupportedException();
  227. }
  228. public override void Write(byte[] buffer, int offset, int size)
  229. {
  230. this.CheckDisposed();
  231. if (buffer == null)
  232. {
  233. throw new ArgumentNullException("buffer");
  234. }
  235. if ((offset < 0) || (offset > buffer.Length))
  236. {
  237. throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
  238. }
  239. if ((size < 0) || (size > (buffer.Length - offset)))
  240. {
  241. throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
  242. }
  243. try
  244. {
  245. for (int i = 0; (size - i) > 0; i += this.socket.Send(buffer, offset + i, size - i, SocketFlags.None))
  246. {
  247. }
  248. }
  249. catch (Exception exception)
  250. {
  251. throw new IOException("Write failure", exception);
  252. }
  253. }
  254. // Properties
  255. public override bool CanRead
  256. {
  257. get
  258. {
  259. return ((this.access == FileAccess.ReadWrite) || (this.access == FileAccess.Read));
  260. }
  261. }
  262. public override bool CanSeek
  263. {
  264. get
  265. {
  266. return false;
  267. }
  268. }
  269. public override bool CanTimeout
  270. {
  271. get
  272. {
  273. return true;
  274. }
  275. }
  276. public override bool CanWrite
  277. {
  278. get
  279. {
  280. return ((this.access == FileAccess.ReadWrite) || (this.access == FileAccess.Write));
  281. }
  282. }
  283. public virtual bool DataAvailable
  284. {
  285. get
  286. {
  287. this.CheckDisposed();
  288. return true;// (this.socket.Available > 0);
  289. }
  290. }
  291. public override long Length
  292. {
  293. get
  294. {
  295. throw new NotSupportedException();
  296. }
  297. }
  298. public override long Position
  299. {
  300. get
  301. {
  302. throw new NotSupportedException();
  303. }
  304. set
  305. {
  306. throw new NotSupportedException();
  307. }
  308. }
  309. protected bool Readable
  310. {
  311. get
  312. {
  313. return this.readable;
  314. }
  315. set
  316. {
  317. this.readable = value;
  318. }
  319. }
  320. //public override int ReadTimeout
  321. //{
  322. // get
  323. // {
  324. // return this.socket.ReceiveTimeout;
  325. // }
  326. // set
  327. // {
  328. // if ((value <= 0) && (value != -1))
  329. // {
  330. // throw new ArgumentOutOfRangeException("value", "The value specified is less than or equal to zero and is not Infinite.");
  331. // }
  332. // this.socket.ReceiveTimeout = value;
  333. // }
  334. //}
  335. protected Socket Socket
  336. {
  337. get
  338. {
  339. return this.socket;
  340. }
  341. }
  342. protected bool Writeable
  343. {
  344. get
  345. {
  346. return this.writeable;
  347. }
  348. set
  349. {
  350. this.writeable = value;
  351. }
  352. }
  353. //public override int WriteTimeout
  354. //{
  355. // get
  356. // {
  357. // return this.socket.SendTimeout;
  358. // }
  359. // set
  360. // {
  361. // if ((value <= 0) && (value != -1))
  362. // {
  363. // throw new ArgumentOutOfRangeException("value", "The value specified is less than or equal to zero and is not Infinite");
  364. // }
  365. // this.socket.SendTimeout = value;
  366. // }
  367. //}
  368. }
  369. }