Skip to content

Commit 1114c60

Browse files
Add NativeSocketAddress and update socket handling
Introduce a native C++ helper (NativeSocketAddress) with Hx externs to extract peer/host info and accept sockets across platforms. Replace direct NativeSocket socket_accept/peer/host usage with NativeSocketAddress in sys/net/Socket, add __hostFromNativeAddress to centralize Host construction (IPv4/IPv6), and make close() null-safe. Update crossbyte/net to use host.toString() where appropriate. Adjust tests: only run SocketTest on cpp, make IPv6 tests explicitly assert false when unsupported, and add dedicated test mains for socket/datagram/net. Also rename and reword CI workflow steps for clarity (job/step names and descriptions).
1 parent 558589e commit 1114c60

13 files changed

Lines changed: 302 additions & 52 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI
1+
name: CrossByte CI
22

33
on:
44
push:
@@ -13,26 +13,26 @@ concurrency:
1313

1414
jobs:
1515
tests:
16-
name: Haxe tests
16+
name: Haxe test suite
1717
runs-on: ubuntu-latest
1818

1919
steps:
2020
- name: Checkout
2121
uses: actions/checkout@v4
2222

23-
- name: Setup Haxe
23+
- name: Prepare Haxe toolchain
2424
uses: krdlab/setup-haxe@v1
2525
with:
2626
haxe-version: "4.3.7"
2727

28-
- name: Install haxelibs
28+
- name: Install dependencies
2929
run: |
3030
haxelib install utest --quiet
3131
32-
- name: Run unit tests
32+
- name: Run Haxe unit tests
3333
run: haxe test.hxml
3434

35-
- name: Check samples
35+
- name: Validate sample builds
3636
shell: bash
3737
run: |
3838
set -euo pipefail
@@ -43,55 +43,55 @@ jobs:
4343
done < <(find samples -name check.hxml -print | sort)
4444
4545
cpp-audit:
46-
name: hxcpp compile audit
46+
name: hxcpp compile checks
4747
runs-on: ubuntu-latest
4848

4949
steps:
5050
- name: Checkout
5151
uses: actions/checkout@v4
5252

53-
- name: Setup Haxe
53+
- name: Prepare Haxe toolchain
5454
uses: krdlab/setup-haxe@v1
5555
with:
5656
haxe-version: "4.3.7"
5757

58-
- name: Install hxcpp
58+
- name: hxcpp
5959
run: haxelib install hxcpp --quiet
6060

61-
- name: Type-check full cpp surface
61+
- name: Type-check full C++ API surface
6262
run: haxe -cp src --macro "include('crossbyte')" --cpp export/audit-full --no-output
6363

64-
- name: Type-check timer burst catch-up
64+
- name: Type-check timer burst support
6565
run: haxe -cp src --macro "include('crossbyte._internal.system.timer.heap')" -D timer_burst_catchup --cpp export/audit-timer --no-output
6666

6767
native-smoke:
68-
name: Native smoke tests
68+
name: Native build & smoke checks
6969
runs-on: windows-latest
7070

7171
steps:
7272
- name: Checkout
7373
uses: actions/checkout@v4
7474

75-
- name: Setup Haxe
75+
- name: Prepare Haxe toolchain
7676
uses: krdlab/setup-haxe@v1
7777
with:
7878
haxe-version: "4.3.7"
7979

80-
- name: Install haxelibs
80+
- name: Install dependencies
8181
run: |
8282
haxelib install utest --quiet
8383
haxelib install hxcpp --quiet
8484
85-
- name: Build focused HTTP client tests
85+
- name: Build HTTP client tests
8686
run: haxe -cp src -cp tests -lib utest -D windows -cpp export\cpp-http-client-test -main crossbyte._internal.http.HttpTestMain
8787

88-
- name: Run focused HTTP client tests
88+
- name: Run HTTP client tests
8989
run: .\export\cpp-http-client-test\HttpTestMain.exe
9090

91-
- name: Build focused HTTP server tests
91+
- name: Build HTTP server tests
9292
run: haxe -cp src -cp tests -lib utest -D windows -cpp export\cpp-http-server-test -main crossbyte.http.HTTPTestMain
9393

94-
- name: Run focused HTTP server tests
94+
- name: Run HTTP server tests
9595
run: .\export\cpp-http-server-test\HTTPTestMain.exe
9696

9797
- name: Build websocket echo sample
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#include <hxcpp.h>
2+
#include "NativeSocketAddress.h"
3+
4+
#include <string.h>
5+
6+
#if defined(HX_WINDOWS) || defined(NEKO_WINDOWS)
7+
#include <winsock2.h>
8+
#include <Ws2tcpip.h>
9+
typedef int SocketLen;
10+
#else
11+
#include <arpa/inet.h>
12+
#include <netdb.h>
13+
#include <netinet/in.h>
14+
#include <sys/socket.h>
15+
#include <sys/types.h>
16+
#include <unistd.h>
17+
typedef int SOCKET;
18+
#define INVALID_SOCKET (-1)
19+
#define SOCKET_ERROR (-1)
20+
typedef socklen_t SocketLen;
21+
#endif
22+
23+
namespace {
24+
25+
static int stdSocketType = 0;
26+
27+
static int crossbyte_socket_type() {
28+
if (stdSocketType == 0) {
29+
Dynamic probe = _hx_std_socket_new(false, false);
30+
stdSocketType = probe->__GetType();
31+
_hx_std_socket_close(probe);
32+
}
33+
return stdSocketType;
34+
}
35+
36+
struct SocketWrapper : public hx::Object {
37+
HX_IS_INSTANCE_OF enum { _hx_ClassId = hx::clsIdSocket };
38+
39+
SOCKET socket;
40+
41+
int __GetType() const {
42+
return crossbyte_socket_type();
43+
}
44+
};
45+
46+
static SOCKET crossbyte_val_sock(Dynamic inValue) {
47+
if (inValue.mPtr == 0) {
48+
hx::Throw(HX_CSTRING("Invalid socket handle"));
49+
return INVALID_SOCKET;
50+
}
51+
52+
if (inValue->__GetType() == vtClass) {
53+
inValue = inValue->__Field(HX_CSTRING("__s"), hx::paccNever);
54+
if (inValue.mPtr == 0) {
55+
hx::Throw(HX_CSTRING("Invalid socket handle"));
56+
return INVALID_SOCKET;
57+
}
58+
}
59+
60+
return reinterpret_cast<SocketWrapper*>(inValue.mPtr)->socket;
61+
}
62+
63+
static Array<int> crossbyte_address_to_array(const sockaddr* address, SocketLen length) {
64+
if (address == 0) {
65+
return null();
66+
}
67+
68+
if (address->sa_family == AF_INET && length >= (SocketLen)sizeof(sockaddr_in)) {
69+
const sockaddr_in* ipv4 = reinterpret_cast<const sockaddr_in*>(address);
70+
Array<int> result = Array_obj<int>::__new(2, 2);
71+
result[0] = *(const int*)&ipv4->sin_addr;
72+
result[1] = ntohs(ipv4->sin_port);
73+
return result;
74+
}
75+
76+
if (address->sa_family == AF_INET6 && length >= (SocketLen)sizeof(sockaddr_in6)) {
77+
const sockaddr_in6* ipv6 = reinterpret_cast<const sockaddr_in6*>(address);
78+
const unsigned char* bytes = reinterpret_cast<const unsigned char*>(&ipv6->sin6_addr);
79+
Array<int> result = Array_obj<int>::__new(18, 18);
80+
result[0] = 0;
81+
result[1] = ntohs(ipv6->sin6_port);
82+
for (int i = 0; i < 16; ++i) {
83+
result[i + 2] = bytes[i];
84+
}
85+
return result;
86+
}
87+
88+
return null();
89+
}
90+
91+
static Array<int> crossbyte_socket_name_info(Dynamic socket, bool peer) {
92+
SOCKET nativeSocket = crossbyte_val_sock(socket);
93+
sockaddr_storage address;
94+
memset(&address, 0, sizeof(address));
95+
SocketLen addressLength = sizeof(address);
96+
97+
hx::EnterGCFreeZone();
98+
int status = peer
99+
? getpeername(nativeSocket, reinterpret_cast<sockaddr*>(&address), &addressLength)
100+
: getsockname(nativeSocket, reinterpret_cast<sockaddr*>(&address), &addressLength);
101+
hx::ExitGCFreeZone();
102+
103+
if (status == SOCKET_ERROR) {
104+
return null();
105+
}
106+
107+
return crossbyte_address_to_array(reinterpret_cast<sockaddr*>(&address), addressLength);
108+
}
109+
110+
static void crossbyte_block_error() {
111+
#if defined(HX_WINDOWS) || defined(NEKO_WINDOWS)
112+
int error = WSAGetLastError();
113+
hx::ExitGCFreeZone();
114+
if (error == WSAEWOULDBLOCK || error == WSAEALREADY) {
115+
hx::Throw(HX_CSTRING("Blocking"));
116+
}
117+
#else
118+
hx::ExitGCFreeZone();
119+
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS || errno == EALREADY) {
120+
hx::Throw(HX_CSTRING("Blocking"));
121+
}
122+
#endif
123+
hx::Throw(HX_CSTRING("Socket operation failed"));
124+
}
125+
126+
} // namespace
127+
128+
Dynamic crossbyte_socket_accept(Dynamic socket) {
129+
SOCKET nativeSocket = crossbyte_val_sock(socket);
130+
sockaddr_storage address;
131+
memset(&address, 0, sizeof(address));
132+
SocketLen addressLength = sizeof(address);
133+
134+
hx::EnterGCFreeZone();
135+
SOCKET accepted = accept(nativeSocket, reinterpret_cast<sockaddr*>(&address), &addressLength);
136+
if (accepted == INVALID_SOCKET) {
137+
crossbyte_block_error();
138+
}
139+
hx::ExitGCFreeZone();
140+
141+
SocketWrapper* wrapper = new SocketWrapper();
142+
wrapper->socket = accepted;
143+
return wrapper;
144+
}
145+
146+
Array<int> crossbyte_socket_host_info(Dynamic socket) {
147+
return crossbyte_socket_name_info(socket, false);
148+
}
149+
150+
Array<int> crossbyte_socket_peer_info(Dynamic socket) {
151+
return crossbyte_socket_name_info(socket, true);
152+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <hxcpp.h>
4+
5+
Dynamic crossbyte_socket_accept(Dynamic socket);
6+
Array<int> crossbyte_socket_host_info(Dynamic socket);
7+
Array<int> crossbyte_socket_peer_info(Dynamic socket);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package crossbyte._internal.net;
2+
3+
#if cpp
4+
@:buildXml("
5+
<files id='haxe'>
6+
<compilerflag value='-I../../src/crossbyte/_internal/net'/>
7+
<file name='../../src/crossbyte/_internal/net/NativeSocketAddress.cpp'>
8+
<depend name='../../src/crossbyte/_internal/net/NativeSocketAddress.h'/>
9+
</file>
10+
</files>
11+
")
12+
@:include("NativeSocketAddress.h")
13+
extern class NativeSocketAddress {
14+
@:native("crossbyte_socket_accept") public static function accept(socket:Dynamic):Dynamic;
15+
@:native("crossbyte_socket_host_info") public static function hostInfo(socket:Dynamic):Array<Int>;
16+
@:native("crossbyte_socket_peer_info") public static function peerInfo(socket:Dynamic):Array<Int>;
17+
}
18+
#end

‎src/crossbyte/net/ServerSocket.hx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class ServerSocket extends EventDispatcher {
218218
cbSocket.__connected = true;
219219
cbSocket.__timestamp = Sys.time();
220220

221-
cbSocket.__host = socket.peer().host.host;
221+
cbSocket.__host = socket.peer().host.toString();
222222
cbSocket.__port = socket.peer().port;
223223

224224
cbSocket.__output = new ByteArray();

‎src/crossbyte/net/Socket.hx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,15 +1113,15 @@ class Socket extends EventDispatcher implements IDataInput implements IDataOutpu
11131113
}
11141114

11151115
@:noCompletion private function get_localAddress():String {
1116-
return __socket.host().host.host;
1116+
return __socket.host().host.toString();
11171117
}
11181118

11191119
@:noCompletion private function get_localPort():Int {
11201120
return __socket.host().port;
11211121
}
11221122

11231123
@:noCompletion private function get_remoteAddress():String {
1124-
return __socket.peer().host.host;
1124+
return __socket.peer().host.toString();
11251125
}
11261126

11271127
@:noCompletion private function get_remotePort():Int {

0 commit comments

Comments
 (0)