Example #1
0
		/// <summary>Places a <see cref="T:namespace SockCel.Socket" /> in a listening state.</summary>
		/// <param name="backlog">The maximum length of the pending connections queue. </param>
		/// <exception cref="T:namespace SockCel.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information. </exception>
		/// <exception cref="T:System.ObjectDisposedException">The <see cref="T:namespace SockCel.Socket" /> has been closed. </exception>
		/// <PermissionSet>
		///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
		/// </PermissionSet>
		public void Listen(int backlog)
		{
			if (this.CleanedUp)
			{
				throw new ObjectDisposedException(base.GetType().FullName);
			}
			SocketError socketError = OSSOCK.listen(this.m_Handle, backlog);
			if (socketError != SocketError.Success)
			{
				SocketException ex = new SocketException();
				this.UpdateStatusAfterSocketError(ex);
				throw ex;
			}
			this.isListening = true;
		}
Example #2
0
		public object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
		{
			if (this.CleanedUp)
			{
				throw new ObjectDisposedException(base.GetType().FullName);
			}
			
			int num = 0;
			int num2 = 4;
			SocketError socketError = OSSOCK.getsockopt(this.m_Handle, optionLevel, optionName, out num, ref num2);
			if (socketError == SocketError.SocketError)
			{
				SocketException ex = new SocketException();
				this.UpdateStatusAfterSocketError(ex);
				throw ex;
			}
			return num;
		}
Example #3
0
		internal void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue, bool silent)
		{
			if (silent && (this.CleanedUp || this.m_Handle.IsInvalid))
			{
				return;
			}
			SocketError socketError = SocketError.Success;
			try
			{
				socketError = OSSOCK.setsockopt(this.m_Handle, optionLevel, optionName, ref optionValue, 4);
			}
			catch
			{
				if (silent && this.m_Handle.IsInvalid)
				{
					return;
				}
				throw;
			}
			if (!silent)
			{
				if (socketError == SocketError.SocketError)
				{
					SocketException ex = new SocketException();
					this.UpdateStatusAfterSocketError(ex);
					throw ex;
				}
				return;
			}
		}
Example #4
0
		private void DoBeginAccept(LazyAsyncResult asyncResult)
		{
			if (this.m_RightEndPoint == null)
				throw new InvalidOperationException("net_sockets_mustbind");

			if (!this.isListening)
				throw new InvalidOperationException("net_sockets_mustlisten");

			bool flag = false;
			SocketError socketError = SocketError.Success;
			Queue acceptQueue = this.GetAcceptQueue();
			bool flag2 = false;

			try
			{
				Monitor.Enter(this, ref flag2);
				if (acceptQueue.Count == 0)
				{
					SocketAddress socketAddress = this.m_RightEndPoint.Serialize();
					this.InternalSetBlocking(false);
					SafeCloseSocket safeCloseSocket = null;
					try
					{
						safeCloseSocket = SafeCloseSocket.Accept(this.m_Handle, socketAddress.m_Buffer, ref socketAddress.m_Size);
						socketError = (SocketError)(safeCloseSocket.IsInvalid ? Marshal.GetLastWin32Error() : 0);
					}
					catch (ObjectDisposedException)
					{
						socketError = SocketError.NotSocket;
					}
					if (socketError != SocketError.WouldBlock)
					{
						if (socketError == SocketError.Success)
						{
							asyncResult.Result = this.CreateAcceptSocket(safeCloseSocket, this.m_RightEndPoint.Create(socketAddress), false);
						}
						else
						{
							asyncResult.ErrorCode = (int)socketError;
						}
						this.InternalSetBlocking(true);
						flag = true;
					}
					else
					{
						acceptQueue.Enqueue(asyncResult);
						if (!this.SetAsyncEventSelect(AsyncEventBits.FdAccept))
						{
							acceptQueue.Dequeue();
							throw new ObjectDisposedException(base.GetType().FullName);
						}
					}
				}
				else
				{
					acceptQueue.Enqueue(asyncResult);
				}
			}
			finally
			{
				if (flag2)
					Monitor.Exit(this);
			}

			if (!flag)
				return;

			if (socketError == SocketError.Success)
			{
				asyncResult.InvokeCallback();
				return;
			}

			SocketException ex = new SocketException(socketError);
			this.UpdateStatusAfterSocketError(ex);
			throw ex;
		}
