Skip to content

Commit 369c333

Browse files
authored
Add SocketType property to Socket (#67)
1 parent 54adf2d commit 369c333

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

source/nanoFramework.System.Net/Sockets/NetworkStream.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public NetworkStream(Socket socket, bool ownsSocket)
112112
// Set the internal socket
113113
_socket = socket;
114114

115-
_socketType = (int)_socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Type);
115+
// set the socket type
116+
_socketType = (int)socket.SocketType;
116117

117118
_ownsSocket = ownsSocket;
118119
}

source/nanoFramework.System.Net/Sockets/Socket.cs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class Socket : IDisposable
2929
private int m_recvTimeout = System.Threading.Timeout.Infinite;
3030
private int m_sendTimeout = System.Threading.Timeout.Infinite;
3131

32+
// socket type
33+
private SocketType _socketType;
34+
3235
/// <summary>
3336
/// Initializes a new instance of the Socket class using the specified address family, socket type and protocol.
3437
/// </summary>
@@ -44,6 +47,8 @@ public class Socket : IDisposable
4447
public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
4548
{
4649
m_Handle = NativeSocket.socket((int)addressFamily, (int)socketType, (int)protocolType);
50+
51+
_socketType = socketType;
4752
}
4853

4954
private Socket(int handle)
@@ -223,6 +228,23 @@ public int SendTimeout
223228
}
224229
}
225230

231+
/// <summary>
232+
/// Gets the type of the <see cref="Socket"/>.
233+
/// </summary>
234+
/// <value>
235+
/// One of the <see cref="SocketType"/> values.
236+
/// </value>
237+
/// <remarks>
238+
/// <see cref="SocketType"/> is read-only and is set when the <see cref="Socket"/> is created.
239+
/// </remarks>
240+
public SocketType SocketType
241+
{
242+
get
243+
{
244+
return _socketType;
245+
}
246+
}
247+
226248
/// <summary>
227249
/// Associates a <see cref="Socket"/> with a local endpoint.
228250
/// </summary>
@@ -842,21 +864,24 @@ public object GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName op
842864
throw new NotSupportedException();
843865
}
844866

867+
// socket options that don't require any request to the native end
868+
if(optionLevel == SocketOptionLevel.Socket)
869+
{
870+
if(optionName == SocketOptionName.Type)
871+
{
872+
return _socketType;
873+
}
874+
}
875+
876+
// reached here: have to make a request to the lower level to get it
877+
845878
byte[] val = new byte[4];
846879

847880
GetSocketOption(optionLevel, optionName, val);
848881

849-
//Use BitConverter.ToInt32
850-
//endianness?
851-
int iVal;
852-
853-
//if(SystemInfo.IsBigEndian)
854-
// iVal = (val[3] << 0 | val[2] << 8 | val[1] << 16 | val[0] << 24);
855-
//else
856-
iVal = (val[0] << 0 | val[1] << 8 | val[2] << 16 | val[3] << 24);
882+
int iVal = (val[0] << 0) | (val[1] << 8) | (val[2] << 16) | (val[3] << 24);
857883

858-
859-
return (object)iVal;
884+
return iVal;
860885
}
861886

862887
/// <summary>

0 commit comments

Comments
 (0)