bool is_closed;
bool is_listening;
- bool use_overlapped_io;
+ bool useOverlappedIO;
int linger_timeout;
- AddressFamily address_family;
- SocketType socket_type;
- ProtocolType protocol_type;
+ AddressFamily addressFamily;
+ SocketType socketType;
+ ProtocolType protocolType;
- /* the field "safe_handle" is looked up by name by the runtime */
- internal SafeSocketHandle safe_handle;
+ /* the field "m_Handle" is looked up by name by the runtime */
+ internal SafeSocketHandle m_Handle;
/*
* This EndPoint is used when creating new endpoints. Because
/* When true, the socket was connected at the time of the last IO operation */
internal bool is_connected;
- internal bool is_disposed;
+ int m_IntCleanedUp;
internal bool connect_in_progress;
+ private static volatile bool s_LoggingEnabled = Logging.On;
+
#region Constructors
static Socket ()
}
}
- //
- // This constructor is used by servers that want to listen for instance on both
- // ipv4 and ipv6. Mono has historically done that if you use InterNetworkV6 (at
- // least on Unix), because that is the default behavior unless the IPV6_V6ONLY
- // option is explicitly set by using setsockopt (sock, IPPROTO_IPV6, IPV6_ONLY)
- //
- public Socket (SocketType socketType, ProtocolType protocolType)
- : this (AddressFamily.InterNetworkV6, socketType, protocolType)
- {
- DualMode = true;
- }
-
public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
{
- this.address_family = addressFamily;
- this.socket_type = socketType;
- this.protocol_type = protocolType;
+ this.addressFamily = addressFamily;
+ this.socketType = socketType;
+ this.protocolType = protocolType;
int error;
- this.safe_handle = new SafeSocketHandle (Socket_internal (addressFamily, socketType, protocolType, out error), true);
+ this.m_Handle = new SafeSocketHandle (Socket_internal (addressFamily, socketType, protocolType, out error), true);
if (error != 0)
throw new SocketException (error);
this.is_listening = (socketInformation.Options & SocketInformationOptions.Listening) != 0;
this.is_connected = (socketInformation.Options & SocketInformationOptions.Connected) != 0;
this.is_blocking = (socketInformation.Options & SocketInformationOptions.NonBlocking) == 0;
- this.use_overlapped_io = (socketInformation.Options & SocketInformationOptions.UseOnlyOverlappedIO) != 0;
+ this.useOverlappedIO = (socketInformation.Options & SocketInformationOptions.UseOnlyOverlappedIO) != 0;
var result = Mono.DataConverter.Unpack ("iiiil", socketInformation.ProtocolInformation, 0);
- this.address_family = (AddressFamily) (int) result [0];
- this.socket_type = (SocketType) (int) result [1];
- this.protocol_type = (ProtocolType) (int) result [2];
+ this.addressFamily = (AddressFamily) (int) result [0];
+ this.socketType = (SocketType) (int) result [1];
+ this.protocolType = (ProtocolType) (int) result [2];
this.is_bound = (ProtocolType) (int) result [3] != 0;
- this.safe_handle = new SafeSocketHandle ((IntPtr) (long) result [4], true);
+ this.m_Handle = new SafeSocketHandle ((IntPtr) (long) result [4], true);
SocketDefaults ();
}
/* private constructor used by Accept, which already has a socket handle to use */
internal Socket(AddressFamily family, SocketType type, ProtocolType proto, SafeSocketHandle safe_handle)
{
- this.address_family = family;
- this.socket_type = type;
- this.protocol_type = proto;
+ this.addressFamily = family;
+ this.socketType = type;
+ this.protocolType = proto;
- this.safe_handle = safe_handle;
+ this.m_Handle = safe_handle;
this.is_connected = true;
}
- ~Socket ()
- {
- Dispose (false);
- }
-
void SocketDefaults ()
{
try {
/* Need to test IPv6 further */
- if (address_family == AddressFamily.InterNetwork
- // || address_family == AddressFamily.InterNetworkV6
+ if (addressFamily == AddressFamily.InterNetwork
+ // || addressFamily == AddressFamily.InterNetworkV6
) {
/* This is the default, but it probably has nasty side
* effects on Linux, as the socket option is kludged by
* turning on or off PMTU discovery... */
this.DontFragment = false;
- if (protocol_type == ProtocolType.Tcp)
+ if (protocolType == ProtocolType.Tcp)
this.NoDelay = false;
- } else if (address_family == AddressFamily.InterNetworkV6) {
+ } else if (addressFamily == AddressFamily.InterNetworkV6) {
this.DualMode = true;
}
ThrowIfDisposedAndClosed ();
int ret, error;
- ret = Available_internal (safe_handle, out error);
+ ret = Available_internal (m_Handle, out error);
if (error != 0)
throw new SocketException (error);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
extern static int Available_internal (IntPtr socket, out int error);
- public bool DontFragment {
- get {
- ThrowIfDisposedAndClosed ();
-
- switch (address_family) {
- case AddressFamily.InterNetwork:
- return ((int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.DontFragment)) != 0;
- case AddressFamily.InterNetworkV6:
- return ((int) GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DontFragment)) != 0;
- default:
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
- }
- set {
- ThrowIfDisposedAndClosed ();
-
- switch (address_family) {
- case AddressFamily.InterNetwork:
- SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DontFragment, value ? 1 : 0);
- break;
- case AddressFamily.InterNetworkV6:
- SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DontFragment, value ? 1 : 0);
- break;
- default:
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
- }
- }
-
+ // FIXME: import from referencesource
public bool EnableBroadcast {
get {
ThrowIfDisposedAndClosed ();
- if (protocol_type != ProtocolType.Udp)
+ if (protocolType != ProtocolType.Udp)
throw new SocketException ((int) SocketError.ProtocolOption);
return ((int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast)) != 0;
set {
ThrowIfDisposedAndClosed ();
- if (protocol_type != ProtocolType.Udp)
+ if (protocolType != ProtocolType.Udp)
throw new SocketException ((int) SocketError.ProtocolOption);
SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, value ? 1 : 0);
}
}
- public bool ExclusiveAddressUse {
- get {
- ThrowIfDisposedAndClosed ();
-
- return ((int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse)) != 0;
- }
- set {
- ThrowIfDisposedAndClosed ();
-
- if (is_bound)
- throw new InvalidOperationException ("Bind has already been called for this socket");
-
- SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, value ? 1 : 0);
- }
- }
-
public bool IsBound {
get {
return is_bound;
}
}
- public LingerOption LingerState {
- get {
- ThrowIfDisposedAndClosed ();
-
- return (LingerOption) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger);
- }
- set {
- ThrowIfDisposedAndClosed ();
- SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, value);
- }
- }
-
+ // FIXME: import from referencesource
public bool MulticastLoopback {
get {
ThrowIfDisposedAndClosed ();
* "Setting this property on a Transmission Control Protocol (TCP)
* socket will have no effect." but the MS runtime throws the
* exception...) */
- if (protocol_type == ProtocolType.Tcp)
+ if (protocolType == ProtocolType.Tcp)
throw new SocketException ((int)SocketError.ProtocolOption);
- switch (address_family) {
+ switch (addressFamily) {
case AddressFamily.InterNetwork:
return ((int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback)) != 0;
case AddressFamily.InterNetworkV6:
* "Setting this property on a Transmission Control Protocol (TCP)
* socket will have no effect." but the MS runtime throws the
* exception...) */
- if (protocol_type == ProtocolType.Tcp)
+ if (protocolType == ProtocolType.Tcp)
throw new SocketException ((int)SocketError.ProtocolOption);
- switch (address_family) {
+ switch (addressFamily) {
case AddressFamily.InterNetwork:
SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, value ? 1 : 0);
break;
}
}
- public bool DualMode {
- get {
- if (AddressFamily != AddressFamily.InterNetworkV6)
- throw new NotSupportedException("This protocol version is not supported");
-
- return ((int)GetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only) == 0);
- }
- set {
- if (AddressFamily != AddressFamily.InterNetworkV6)
- throw new NotSupportedException("This protocol version is not supported");
-
- SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, value ? 0 : 1);
- }
- }
-
- private bool IsDualMode {
- get {
- return AddressFamily == AddressFamily.InterNetworkV6 && DualMode;
- }
- }
-
- [MonoTODO ("This doesn't do anything on Mono yet")]
- public bool UseOnlyOverlappedIO {
- get { return use_overlapped_io; }
- set { use_overlapped_io = value; }
- }
-
- public IntPtr Handle {
- get { return safe_handle.DangerousGetHandle (); }
- }
-
// Wish: support non-IP endpoints.
public EndPoint LocalEndPoint {
get {
return null;
int error;
- SocketAddress sa = LocalEndPoint_internal (safe_handle, (int) address_family, out error);
+ SocketAddress sa = LocalEndPoint_internal (m_Handle, (int) addressFamily, out error);
if (error != 0)
throw new SocketException (error);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
extern static SocketAddress LocalEndPoint_internal (IntPtr socket, int family, out int error);
- public SocketType SocketType {
- get { return socket_type; }
- }
-
- public int SendTimeout {
- get {
- ThrowIfDisposedAndClosed ();
-
- return (int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
- }
- set {
- ThrowIfDisposedAndClosed ();
-
- if (value < -1)
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than -1");
-
- /* According to the MSDN docs we should adjust values between 1 and
- * 499 to 500, but the MS runtime doesn't do this. */
- if (value == -1)
- value = 0;
-
- SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, value);
- }
- }
-
- public int ReceiveTimeout {
- get {
- ThrowIfDisposedAndClosed ();
-
- return (int) GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
- }
- set {
- ThrowIfDisposedAndClosed ();
-
- if (value < -1)
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than -1");
-
- if (value == -1)
- value = 0;
-
- SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, value);
- }
- }
-
- public AddressFamily AddressFamily {
- get { return address_family; }
- }
-
public bool Blocking {
get { return is_blocking; }
set {
ThrowIfDisposedAndClosed ();
int error;
- Blocking_internal (safe_handle, value, out error);
+ Blocking_internal (m_Handle, value, out error);
if (error != 0)
throw new SocketException (error);
internal set { is_connected = value; }
}
- public ProtocolType ProtocolType {
- get { return protocol_type; }
- }
-
+ // FIXME: import from referencesource
public bool NoDelay {
get {
ThrowIfDisposedAndClosed ();
}
}
- public int ReceiveBufferSize {
- get {
- ThrowIfDisposedAndClosed ();
-
- return (int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);
- }
- set {
- ThrowIfDisposedAndClosed ();
-
- if (value < 0)
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
-
- SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, value);
- }
- }
-
- public int SendBufferSize {
- get {
- ThrowIfDisposedAndClosed ();
-
- return (int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
- }
- set {
- ThrowIfDisposedAndClosed ();
-
- if (value < 0)
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
-
- SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendBuffer, value);
- }
- }
-
- public short Ttl {
- get {
- ThrowIfDisposedAndClosed ();
-
- switch (address_family) {
- case AddressFamily.InterNetwork:
- return (short) (int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive);
- case AddressFamily.InterNetworkV6:
- return (short) (int) GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit);
- default:
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
- }
- set {
- ThrowIfDisposedAndClosed ();
-
- if (value < 0 || value > 255)
- throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero or greater than 255.");
-
- switch (address_family) {
- case AddressFamily.InterNetwork:
- SetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, value);
- break;
- case AddressFamily.InterNetworkV6:
- SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit, value);
- break;
- default:
- throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
- }
- }
- }
-
public EndPoint RemoteEndPoint {
get {
ThrowIfDisposedAndClosed ();
return null;
int error;
- SocketAddress sa = RemoteEndPoint_internal (safe_handle, (int) address_family, out error);
+ SocketAddress sa = RemoteEndPoint_internal (m_Handle, (int) addressFamily, out error);
if (error != 0)
throw new SocketException (error);
throw new NotSupportedException ("'mode' parameter is not valid.");
int error;
- bool result = Poll_internal (safe_handle, mode, microSeconds, out error);
+ bool result = Poll_internal (m_Handle, mode, microSeconds, out error);
if (error != 0)
throw new SocketException (error);
ThrowIfDisposedAndClosed ();
int error = 0;
- SafeSocketHandle safe_handle = Accept_internal (this.safe_handle, out error, is_blocking);
+ SafeSocketHandle safe_handle = Accept_internal (this.m_Handle, out error, is_blocking);
if (error != 0) {
if (is_closed)
ThrowIfDisposedAndClosed ();
int error = 0;
- SafeSocketHandle safe_handle = Accept_internal (this.safe_handle, out error, is_blocking);
+ SafeSocketHandle safe_handle = Accept_internal (this.m_Handle, out error, is_blocking);
if (error != 0) {
if (is_closed)
throw new SocketException (error);
}
- acceptSocket.address_family = this.AddressFamily;
- acceptSocket.socket_type = this.SocketType;
- acceptSocket.protocol_type = this.ProtocolType;
- acceptSocket.safe_handle = safe_handle;
+ acceptSocket.addressFamily = this.AddressFamily;
+ acceptSocket.socketType = this.SocketType;
+ acceptSocket.protocolType = this.ProtocolType;
+ acceptSocket.m_Handle = safe_handle;
acceptSocket.is_connected = true;
acceptSocket.seed_endpoint = this.seed_endpoint;
acceptSocket.Blocking = this.Blocking;
sockares.Complete (acc_socket);
});
- public IAsyncResult BeginAccept (int receiveSize, AsyncCallback callback, object state)
- {
- ThrowIfDisposedAndClosed ();
-
- if (receiveSize < 0)
- throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero");
-
- SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.AcceptReceive) {
- Buffer = new byte [receiveSize],
- Offset = 0,
- Size = receiveSize,
- SockFlags = SocketFlags.None,
- };
-
- QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginAcceptReceiveCallback, sockares));
-
- return sockares;
- }
-
public IAsyncResult BeginAccept (Socket acceptSocket, int receiveSize, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
if (sockares.Size > 0) {
try {
SocketError error;
- total = acc_socket.Receive_nochecks (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, out error);
+ total = acc_socket.Receive (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, out error);
if (error != 0) {
sockares.Complete (new SocketException ((int) error));
return;
return EndAccept (out buffer, out bytes, result);
}
- public Socket EndAccept (out byte[] buffer, IAsyncResult asyncResult)
- {
- int bytes;
- return EndAccept (out buffer, out bytes, asyncResult);
- }
-
public Socket EndAccept (out byte[] buffer, out int bytesTransferred, IAsyncResult asyncResult)
{
ThrowIfDisposedAndClosed ();
}
int error;
- Bind_internal (safe_handle, localEP.Serialize(), out error);
+ Bind_internal (m_Handle, localEP.Serialize(), out error);
if (error != 0)
throw new SocketException (error);
throw new SocketException ((int) SocketError.InvalidArgument);
int error;
- Listen_internal(safe_handle, backlog, out error);
+ Listen_internal(m_Handle, backlog, out error);
if (error != 0)
throw new SocketException (error);
iep = RemapIPEndPoint (iep);
- Connect_internal (safe_handle, iep.Serialize (), out error);
+ Connect_internal (m_Handle, iep.Serialize (), out error);
if (error == 0) {
is_connected = true;
is_bound = true;
IPEndPoint ep = remoteEP as IPEndPoint;
/* Dgram uses Any to 'disconnect' */
- if (ep != null && socket_type != SocketType.Dgram) {
+ if (ep != null && socketType != SocketType.Dgram) {
if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any))
throw new SocketException ((int) SocketError.AddressNotAvailable);
}
SocketAddress serial = remoteEP.Serialize ();
int error = 0;
- Connect_internal (safe_handle, serial, out error);
+ Connect_internal (m_Handle, serial, out error);
if (error == 0 || error == 10035)
seed_endpoint = remoteEP; // Keep the ep around for non-blocking sockets
throw new SocketException (error);
}
- is_connected = !(socket_type == SocketType.Dgram && ep != null && (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)));
+ is_connected = !(socketType == SocketType.Dgram && ep != null && (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)));
is_bound = true;
}
}
});
- public IAsyncResult BeginConnect (IPAddress address, int port, AsyncCallback callback, object state)
- {
- ThrowIfDisposedAndClosed ();
-
- if (address == null)
- throw new ArgumentNullException ("address");
- if (address.ToString ().Length == 0)
- throw new ArgumentException ("The length of the IP address is zero");
- if (port <= 0 || port > 65535)
- throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
- if (is_listening)
- throw new InvalidOperationException ();
-
- return BeginConnect (new IPEndPoint (address, port), callback, state);
- }
-
public IAsyncResult BeginConnect (string host, int port, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
if (host == null)
throw new ArgumentNullException ("host");
- if (address_family != AddressFamily.InterNetwork && address_family != AddressFamily.InterNetworkV6)
+ if (addressFamily != AddressFamily.InterNetwork && addressFamily != AddressFamily.InterNetworkV6)
throw new NotSupportedException ("This method is valid only for sockets in the InterNetwork and InterNetworkV6 families");
if (port <= 0 || port > 65535)
throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
if (end_point == null)
throw new ArgumentNullException ("end_point");
+ if (is_listening)
+ throw new InvalidOperationException ();
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Connect) {
EndPoint = end_point,
// Calling connect() again will reset the connection attempt and cause
// an error. Better to just close the socket and move on.
connect_in_progress = false;
- safe_handle.Dispose ();
- safe_handle = new SafeSocketHandle (Socket_internal (address_family, socket_type, protocol_type, out error), true);
+ m_Handle.Dispose ();
+ m_Handle = new SafeSocketHandle (Socket_internal (addressFamily, socketType, protocolType, out error), true);
if (error != 0)
throw new SocketException (error);
}
bool blk = is_blocking;
if (blk)
Blocking = false;
- Connect_internal (safe_handle, end_point.Serialize (), out error);
+ Connect_internal (m_Handle, end_point.Serialize (), out error);
if (blk)
Blocking = true;
ThrowIfDisposedAndClosed ();
int error = 0;
- Disconnect_internal (safe_handle, reuseSocket, out error);
+ Disconnect_internal (m_Handle, reuseSocket, out error);
if (error != 0) {
if (error == 50) {
#region Receive
- public int Receive (byte [] buffer)
- {
- return Receive (buffer, SocketFlags.None);
- }
-
- public int Receive (byte [] buffer, SocketFlags socketFlags)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
-
- SocketError error;
- int ret = Receive_nochecks (buffer, 0, buffer.Length, socketFlags, out error);
-
- if (error != SocketError.Success) {
- if (error == SocketError.WouldBlock && is_blocking) // This might happen when ReceiveTimeout is set
- throw new SocketException ((int) error, TIMEOUT_EXCEPTION_MSG);
- throw new SocketException ((int) error);
- }
-
- return ret;
- }
-
- public int Receive (byte [] buffer, int size, SocketFlags socketFlags)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, 0, size);
-
- SocketError error;
- int ret = Receive_nochecks (buffer, 0, size, socketFlags, out error);
-
- if (error != SocketError.Success) {
- if (error == SocketError.WouldBlock && is_blocking) // This might happen when ReceiveTimeout is set
- throw new SocketException ((int) error, TIMEOUT_EXCEPTION_MSG);
- throw new SocketException ((int) error);
- }
-
- return ret;
- }
-
- public int Receive (byte [] buffer, int offset, int size, SocketFlags socketFlags)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, offset, size);
-
- SocketError error;
- int ret = Receive_nochecks (buffer, offset, size, socketFlags, out error);
-
- if (error != SocketError.Success) {
- if (error == SocketError.WouldBlock && is_blocking) // This might happen when ReceiveTimeout is set
- throw new SocketException ((int) error, TIMEOUT_EXCEPTION_MSG);
- throw new SocketException ((int) error);
- }
-
- return ret;
- }
-
public int Receive (byte [] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode)
{
ThrowIfDisposedAndClosed ();
ThrowIfBufferNull (buffer);
ThrowIfBufferOutOfRange (buffer, offset, size);
- return Receive_nochecks (buffer, offset, size, socketFlags, out errorCode);
- }
-
- public int Receive (IList<ArraySegment<byte>> buffers)
- {
- SocketError error;
- int ret = Receive (buffers, SocketFlags.None, out error);
+ int nativeError;
+ int ret = Receive_internal (m_Handle, buffer, offset, size, socketFlags, out nativeError);
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
+ errorCode = (SocketError) nativeError;
+ if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress) {
+ is_connected = false;
+ is_bound = false;
+ } else {
+ is_connected = true;
+ }
return ret;
}
- [CLSCompliant (false)]
- public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
- {
- SocketError error;
- int ret = Receive (buffers, socketFlags, out error);
-
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
-
- return(ret);
- }
-
[CLSCompliant (false)]
public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
{
}
try {
- ret = Receive_internal (safe_handle, bufarray, socketFlags, out nativeError);
+ ret = Receive_internal (m_Handle, bufarray, socketFlags, out nativeError);
} finally {
for (int i = 0; i < numsegments; i++) {
if (gch[i].IsAllocated)
}
});
- public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state)
+ public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
ThrowIfBufferNull (buffer);
ThrowIfBufferOutOfRange (buffer, offset, size);
+ /* As far as I can tell from the docs and from experimentation, a pointer to the
+ * SocketError parameter is not supposed to be saved for the async parts. And as we don't
+ * set any socket errors in the setup code, we just have to set it to Success. */
+ errorCode = SocketError.Success;
+
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Receive) {
Buffer = buffer,
Offset = offset,
Size = size,
- SockFlags = socket_flags,
+ SockFlags = socketFlags,
};
QueueIOSelectorJob (readQ, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveCallback, sockares));
return sockares;
}
- public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags flags, out SocketError error, AsyncCallback callback, object state)
- {
- /* As far as I can tell from the docs and from experimentation, a pointer to the
- * SocketError parameter is not supposed to be saved for the async parts. And as we don't
- * set any socket errors in the setup code, we just have to set it to Success. */
- error = SocketError.Success;
- return BeginReceive (buffer, offset, size, flags, callback, state);
- }
-
static IOAsyncCallback BeginReceiveCallback = new IOAsyncCallback (ares => {
SocketAsyncResult sockares = (SocketAsyncResult) ares;
int total = 0;
try {
- total = Receive_internal (sockares.socket.safe_handle, sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, out sockares.error);
+ total = Receive_internal (sockares.socket.m_Handle, sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, out sockares.error);
} catch (Exception e) {
sockares.Complete (e);
return;
});
[CLSCompliant (false)]
- public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
+ public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
if (buffers == null)
throw new ArgumentNullException ("buffers");
+ /* I assume the same SocketError semantics as above */
+ errorCode = SocketError.Success;
+
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.ReceiveGeneric) {
Buffers = buffers,
SockFlags = socketFlags,
return sockares;
}
- [CLSCompliant (false)]
- public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
- {
- /* I assume the same SocketError semantics as above */
- errorCode = SocketError.Success;
- return BeginReceive (buffers, socketFlags, callback, state);
- }
-
static IOAsyncCallback BeginReceiveGenericCallback = new IOAsyncCallback (ares => {
SocketAsyncResult sockares = (SocketAsyncResult) ares;
int total = 0;
sockares.Complete (total);
});
- public int EndReceive (IAsyncResult result)
- {
- SocketError error;
- int bytesReceived = EndReceive (result, out error);
-
- if (error != SocketError.Success) {
- if (error != SocketError.WouldBlock && error != SocketError.InProgress)
- is_connected = false;
- throw new SocketException ((int)error);
- }
-
- return bytesReceived;
- }
-
public int EndReceive (IAsyncResult asyncResult, out SocketError errorCode)
{
ThrowIfDisposedAndClosed ();
if (!sockares.IsCompleted)
sockares.AsyncWaitHandle.WaitOne ();
- // If no socket error occurred, call CheckIfThrowDelayedException in case there are other
+ errorCode = sockares.ErrorCode;
+
+ if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress)
+ is_connected = false;
+
+ // If no socket error occurred, call CheckIfThrowDelayedException in case there are other
// kinds of exceptions that should be thrown.
- if ((errorCode = sockares.ErrorCode) == SocketError.Success)
+ if (errorCode == SocketError.Success)
sockares.CheckIfThrowDelayedException();
return sockares.Total;
}
- int Receive_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
- {
- int nativeError;
- int ret = Receive_internal (safe_handle, buf, offset, size, flags, out nativeError);
-
- error = (SocketError) nativeError;
- if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
- is_connected = false;
- is_bound = false;
- } else {
- is_connected = true;
- }
-
- return ret;
- }
-
static int Receive_internal (SafeSocketHandle safeHandle, WSABUF[] bufarray, SocketFlags flags, out int error)
{
try {
#region ReceiveFrom
- public int ReceiveFrom (byte [] buffer, ref EndPoint remoteEP)
+ public int ReceiveFrom (byte [] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP)
{
ThrowIfDisposedAndClosed ();
ThrowIfBufferNull (buffer);
+ ThrowIfBufferOutOfRange (buffer, offset, size);
- return ReceiveFrom (buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP);
- }
+ if (remoteEP == null)
+ throw new ArgumentNullException ("remoteEP");
- public int ReceiveFrom (byte [] buffer, SocketFlags socketFlags, ref EndPoint remoteEP)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
+ SocketError errorCode;
+ int ret = ReceiveFrom (buffer, offset, size, socketFlags, ref remoteEP, out errorCode);
+
+ if (errorCode != SocketError.Success)
+ throw new SocketException (errorCode);
- return ReceiveFrom (buffer, 0, buffer.Length, socketFlags, ref remoteEP);
+ return ret;
}
- public int ReceiveFrom (byte [] buffer, int size, SocketFlags socketFlags, ref EndPoint remoteEP)
+ internal int ReceiveFrom (byte [] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, out SocketError errorCode)
{
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, 0, size);
+ SocketAddress sockaddr = remoteEP.Serialize();
- return ReceiveFrom (buffer, 0, size, socketFlags, ref remoteEP);
- }
+ int nativeError;
+ int cnt = ReceiveFrom_internal (m_Handle, buffer, offset, size, socketFlags, ref sockaddr, out nativeError);
- public int ReceiveFrom (byte [] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, offset, size);
+ errorCode = (SocketError) nativeError;
+ if (errorCode != SocketError.Success) {
+ if (errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress) {
+ is_connected = false;
+ } else if (errorCode == SocketError.WouldBlock && is_blocking) { // This might happen when ReceiveTimeout is set
+ errorCode = SocketError.TimedOut;
+ }
- if (remoteEP == null)
- throw new ArgumentNullException ("remoteEP");
+ return 0;
+ }
- int error;
- return ReceiveFrom_nochecks_exc (buffer, offset, size, socketFlags, ref remoteEP, true, out error);
+ is_connected = true;
+ is_bound = true;
+
+ /* If sockaddr is null then we're a connection oriented protocol and should ignore the
+ * remoteEP parameter (see MSDN documentation for Socket.ReceiveFrom(...) ) */
+ if (sockaddr != null) {
+ /* Stupidly, EndPoint.Create() is an instance method */
+ remoteEP = remoteEP.Create (sockaddr);
+ }
+
+ seed_endpoint = remoteEP;
+
+ return cnt;
}
public bool ReceiveFromAsync (SocketAsyncEventArgs e)
int total = 0;
try {
- int error;
- total = sockares.socket.ReceiveFrom_nochecks_exc (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, ref sockares.EndPoint, true, out error);
+ SocketError errorCode;
+ total = sockares.socket.ReceiveFrom (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, ref sockares.EndPoint, out errorCode);
+
+ if (errorCode != SocketError.Success) {
+ sockares.Complete (new SocketException (errorCode));
+ return;
+ }
} catch (Exception e) {
sockares.Complete (e);
return;
return sockares.Total;
}
- internal int ReceiveFrom_nochecks_exc (byte [] buf, int offset, int size, SocketFlags flags, ref EndPoint remote_end, bool throwOnError, out int error)
- {
- SocketAddress sockaddr = remote_end.Serialize();
-
- int cnt = ReceiveFrom_internal (safe_handle, buf, offset, size, flags, ref sockaddr, out error);
-
- SocketError err = (SocketError) error;
- if (err != 0) {
- if (err != SocketError.WouldBlock && err != SocketError.InProgress) {
- is_connected = false;
- } else if (err == SocketError.WouldBlock && is_blocking) { // This might happen when ReceiveTimeout is set
- if (throwOnError)
- throw new SocketException ((int) SocketError.TimedOut, TIMEOUT_EXCEPTION_MSG);
- error = (int) SocketError.TimedOut;
- return 0;
- }
-
- if (throwOnError)
- throw new SocketException (error);
-
- return 0;
- }
-
- is_connected = true;
- is_bound = true;
- /* If sockaddr is null then we're a connection oriented protocol and should ignore the
- * remote_end parameter (see MSDN documentation for Socket.ReceiveFrom(...) ) */
- if (sockaddr != null) {
- /* Stupidly, EndPoint.Create() is an instance method */
- remote_end = remote_end.Create (sockaddr);
- }
-
- seed_endpoint = remote_end;
-
- return cnt;
- }
static int ReceiveFrom_internal (SafeSocketHandle safeHandle, byte[] buffer, int offset, int count, SocketFlags flags, ref SocketAddress sockaddr, out int error)
{
#endregion
-#region SetIPProtectionLevel
-
- public void SetIPProtectionLevel (IPProtectionLevel level) {
- if (level == IPProtectionLevel.Unspecified)
- throw new ArgumentException (SR.GetString (SR.net_sockets_invalid_optionValue_all), "level");
-
- if (address_family == AddressFamily.InterNetworkV6)
- SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.IPProtectionLevel, (int)level);
- else if (address_family == AddressFamily.InterNetwork)
- SetSocketOption (SocketOptionLevel.IP, SocketOptionName.IPProtectionLevel, (int)level);
- else
- throw new NotSupportedException (SR.GetString (SR.net_invalidversion));
- }
-
-#endregion
-
#region Send
- public int Send (byte [] buffer)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
-
- SocketError error;
- int ret = Send_nochecks (buffer, 0, buffer.Length, SocketFlags.None, out error);
-
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
-
- return ret;
- }
-
- public int Send (byte [] buffer, SocketFlags socketFlags)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
-
- SocketError error;
- int ret = Send_nochecks (buffer, 0, buffer.Length, socketFlags, out error);
-
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
-
- return ret;
- }
-
- public int Send (byte [] buffer, int size, SocketFlags socketFlags)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, 0, size);
-
- SocketError error;
- int ret = Send_nochecks (buffer, 0, size, socketFlags, out error);
-
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
-
- return ret;
- }
-
- public int Send (byte [] buffer, int offset, int size, SocketFlags socketFlags)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, offset, size);
-
- SocketError error;
- int ret = Send_nochecks (buffer, offset, size, socketFlags, out error);
-
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
-
- return ret;
- }
-
public int Send (byte [] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode)
{
ThrowIfDisposedAndClosed ();
ThrowIfBufferNull (buffer);
ThrowIfBufferOutOfRange (buffer, offset, size);
- return Send_nochecks (buffer, offset, size, socketFlags, out errorCode);
- }
-
- public
- int Send (IList<ArraySegment<byte>> buffers)
- {
- SocketError error;
- int ret = Send (buffers, SocketFlags.None, out error);
-
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
-
- return ret;
- }
-
- public
- int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
- {
- SocketError error;
- int ret = Send (buffers, socketFlags, out error);
+ if (size == 0) {
+ errorCode = SocketError.Success;
+ return 0;
+ }
- if (error != SocketError.Success)
- throw new SocketException ((int) error);
+ int nativeError;
+ int sent = 0;
+ do {
+ sent += Send_internal (
+m_Handle, buffer, offset + sent, size - sent, socketFlags, out nativeError);
+ errorCode = (SocketError)nativeError;
+ if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress) {
+ is_connected = false;
+ is_bound = false;
+ break;
+ } else {
+ is_connected = true;
+ }
+ } while (sent < size);
- return ret;
+ return sent;
}
[CLSCompliant (false)]
}
try {
- ret = Send_internal (safe_handle, bufarray, socketFlags, out nativeError);
+ ret = Send_internal (m_Handle, bufarray, socketFlags, out nativeError);
} finally {
for(int i = 0; i < numsegments; i++) {
if (gch[i].IsAllocated) {
return ret;
}
- int Send_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
- {
- if (size == 0) {
- error = SocketError.Success;
- return 0;
- }
-
- int nativeError;
- int sent = 0;
- do {
- sent += Send_internal (safe_handle, buf, offset + sent, size - sent, flags, out nativeError);
- error = (SocketError)nativeError;
- if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
- is_connected = false;
- is_bound = false;
- break;
- } else {
- is_connected = true;
- }
- } while (sent < size);
-
- return sent;
- }
-
public bool SendAsync (SocketAsyncEventArgs e)
{
// NO check is made whether e != null in MS.NET (NRE is thrown in such case)
public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
{
+ ThrowIfDisposedAndClosed ();
+ ThrowIfBufferNull (buffer);
+ ThrowIfBufferOutOfRange (buffer, offset, size);
+
if (!is_connected) {
errorCode = SocketError.NotConnected;
- throw new SocketException ((int) errorCode);
+ return null;
}
errorCode = SocketError.Success;
- return BeginSend (buffer, offset, size, socketFlags, callback, state);
- }
-
- public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
- ThrowIfBufferOutOfRange (buffer, offset, size);
-
- if (!is_connected)
- throw new SocketException ((int)SocketError.NotConnected);
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Send) {
Buffer = buffer,
Offset = offset,
Size = size,
- SockFlags = socket_flags,
+ SockFlags = socketFlags,
};
QueueIOSelectorJob (writeQ, sockares.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendCallback ((SocketAsyncResult) s, 0), sockares));
int total = 0;
try {
- total = Socket.Send_internal (sockares.socket.safe_handle, sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, out sockares.error);
+ total = Socket.Send_internal (sockares.socket.m_Handle, sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, out sockares.error);
} catch (Exception e) {
sockares.Complete (e);
return;
sockares.Offset += total;
sockares.Size -= total;
- if (sockares.socket.is_disposed) {
+ if (sockares.socket.CleanedUp) {
sockares.Complete (total);
return;
}
sockares.Complete (total);
}
- public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
+ [CLSCompliant (false)]
+ public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
if (buffers == null)
throw new ArgumentNullException ("buffers");
- if (!is_connected)
- throw new SocketException ((int)SocketError.NotConnected);
+
+ if (!is_connected) {
+ errorCode = SocketError.NotConnected;
+ return null;
+ }
+
+ errorCode = SocketError.Success;
SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.SendGeneric) {
Buffers = buffers,
return sockares;
}
- [CLSCompliant (false)]
- public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
- {
- if (!is_connected) {
- errorCode = SocketError.NotConnected;
- throw new SocketException ((int)errorCode);
- }
-
- errorCode = SocketError.Success;
- return BeginSend (buffers, socketFlags, callback, state);
- }
-
static IOAsyncCallback BeginSendGenericCallback = new IOAsyncCallback (ares => {
SocketAsyncResult sockares = (SocketAsyncResult) ares;
int total = 0;
sockares.Complete (total);
});
- public int EndSend (IAsyncResult result)
- {
- SocketError error;
- int bytesSent = EndSend (result, out error);
-
- if (error != SocketError.Success) {
- if (error != SocketError.WouldBlock && error != SocketError.InProgress)
- is_connected = false;
- throw new SocketException ((int)error);
- }
-
- return bytesSent;
- }
-
public int EndSend (IAsyncResult asyncResult, out SocketError errorCode)
{
ThrowIfDisposedAndClosed ();
if (!sockares.IsCompleted)
sockares.AsyncWaitHandle.WaitOne ();
+ errorCode = sockares.ErrorCode;
+
+ if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress)
+ is_connected = false;
+
/* If no socket error occurred, call CheckIfThrowDelayedException in
* case there are other kinds of exceptions that should be thrown.*/
- if ((errorCode = sockares.ErrorCode) == SocketError.Success)
+ if (errorCode == SocketError.Success)
sockares.CheckIfThrowDelayedException ();
return sockares.Total;
#region SendTo
- public int SendTo (byte [] buffer, EndPoint remoteEP)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
-
- return SendTo (buffer, 0, buffer.Length, SocketFlags.None, remoteEP);
- }
-
- public int SendTo (byte [] buffer, SocketFlags socketFlags, EndPoint remoteEP)
- {
- ThrowIfDisposedAndClosed ();
- ThrowIfBufferNull (buffer);
-
- return SendTo (buffer, 0, buffer.Length, socketFlags, remoteEP);
- }
-
- public int SendTo (byte [] buffer, int size, SocketFlags socketFlags, EndPoint remoteEP)
- {
- return SendTo (buffer, 0, size, socketFlags, remoteEP);
- }
-
public int SendTo (byte [] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP)
{
ThrowIfDisposedAndClosed ();
if (remoteEP == null)
throw new ArgumentNullException("remoteEP");
- return SendTo_nochecks (buffer, offset, size, socketFlags, remoteEP);
+ int error;
+ int ret = SendTo_internal (m_Handle, buffer, offset, size, socketFlags, remoteEP.Serialize (), out error);
+
+ SocketError err = (SocketError) error;
+ if (err != 0) {
+ if (err != SocketError.WouldBlock && err != SocketError.InProgress)
+ is_connected = false;
+ throw new SocketException (error);
+ }
+
+ is_connected = true;
+ is_bound = true;
+ seed_endpoint = remoteEP;
+
+ return ret;
}
public bool SendToAsync (SocketAsyncEventArgs e)
{
int total = 0;
try {
- total = sockares.socket.SendTo_nochecks (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, sockares.EndPoint);
+ total = sockares.socket.SendTo (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, sockares.EndPoint);
if (sockares.error == 0) {
sent_so_far += total;
return sockares.Total;
}
- int SendTo_nochecks (byte [] buffer, int offset, int size, SocketFlags flags, EndPoint remote_end)
- {
- int error;
- int ret = SendTo_internal (safe_handle, buffer, offset, size, flags, remote_end.Serialize (), out error);
-
- SocketError err = (SocketError) error;
- if (err != 0) {
- if (err != SocketError.WouldBlock && err != SocketError.InProgress)
- is_connected = false;
- throw new SocketException (error);
- }
-
- is_connected = true;
- is_bound = true;
- seed_endpoint = remote_end;
-
- return ret;
- }
-
static int SendTo_internal (SafeSocketHandle safeHandle, byte[] buffer, int offset, int count, SocketFlags flags, SocketAddress sa, out int error)
{
try {
#region SendFile
- public void SendFile (string fileName)
- {
- ThrowIfDisposedAndClosed ();
-
- if (!is_connected)
- throw new NotSupportedException ();
- if (!is_blocking)
- throw new InvalidOperationException ();
-
- SendFile (fileName, null, null, 0);
- }
-
public void SendFile (string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
{
ThrowIfDisposedAndClosed ();
if (!is_blocking)
throw new InvalidOperationException ();
- if (!SendFile_internal (safe_handle, fileName, preBuffer, postBuffer, flags)) {
+ if (!SendFile_internal (m_Handle, fileName, preBuffer, postBuffer, flags)) {
SocketException exc = new SocketException ();
if (exc.ErrorCode == 2 || exc.ErrorCode == 3)
throw new FileNotFoundException ();
}
}
- public IAsyncResult BeginSendFile (string fileName, AsyncCallback callback, object state)
- {
- ThrowIfDisposedAndClosed ();
-
- if (!is_connected)
- throw new NotSupportedException ();
- if (!File.Exists (fileName))
- throw new FileNotFoundException ();
-
- return BeginSendFile (fileName, null, null, 0, callback, state);
- }
-
public IAsyncResult BeginSendFile (string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback, object state)
{
ThrowIfDisposedAndClosed ();
(is_listening ? SocketInformationOptions.Listening : 0) |
(is_connected ? SocketInformationOptions.Connected : 0) |
(is_blocking ? 0 : SocketInformationOptions.NonBlocking) |
- (use_overlapped_io ? SocketInformationOptions.UseOnlyOverlappedIO : 0);
+ (useOverlappedIO ? SocketInformationOptions.UseOnlyOverlappedIO : 0);
- si.ProtocolInformation = Mono.DataConverter.Pack ("iiiil", (int)address_family, (int)socket_type, (int)protocol_type, is_bound ? 1 : 0, (long)Handle);
- safe_handle = null;
+ si.ProtocolInformation = Mono.DataConverter.Pack ("iiiil", (int)addressFamily, (int)socketType, (int)protocolType, is_bound ? 1 : 0, (long)Handle);
+ m_Handle = null;
return si;
}
throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer");
int error;
- GetSocketOption_arr_internal (safe_handle, optionLevel, optionName, ref optionValue, out error);
+ GetSocketOption_arr_internal (m_Handle, optionLevel, optionName, ref optionValue, out error);
if (error != 0)
throw new SocketException (error);
int error;
byte[] byte_val = new byte [optionLength];
- GetSocketOption_arr_internal (safe_handle, optionLevel, optionName, ref byte_val, out error);
+ GetSocketOption_arr_internal (m_Handle, optionLevel, optionName, ref byte_val, out error);
if (error != 0)
throw new SocketException (error);
int error;
object obj_val;
- GetSocketOption_obj_internal (safe_handle, optionLevel, optionName, out obj_val, out error);
+ GetSocketOption_obj_internal (m_Handle, optionLevel, optionName, out obj_val, out error);
if (error != 0)
throw new SocketException (error);
throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer");
int error;
- SetSocketOption_internal (safe_handle, optionLevel, optionName, null, optionValue, 0, out error);
+ SetSocketOption_internal (m_Handle, optionLevel, optionName, null, optionValue, 0, out error);
if (error != 0) {
if (error == (int) SocketError.InvalidArgument)
LingerOption linger = optionValue as LingerOption;
if (linger == null)
throw new ArgumentException ("A 'LingerOption' value must be specified.", "optionValue");
- SetSocketOption_internal (safe_handle, optionLevel, optionName, linger, null, 0, out error);
+ SetSocketOption_internal (m_Handle, optionLevel, optionName, linger, null, 0, out error);
} else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
MulticastOption multicast = optionValue as MulticastOption;
if (multicast == null)
throw new ArgumentException ("A 'MulticastOption' value must be specified.", "optionValue");
- SetSocketOption_internal (safe_handle, optionLevel, optionName, multicast, null, 0, out error);
+ SetSocketOption_internal (m_Handle, optionLevel, optionName, multicast, null, 0, out error);
} else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
IPv6MulticastOption multicast = optionValue as IPv6MulticastOption;
if (multicast == null)
throw new ArgumentException ("A 'IPv6MulticastOption' value must be specified.", "optionValue");
- SetSocketOption_internal (safe_handle, optionLevel, optionName, multicast, null, 0, out error);
+ SetSocketOption_internal (m_Handle, optionLevel, optionName, multicast, null, 0, out error);
} else {
throw new ArgumentException ("Invalid value specified.", "optionValue");
}
{
ThrowIfDisposedAndClosed ();
- if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.ReuseAddress && optionValue != 0 && !SupportsPortReuse (protocol_type))
+ if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.ReuseAddress && optionValue != 0 && !SupportsPortReuse (protocolType))
throw new SocketException ((int) SocketError.OperationNotSupported, "Operating system sockets do not support ReuseAddress.\nIf your socket is not intended to bind to the same address and port multiple times remove this option, otherwise you should ignore this exception inside a try catch and check that ReuseAddress is true before binding to the same address and port multiple times.");
int error;
- SetSocketOption_internal (safe_handle, optionLevel, optionName, null, null, optionValue, out error);
+ SetSocketOption_internal (m_Handle, optionLevel, optionName, null, null, optionValue, out error);
if (error != 0) {
if (error == (int) SocketError.InvalidArgument)
public int IOControl (int ioControlCode, byte [] optionInValue, byte [] optionOutValue)
{
- if (is_disposed)
+ if (CleanedUp)
throw new ObjectDisposedException (GetType ().ToString ());
int error;
- int result = IOControl_internal (safe_handle, ioControlCode, optionInValue, optionOutValue, out error);
+ int result = IOControl_internal (m_Handle, ioControlCode, optionInValue, optionOutValue, out error);
if (error != 0)
throw new SocketException (error);
return result;
}
- public int IOControl (IOControlCode ioControlCode, byte[] optionInValue, byte[] optionOutValue)
- {
- return IOControl ((int) ioControlCode, optionInValue, optionOutValue);
- }
-
static int IOControl_internal (SafeSocketHandle safeHandle, int ioctl_code, byte [] input, byte [] output, out int error)
{
bool release = false;
throw new SocketException (10057); // Not connected
int error;
- Shutdown_internal (safe_handle, how, out error);
+ Shutdown_internal (m_Handle, how, out error);
if (error != 0)
throw new SocketException (error);
protected virtual void Dispose (bool disposing)
{
- if (is_disposed)
+ if (CleanedUp)
return;
- is_disposed = true;
+ m_IntCleanedUp = 1;
bool was_connected = is_connected;
is_connected = false;
- if (safe_handle != null) {
+ if (m_Handle != null) {
is_closed = true;
IntPtr x = Handle;
if (was_connected)
Linger (x);
- safe_handle.Dispose ();
+ m_Handle.Dispose ();
}
}
- public void Dispose ()
- {
- Dispose (true);
- GC.SuppressFinalize (this);
- }
-
void Linger (IntPtr handle)
{
if (!is_connected || linger_timeout <= 0)
void ThrowIfDisposedAndClosed (Socket socket)
{
- if (socket.is_disposed && socket.is_closed)
+ if (socket.CleanedUp && socket.is_closed)
throw new ObjectDisposedException (socket.GetType ().ToString ());
}
void ThrowIfDisposedAndClosed ()
{
- if (is_disposed && is_closed)
+ if (CleanedUp && is_closed)
throw new ObjectDisposedException (GetType ().ToString ());
}
void ThrowIfUdp ()
{
- if (protocol_type == ProtocolType.Udp)
+ if (protocolType == ProtocolType.Udp)
throw new SocketException ((int)SocketError.ProtocolOption);
}