Example #5
0
		private void DoBeginAccept(Socket acceptSocket, int receiveSize, AcceptOverlappedAsyncResult asyncResult)
		{
			if (this.m_RightEndPoint == null)
			{
				throw new InvalidOperationException("net_sockets_mustbind");
			}
			if (!this.isListening)
			{
				throw new InvalidOperationException("net_sockets_mustlisten");
			}
			if (acceptSocket == null)
			{
				acceptSocket = new Socket(this.addressFamily, this.socketType, this.protocolType);
			}
			else
			{
				if (acceptSocket.m_RightEndPoint != null)
				{
					throw new InvalidOperationException("net_sockets_namedmustnotbebound");
				}
			}
			asyncResult.AcceptSocket = acceptSocket;
			int num = this.m_RightEndPoint.Serialize().Size + 16;
			byte[] buffer = new byte[receiveSize + num * 2];
			asyncResult.SetUnmanagedStructures(buffer, num);
			SocketError socketError = SocketError.Success;
			int num2;
			if (!this.AcceptEx(this.m_Handle, acceptSocket.m_Handle, Marshal.UnsafeAddrOfPinnedArrayElement(asyncResult.Buffer, 0), receiveSize, num, num, out num2, asyncResult.OverlappedHandle))
			{
				socketError = (SocketError)Marshal.GetLastWin32Error();
			}
			socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError);
			if (socketError != SocketError.Success)
			{
				SocketException ex = new SocketException(socketError);
				this.UpdateStatusAfterSocketError(ex); 
				throw ex;
			}
		}
Example #6
0
		private void DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
		{
			IPEndPoint iPEndPoint = endPointSnapshot as IPEndPoint;
			if (!Socket.OSSupportsIPv4 && iPEndPoint != null && iPEndPoint.Address.IsIPv4MappedToIPv6)
			{
				SocketException ex = new SocketException(SocketError.InvalidArgument);
				this.UpdateStatusAfterSocketError(ex);
				throw ex;
			}
			SocketError socketError = OSSOCK.bind(this.m_Handle, socketAddress.m_Buffer, socketAddress.m_Size);
			if (socketError != SocketError.Success)
			{
				SocketException ex2 = new SocketException();
				this.UpdateStatusAfterSocketError(ex2);
				throw ex2;
			}
			if (this.m_RightEndPoint == null)
			{
				this.m_RightEndPoint = endPointSnapshot;
			}
		}
Example #7
0
		internal void UpdateStatusAfterSocketError(SocketException socketException)
		{
			this.UpdateStatusAfterSocketError((SocketError)socketException.NativeErrorCode);
		}
Example #8
0
		/// <summary>Disables sends and receives on a <see cref="T:namespace SockCel.Socket" />.</summary>
		/// <param name="how">One of the <see cref="T:namespace SockCel.SocketShutdown" /> values that specifies the operation that will no longer be allowed. </param>
		/// <exception cref="T:namespace SockCel.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information. </exception>
		/// <exception cref="T:System.ObjectDisposedException">The <see cref="T:namespace SockCel.Socket" /> has been closed. </exception>
		/// <PermissionSet>
		///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
		/// </PermissionSet>
		public void Shutdown(SocketShutdown how)
		{
			if (this.CleanedUp)
			{
				throw new ObjectDisposedException(base.GetType().FullName);
			}
			SocketError socketError = OSSOCK.shutdown(this.m_Handle, (int)how);
			socketError = (SocketError)((socketError != SocketError.SocketError) ? 0 : Marshal.GetLastWin32Error());
			if (socketError != SocketError.Success && socketError != SocketError.NotSocket)
			{
				SocketException ex = new SocketException(socketError);
				this.UpdateStatusAfterSocketError(ex);
				throw ex;
			}
			this.SetToDisconnected();
			this.InternalSetBlocking(this.willBlockInternal);
		}
