mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-14 14:24:10 +02:00
Fixed INVALID_SOCKET, added Blocking property, Select uses milliseconds
This commit is contained in:
parent
8fb6f7304d
commit
673d9a18f3
1 changed files with 76 additions and 37 deletions
|
@ -9,10 +9,15 @@ namespace System.Net
|
||||||
const int32 WSAECONNABORTED = 10053;
|
const int32 WSAECONNABORTED = 10053;
|
||||||
const int32 WSAECONNRESET = 10054;
|
const int32 WSAECONNRESET = 10054;
|
||||||
|
|
||||||
|
#if BF_PLATFORM_WINDOWS
|
||||||
public struct HSocket : uint
|
public struct HSocket : uint
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
public struct HSocket : uint32
|
||||||
|
{
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
[CRepr]
|
[CRepr]
|
||||||
public struct TimeVal
|
public struct TimeVal
|
||||||
|
@ -29,11 +34,12 @@ namespace System.Net
|
||||||
int32 mCount;
|
int32 mCount;
|
||||||
HSocket[64] mSockets;
|
HSocket[64] mSockets;
|
||||||
|
|
||||||
public void Add(HSocket s) mut
|
public bool Add(HSocket s) mut
|
||||||
{
|
{
|
||||||
Debug.Assert(mCount < cMaxCount);
|
if (mCount >= cMaxCount)
|
||||||
if (mCount < cMaxCount)
|
return false;
|
||||||
mSockets[mCount++] = s;
|
mSockets[mCount++] = s;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsSet(HSocket s)
|
public bool IsSet(HSocket s)
|
||||||
|
@ -109,7 +115,7 @@ namespace System.Net
|
||||||
public char8** h_addr_list; /* list of addresses */
|
public char8** h_addr_list; /* list of addresses */
|
||||||
}
|
}
|
||||||
|
|
||||||
const HSocket INVALID_SOCKET = (HSocket)0xffffffff;
|
const HSocket INVALID_SOCKET = (HSocket)-1;
|
||||||
const int32 SOCKET_ERROR = -1;
|
const int32 SOCKET_ERROR = -1;
|
||||||
const int AF_INET = 2;
|
const int AF_INET = 2;
|
||||||
const int SOCK_STREAM = 1;
|
const int SOCK_STREAM = 1;
|
||||||
|
@ -122,7 +128,8 @@ namespace System.Net
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
HSocket mHandle = INVALID_SOCKET;
|
HSocket mHandle = INVALID_SOCKET;
|
||||||
bool mIsConnected = true;
|
bool mIsConnected;
|
||||||
|
bool mIsBlocking = false;
|
||||||
|
|
||||||
public bool IsOpen
|
public bool IsOpen
|
||||||
{
|
{
|
||||||
|
@ -132,6 +139,33 @@ namespace System.Net
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsConnected
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return mIsConnected;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public HSocket NativeSocket
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return mHandle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Blocking
|
||||||
|
{
|
||||||
|
get => mIsBlocking;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
mIsBlocking = true;
|
||||||
|
if (mHandle != INVALID_SOCKET)
|
||||||
|
SetBlocking(mIsBlocking);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if BF_PLATFORM_WINDOWS
|
#if BF_PLATFORM_WINDOWS
|
||||||
[Import("wsock32.lib"), CLink, CallingConvention(.Stdcall)]
|
[Import("wsock32.lib"), CLink, CallingConvention(.Stdcall)]
|
||||||
static extern int32 WSAStartup(uint16 versionRequired, WSAData* wsaData);
|
static extern int32 WSAStartup(uint16 versionRequired, WSAData* wsaData);
|
||||||
|
@ -225,18 +259,28 @@ namespace System.Net
|
||||||
((val & 0xFF00) >> 8));
|
((val & 0xFF00) >> 8));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result<void> Listen(int32 port, int32 backlog = 5)
|
void SetBlocking(bool blocking)
|
||||||
{
|
{
|
||||||
Debug.Assert(mHandle == INVALID_SOCKET);
|
int param = blocking ? 0 : 1;
|
||||||
|
|
||||||
mHandle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
||||||
int param = 1;
|
|
||||||
|
|
||||||
#if BF_PLATFORM_WINDOWS
|
#if BF_PLATFORM_WINDOWS
|
||||||
ioctlsocket(mHandle, FIONBIO, ¶m);
|
ioctlsocket(mHandle, FIONBIO, ¶m);
|
||||||
#else
|
#else
|
||||||
ioctl(mHandle, FIONBIO, ¶m);
|
ioctl(mHandle, FIONBIO, ¶m);
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void RehupSettings()
|
||||||
|
{
|
||||||
|
SetBlocking(mIsBlocking);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Result<void> Listen(int32 port, int32 backlog = 5)
|
||||||
|
{
|
||||||
|
Debug.Assert(mHandle == INVALID_SOCKET);
|
||||||
|
|
||||||
|
mHandle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
|
||||||
if (mHandle == INVALID_SOCKET)
|
if (mHandle == INVALID_SOCKET)
|
||||||
{
|
{
|
||||||
#unwarn
|
#unwarn
|
||||||
|
@ -244,6 +288,8 @@ namespace System.Net
|
||||||
return .Err;
|
return .Err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RehupSettings();
|
||||||
|
|
||||||
SockAddr_in service;
|
SockAddr_in service;
|
||||||
service.sin_family = AF_INET;
|
service.sin_family = AF_INET;
|
||||||
service.sin_addr = in_addr(127, 0, 0, 1);
|
service.sin_addr = in_addr(127, 0, 0, 1);
|
||||||
|
@ -284,13 +330,6 @@ namespace System.Net
|
||||||
if (connect(mHandle, &sockAddr, sizeof(SockAddr_in)) == SOCKET_ERROR)
|
if (connect(mHandle, &sockAddr, sizeof(SockAddr_in)) == SOCKET_ERROR)
|
||||||
return .Err;
|
return .Err;
|
||||||
|
|
||||||
int param = 1;
|
|
||||||
|
|
||||||
#if BF_PLATFORM_WINDOWS
|
|
||||||
ioctlsocket(mHandle, FIONBIO, ¶m);
|
|
||||||
#else
|
|
||||||
ioctl(mHandle, FIONBIO, ¶m);
|
|
||||||
#endif
|
|
||||||
if (mHandle == INVALID_SOCKET)
|
if (mHandle == INVALID_SOCKET)
|
||||||
{
|
{
|
||||||
#unwarn
|
#unwarn
|
||||||
|
@ -298,6 +337,9 @@ namespace System.Net
|
||||||
return .Err;
|
return .Err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mIsConnected = true;
|
||||||
|
RehupSettings();
|
||||||
|
|
||||||
return .Ok;
|
return .Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -310,12 +352,25 @@ namespace System.Net
|
||||||
{
|
{
|
||||||
#unwarn
|
#unwarn
|
||||||
int lastErr = GetLastError();
|
int lastErr = GetLastError();
|
||||||
|
return .Err;
|
||||||
}
|
}
|
||||||
return (mHandle != INVALID_SOCKET) ? .Ok : .Err;
|
|
||||||
|
RehupSettings();
|
||||||
|
mIsConnected = true;
|
||||||
|
return .Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int32 Select(FDSet* readFDS, FDSet* writeFDS, FDSet* exceptFDS, int waitTimeUS)
|
public static int32 Select(FDSet* readFDS, FDSet* writeFDS, FDSet* exceptFDS, int waitTimeMS)
|
||||||
{
|
{
|
||||||
|
TimeVal timeVal;
|
||||||
|
timeVal.mSec = (.)(waitTimeMS / 1000);
|
||||||
|
timeVal.mUSec = (.)((waitTimeMS % 1000) * 1000);
|
||||||
|
return select(0, readFDS, writeFDS, exceptFDS, &timeVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int32 Select(FDSet* readFDS, FDSet* writeFDS, FDSet* exceptFDS, float waitTimeMS)
|
||||||
|
{
|
||||||
|
int waitTimeUS = (int)(waitTimeMS * 1000);
|
||||||
TimeVal timeVal;
|
TimeVal timeVal;
|
||||||
timeVal.mSec = (.)(waitTimeUS / (1000*1000));
|
timeVal.mSec = (.)(waitTimeUS / (1000*1000));
|
||||||
timeVal.mUSec = (.)(waitTimeUS % (1000*1000));
|
timeVal.mUSec = (.)(waitTimeUS % (1000*1000));
|
||||||
|
@ -388,22 +443,6 @@ namespace System.Net
|
||||||
#endif
|
#endif
|
||||||
mHandle = INVALID_SOCKET;
|
mHandle = INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsConnected
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return mIsConnected;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public HSocket NativeSocket
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return mHandle;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue