PageRenderTime 36ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Libraries/System.Net/Sockets/Socket.cs

#
C# | 260 lines | 161 code | 40 blank | 59 comment | 15 complexity | 1108460edc8a2f7003e342bc14bb741a MD5 | raw file
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // File: Socket.cs
  8. //
  9. // Note:
  10. //
  11. using Microsoft.SingSharp;
  12. using Microsoft.SingSharp.Runtime;
  13. using Microsoft.Singularity.Channels;
  14. using Microsoft.Singularity.Directory;
  15. using NetStack.Contracts;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. namespace System.Net.Sockets
  20. {
  21. public class Socket : IDisposable
  22. {
  23. InternalSocket internalSocket;
  24. bool disposed = false;
  25. // ================== Private Methods ==================
  26. private Socket(InternalSocket internalSocket)
  27. {
  28. this.internalSocket = internalSocket;
  29. }
  30. // ================== Public Methods ==================
  31. public Socket(AddressFamily addressFamily,
  32. SocketType socketType,
  33. ProtocolType protocolType)
  34. {
  35. // We only support TCP streams
  36. if (addressFamily != AddressFamily.InterNetwork) {
  37. throw new SocketException(SocketErrors.WSAEAFNOSUPPORT);
  38. }
  39. if (socketType == SocketType.Stream &&
  40. protocolType == ProtocolType.Tcp)
  41. {
  42. internalSocket = new TcpSocket();
  43. return;
  44. }
  45. if (socketType == SocketType.Dgram &&
  46. protocolType == ProtocolType.Udp)
  47. {
  48. internalSocket = new UdpSocket();
  49. return;
  50. }
  51. throw new SocketException(SocketErrors.WSAEPROTONOSUPPORT);
  52. }
  53. public void Dispose()
  54. {
  55. if (!disposed) {
  56. internalSocket.Dispose();
  57. disposed = true;
  58. }
  59. }
  60. public Socket Accept()
  61. {
  62. InternalSocket s = internalSocket.Accept();
  63. if (s != null)
  64. return new Socket(s);
  65. return null;
  66. }
  67. public void Bind(EndPoint localEP)
  68. {
  69. internalSocket.Bind(localEP);
  70. }
  71. public void Close()
  72. {
  73. internalSocket.Close();
  74. }
  75. public void Connect(EndPoint remoteEP)
  76. {
  77. internalSocket.Connect(remoteEP);
  78. }
  79. //
  80. //public object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
  81. //{
  82. // // TODO
  83. // throw new NotSupportedException();
  84. //}
  85. //
  86. //public void GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)
  87. //{
  88. // // TODO
  89. // throw new NotSupportedException();
  90. //}
  91. //
  92. //public byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength)
  93. //{
  94. // // TODO
  95. // throw new NotSupportedException();
  96. //}
  97. //
  98. public void Listen(int backlog)
  99. {
  100. internalSocket.Listen(backlog);
  101. }
  102. public bool Poll(int microSeconds, SelectMode mode)
  103. {
  104. return internalSocket.Poll(microSeconds, mode);
  105. }
  106. public int Receive(byte[] buffer, int size, SocketFlags socketFlags)
  107. {
  108. return Receive(buffer, 0, size, socketFlags);
  109. }
  110. public int Receive(byte[] buffer, SocketFlags socketFlags)
  111. {
  112. return Receive(buffer, 0, buffer!=null ? buffer.Length : 0, socketFlags);
  113. }
  114. public int Receive(byte[] buffer)
  115. {
  116. return Receive(buffer, 0, buffer!=null ? buffer.Length : 0, SocketFlags.None);
  117. }
  118. public int Receive(byte[] buffer, int offset, int size, SocketFlags socketFlags)
  119. {
  120. if (buffer == null) {
  121. throw new ArgumentNullException("buffer");
  122. }
  123. return internalSocket.Receive(buffer, offset, size, socketFlags);
  124. }
  125. //
  126. //public static void Select(IList checkRead, IList checkWrite, IList checkError, int microSeconds)
  127. //{
  128. // // TODO
  129. // throw new NotSupportedException();
  130. //}
  131. //
  132. public int Send(byte[] buffer, int size, SocketFlags socketFlags)
  133. {
  134. return Send(buffer, 0, size, socketFlags);
  135. }
  136. public int Send(byte[] buffer, SocketFlags socketFlags)
  137. {
  138. return Send(buffer, 0, buffer!=null ? buffer.Length : 0, socketFlags);
  139. }
  140. public int Send(byte[] buffer)
  141. {
  142. return Send(buffer, 0, buffer!=null ? buffer.Length : 0, SocketFlags.None);
  143. }
  144. public int Send(byte[] buffer, int offset, int size, SocketFlags socketFlags)
  145. {
  146. if (buffer == null) {
  147. throw new ArgumentException("buffer");
  148. }
  149. return internalSocket.Send(buffer, offset, size, socketFlags);
  150. }
  151. //
  152. //public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
  153. //{
  154. // // TODO
  155. // throw new NotSupportedException();
  156. //}
  157. //
  158. //public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue)
  159. //{
  160. // // TODO
  161. // throw new NotSupportedException();
  162. //}
  163. //
  164. //public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
  165. //{
  166. // SetSocketOption(optionLevel, optionName, (optionValue?1:0));
  167. //}
  168. //
  169. public void Shutdown(SocketShutdown how)
  170. {
  171. internalSocket.Shutdown(how);
  172. }
  173. // ================== Properties ==================
  174. public static bool SupportsIPv4
  175. {
  176. get { return true; }
  177. }
  178. public static bool SupportsIPv6
  179. {
  180. get { return false; }
  181. }
  182. public int Available
  183. {
  184. get { return internalSocket.Available; }
  185. }
  186. public EndPoint LocalEndPoint
  187. {
  188. get { return internalSocket.LocalEndPoint; }
  189. }
  190. public EndPoint RemoteEndPoint
  191. {
  192. get { return internalSocket.RemoteEndPoint; }
  193. }
  194. public bool Blocking
  195. {
  196. get { return internalSocket.Blocking; }
  197. }
  198. public bool Connected
  199. {
  200. get { return internalSocket.Connected; }
  201. }
  202. public AddressFamily AddressFamily
  203. {
  204. get { return internalSocket.AddressFamily; }
  205. }
  206. public SocketType SocketType
  207. {
  208. get { return internalSocket.SocketType; }
  209. }
  210. public ProtocolType ProtocolType
  211. {
  212. get { return internalSocket.ProtocolType; }
  213. }
  214. // ================== Singularity-Specific ==================
  215. public int Send([Claims] byte[]! in ExHeap buffer, SocketFlags socketFlags)
  216. {
  217. return internalSocket.Send(buffer, socketFlags);
  218. }
  219. }
  220. }