Example #9
0
		/// <summary>Asynchronously accepts an incoming connection attempt and creates a new <see cref="T:namespace SockCel.Socket" /> object to handle remote host communication. This method returns a buffer that contains the initial data and the number of bytes transferred.</summary>
		/// <returns>A <see cref="T:namespace SockCel.Socket" /> object to handle communication with the remote host.</returns>
		/// <param name="buffer">An array of type <see cref="T:System.Byte" /> that contains the bytes transferred. </param>
		/// <param name="bytesTransferred">The number of bytes transferred. </param>
		/// <param name="asyncResult">An <see cref="T:System.IAsyncResult" /> object that stores state information for this asynchronous operation as well as any user defined data. </param>
		/// <exception cref="T:System.NotSupportedException">Windows NT is required for this method. </exception>
		/// <exception cref="T:System.ObjectDisposedException">The <see cref="T:namespace SockCel.Socket" /> object has been closed. </exception>
		/// <exception cref="T:System.ArgumentNullException">
		///   <paramref name="asyncResult" /> is empty. </exception>
		/// <exception cref="T:System.ArgumentException">
		///   <paramref name="asyncResult" /> was not created by a call to <see cref="M:namespace SockCel.Socket.BeginAccept(System.AsyncCallback,System.Object)" />. </exception>
		/// <exception cref="T:System.InvalidOperationException">
		///   <see cref="M:namespace SockCel.Socket.EndAccept(System.IAsyncResult)" /> method was previously called. </exception>
		/// <exception cref="T:namespace SockCel.SocketException">An error occurred when attempting to access the <see cref="T:namespace SockCel.Socket" />. See the Remarks section for more information. </exception>
		/// <PermissionSet>
		///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
		///   <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		/// </PermissionSet>
		public Socket EndAccept(out byte[] buffer, out int bytesTransferred, IAsyncResult asyncResult)
		{ 
			if (this.CleanedUp)
			{
				throw new ObjectDisposedException(base.GetType().FullName);
			}
			if (asyncResult == null)
			{
				throw new ArgumentNullException("asyncResult");
			}
			AcceptOverlappedAsyncResult acceptOverlappedAsyncResult = asyncResult as AcceptOverlappedAsyncResult;
			if (acceptOverlappedAsyncResult == null || acceptOverlappedAsyncResult.AsyncObject != this)
			{
				throw new ArgumentException("net_io_invalidasyncresult");
			}
			if (acceptOverlappedAsyncResult.EndCalled)
			{
				throw new InvalidOperationException("net_io_invalidendcall");
			}
			Socket socket = (Socket)acceptOverlappedAsyncResult.InternalWaitForCompletion();
			bytesTransferred = acceptOverlappedAsyncResult.BytesTransferred;
			buffer = acceptOverlappedAsyncResult.Buffer;
			acceptOverlappedAsyncResult.EndCalled = true; 
			if (acceptOverlappedAsyncResult.ErrorCode != 0)
			{
				SocketException ex = new SocketException(acceptOverlappedAsyncResult.ErrorCode);
				this.UpdateStatusAfterSocketError(ex);
				throw ex;
			}
			return socket;
		}
Example #10
0
		/// <summary>Asynchronously accepts an incoming connection attempt and creates a new <see cref="T:namespace SockCel.Socket" /> to handle remote host communication.</summary>
		/// <returns>A <see cref="T:namespace SockCel.Socket" /> to handle communication with the remote host.</returns>
		/// <param name="asyncResult">An <see cref="T:System.IAsyncResult" /> that stores state information for this asynchronous operation as well as any user defined data. </param>
		/// <exception cref="T:System.ArgumentNullException">
		///   <paramref name="asyncResult" /> is null. </exception>
		/// <exception cref="T:System.ArgumentException">
		///   <paramref name="asyncResult" /> was not created by a call to <see cref="M:namespace SockCel.Socket.BeginAccept(System.AsyncCallback,System.Object)" />. </exception>
		/// <exception cref="T:namespace SockCel.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information. </exception>
		/// <exception cref="T:System.ObjectDisposedException">The <see cref="T:namespace SockCel.Socket" /> has been closed. </exception>
		/// <exception cref="T:System.InvalidOperationException">
		///   <see cref="M:namespace SockCel.Socket.EndAccept(System.IAsyncResult)" /> method was previously called. </exception>
		/// <exception cref="T:System.NotSupportedException">Windows NT is required for this method. </exception>
		/// <PermissionSet>
		///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
		///   <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
		/// </PermissionSet>
		public Socket EndAccept(IAsyncResult asyncResult)
		{
			if (this.CleanedUp)
			{
				throw new ObjectDisposedException(base.GetType().FullName);
			}
			if (asyncResult != null && asyncResult is AcceptOverlappedAsyncResult)
			{
				byte[] array;
				int num;
				return this.EndAccept(out array, out num, asyncResult);
			}
			if (asyncResult == null)
			{
				throw new ArgumentNullException("asyncResult");
			}
			AcceptAsyncResult acceptAsyncResult = asyncResult as AcceptAsyncResult;
			if (acceptAsyncResult == null || acceptAsyncResult.AsyncObject != this)
			{
				throw new ArgumentException("net_io_invalidasyncresult");
			}
			if (acceptAsyncResult.EndCalled)
			{
				throw new InvalidOperationException("net_io_invalidendcall");
			}
			object obj = acceptAsyncResult.InternalWaitForCompletion();
			acceptAsyncResult.EndCalled = true;
			Exception ex = obj as Exception;
			if (ex != null)
			{
				throw ex;
			}
			if (acceptAsyncResult.ErrorCode != 0)
			{
				SocketException ex2 = new SocketException(acceptAsyncResult.ErrorCode);
				this.UpdateStatusAfterSocketError(ex2);
				throw ex2;
			}
			Socket socket = (Socket)obj;
			return socket;
		}