sfml.js 1.0.0-rc.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +16 -11
- package/README.ko.md +16 -11
- package/README.md +16 -11
- package/README.zh-CN.md +16 -11
- package/assets/breakout.png +0 -0
- package/binding.gyp +23 -16
- package/dist/audio/effect-processor.js +9 -3
- package/dist/audio/input-sound-file.js +41 -15
- package/dist/audio/listener.js +33 -11
- package/dist/audio/music.js +41 -18
- package/dist/audio/output-sound-file.js +4 -2
- package/dist/audio/playback-device.js +11 -3
- package/dist/audio/sound-buffer-recorder.js +8 -5
- package/dist/audio/sound-buffer.js +40 -17
- package/dist/audio/sound-file-factory.js +21 -9
- package/dist/audio/sound-file-reader.js +21 -7
- package/dist/audio/sound-recorder.js +36 -15
- package/dist/audio/sound-source.js +117 -40
- package/dist/audio/sound-stream.js +14 -5
- package/dist/audio/sound.js +9 -4
- package/dist/audio/value.js +42 -16
- package/dist/graphics/blend-mode.js +2 -1
- package/dist/graphics/boolean-vector.d.ts +1 -2
- package/dist/graphics/boolean-vector.js +19 -9
- package/dist/graphics/color.js +2 -1
- package/dist/graphics/convex-shape.js +7 -3
- package/dist/graphics/font.js +52 -23
- package/dist/graphics/glsl-matrix.js +8 -3
- package/dist/graphics/glsl.js +47 -16
- package/dist/graphics/glyph.js +27 -10
- package/dist/graphics/image.js +30 -14
- package/dist/graphics/rect.js +15 -8
- package/dist/graphics/render-states.js +18 -9
- package/dist/graphics/render-target.js +49 -23
- package/dist/graphics/render-texture.js +24 -12
- package/dist/graphics/render-window.js +6 -3
- package/dist/graphics/shader.js +67 -32
- package/dist/graphics/shape.js +25 -13
- package/dist/graphics/sprite.js +19 -9
- package/dist/graphics/stencil-mode.js +10 -6
- package/dist/graphics/swap.js +2 -1
- package/dist/graphics/text.js +123 -48
- package/dist/graphics/texture.js +48 -24
- package/dist/graphics/transform.js +29 -13
- package/dist/graphics/transformable.js +2 -1
- package/dist/graphics/vertex-array.js +40 -18
- package/dist/graphics/vertex-buffer.js +39 -19
- package/dist/graphics/vertex.js +6 -3
- package/dist/graphics/view.js +30 -14
- package/dist/index.d.ts +4 -4
- package/dist/internal/audio-codec-registry.js +12 -6
- package/dist/internal/audio-input-stream.js +2 -1
- package/dist/internal/audio-music-pump.js +58 -24
- package/dist/internal/audio-recorder-session.js +17 -7
- package/dist/internal/audio-worker-session.js +28 -10
- package/dist/internal/audio-worker.js +40 -21
- package/dist/internal/glsl-value.d.ts +3 -1
- package/dist/internal/glsl-value.js +18 -8
- package/dist/internal/graphics-state.js +29 -15
- package/dist/internal/input-stream.d.ts +3 -1
- package/dist/internal/input-stream.js +16 -7
- package/dist/internal/numeric-properties.js +2 -1
- package/dist/internal/render-states.js +2 -1
- package/dist/internal/scalar.js +22 -13
- package/dist/internal/shaped-glyph.d.ts +3 -1
- package/dist/internal/shaped-glyph.js +12 -4
- package/dist/internal/struct-view.d.ts +3 -1
- package/dist/internal/struct-view.js +3 -1
- package/dist/internal/transformable.js +2 -1
- package/dist/network/common.js +23 -11
- package/dist/network/dns.js +25 -17
- package/dist/network/ftp.js +43 -18
- package/dist/network/http.js +126 -56
- package/dist/network/ip-address.js +50 -25
- package/dist/network/packet.js +63 -25
- package/dist/network/sftp.js +175 -74
- package/dist/network/socket-selector.js +28 -14
- package/dist/network/socket.js +71 -30
- package/dist/network/tcp-listener.js +42 -19
- package/dist/network/tcp-socket.js +111 -53
- package/dist/network/tls-material.js +2 -1
- package/dist/network/udp-socket.js +68 -33
- package/dist/system/angle.js +2 -1
- package/dist/system/clock.js +4 -2
- package/dist/system/err.js +5 -2
- package/dist/system/file-input-stream.js +8 -4
- package/dist/system/input-stream.js +6 -3
- package/dist/system/operators.d.ts +1 -1
- package/dist/system/operators.js +90 -50
- package/dist/system/sleep.js +4 -2
- package/dist/system/time.js +12 -6
- package/dist/system/vector2.js +6 -7
- package/dist/system/vector3.js +11 -6
- package/dist/window/clipboard.js +2 -1
- package/dist/window/context.js +10 -5
- package/dist/window/cursor.js +2 -1
- package/dist/window/event.js +9 -5
- package/dist/window/mouse.js +4 -2
- package/dist/window/video-mode.js +2 -1
- package/dist/window/window.js +19 -12
- package/native/addon.cc +25 -12
- package/native/audio-binding.cc +266 -126
- package/native/audio-codec-binding.cc +147 -77
- package/native/audio-device-binding.cc +76 -49
- package/native/audio-file-binding.cc +116 -67
- package/native/audio-recorder-binding.cc +147 -98
- package/native/audio-recorder.h +1 -1
- package/native/audio-state.h +53 -30
- package/native/audio-stream-binding.cc +148 -94
- package/native/audio-stream.h +5 -4
- package/native/audio-worker-binding.cc +163 -81
- package/native/audio-worker.h +35 -19
- package/native/batch-drawable.cc +29 -16
- package/native/batch-drawable.h +11 -8
- package/native/context-binding.cc +40 -27
- package/native/context-settings.h +15 -8
- package/native/deferred-delete.cc +9 -6
- package/native/deferred-delete.h +7 -8
- package/native/error-stream-binding.cc +42 -28
- package/native/font-binding.cc +89 -48
- package/native/graphics-binding.cc +122 -72
- package/native/graphics-state.h +50 -36
- package/native/image-binding.cc +38 -24
- package/native/image-state.h +26 -18
- package/native/input-binding.cc +42 -27
- package/native/render-window-binding.cc +38 -23
- package/native/shader-binding.cc +115 -50
- package/native/shape-binding.cc +94 -49
- package/native/shape-state.h +18 -9
- package/native/shaped-glyph-storage.h +9 -5
- package/native/stream-binding.cc +143 -117
- package/native/system-binding.cc +17 -12
- package/native/text-binding.cc +68 -35
- package/native/text-state.h +25 -18
- package/native/texture-binding.cc +42 -33
- package/native/texture-resource.h +20 -12
- package/native/time-value.h +3 -1
- package/native/transformable-binding.cc +12 -6
- package/native/transformable-state.h +6 -6
- package/native/value-binding.h +46 -28
- package/native/value-layout.h +10 -10
- package/native/values-binding.cc +29 -23
- package/native/vertex-binding.cc +18 -12
- package/native/vertex-buffer-binding.cc +38 -24
- package/native/vertex-storage.h +2 -2
- package/native/view-data.h +4 -3
- package/native/window-binding.cc +75 -45
- package/native/window-event.h +20 -11
- package/native/window-owner.h +15 -12
- package/package.json +43 -50
- package/src/audio/effect-processor.ts +9 -3
- package/src/audio/input-sound-file.ts +108 -36
- package/src/audio/listener.ts +39 -13
- package/src/audio/music.ts +112 -47
- package/src/audio/output-sound-file.ts +40 -11
- package/src/audio/playback-device.ts +23 -8
- package/src/audio/sound-buffer-recorder.ts +35 -13
- package/src/audio/sound-buffer.ts +166 -58
- package/src/audio/sound-file-factory.ts +127 -49
- package/src/audio/sound-file-reader.ts +49 -15
- package/src/audio/sound-recorder.ts +100 -34
- package/src/audio/sound-source.ts +142 -45
- package/src/audio/sound-stream.ts +15 -5
- package/src/audio/sound.ts +12 -4
- package/src/audio/value.ts +89 -26
- package/src/graphics/blend-mode.ts +3 -1
- package/src/graphics/boolean-vector.ts +56 -26
- package/src/graphics/color.ts +3 -1
- package/src/graphics/convex-shape.ts +10 -3
- package/src/graphics/font.ts +171 -50
- package/src/graphics/glsl-matrix.ts +9 -3
- package/src/graphics/glsl.ts +81 -35
- package/src/graphics/glyph.ts +67 -26
- package/src/graphics/image.ts +119 -45
- package/src/graphics/rect.ts +56 -24
- package/src/graphics/render-states.ts +31 -14
- package/src/graphics/render-target.ts +197 -81
- package/src/graphics/render-texture.ts +36 -12
- package/src/graphics/render-window.ts +9 -3
- package/src/graphics/shader.ts +186 -61
- package/src/graphics/shape.ts +93 -35
- package/src/graphics/sprite.ts +64 -25
- package/src/graphics/stencil-mode.ts +12 -6
- package/src/graphics/swap.ts +3 -2
- package/src/graphics/text.ts +229 -74
- package/src/graphics/texture.ts +180 -61
- package/src/graphics/transform.ts +132 -47
- package/src/graphics/transformable.ts +3 -1
- package/src/graphics/vertex-array.ts +87 -35
- package/src/graphics/vertex-buffer.ts +97 -33
- package/src/graphics/vertex.ts +9 -3
- package/src/graphics/view.ts +59 -21
- package/src/index.ts +101 -23
- package/src/internal/audio-codec-registry.ts +18 -6
- package/src/internal/audio-input-stream.ts +6 -2
- package/src/internal/audio-music-pump.ts +116 -41
- package/src/internal/audio-native.ts +249 -96
- package/src/internal/audio-recorder-session.ts +68 -24
- package/src/internal/audio-worker-session.ts +65 -21
- package/src/internal/audio-worker.ts +85 -41
- package/src/internal/glsl-value.ts +26 -11
- package/src/internal/graphics-state.ts +57 -20
- package/src/internal/input-stream.ts +23 -10
- package/src/internal/native.ts +252 -99
- package/src/internal/numeric-properties.ts +2 -1
- package/src/internal/render-states.ts +3 -1
- package/src/internal/scalar.ts +33 -14
- package/src/internal/shaped-glyph.ts +25 -12
- package/src/internal/struct-view.ts +40 -13
- package/src/internal/transformable.ts +3 -1
- package/src/network/common.ts +35 -11
- package/src/network/dns.ts +95 -38
- package/src/network/ftp.ts +129 -47
- package/src/network/http.ts +232 -77
- package/src/network/ip-address.ts +118 -48
- package/src/network/packet.ts +161 -54
- package/src/network/sftp.ts +498 -168
- package/src/network/socket-selector.ts +66 -26
- package/src/network/socket.ts +133 -46
- package/src/network/tcp-listener.ts +78 -29
- package/src/network/tcp-socket.ts +240 -95
- package/src/network/tls-material.ts +6 -2
- package/src/network/udp-socket.ts +147 -53
- package/src/system/angle.ts +3 -1
- package/src/system/clock.ts +12 -4
- package/src/system/err.ts +5 -2
- package/src/system/file-input-stream.ts +12 -4
- package/src/system/input-stream.ts +9 -3
- package/src/system/operators.ts +290 -115
- package/src/system/sleep.ts +6 -2
- package/src/system/time.ts +18 -6
- package/src/system/timeout-with-predicate.ts +19 -8
- package/src/system/vector2.ts +101 -49
- package/src/system/vector3.ts +85 -36
- package/src/window/clipboard.ts +3 -1
- package/src/window/context.ts +15 -5
- package/src/window/cursor.ts +3 -1
- package/src/window/event.ts +26 -12
- package/src/window/mouse.ts +6 -2
- package/src/window/video-mode.ts +3 -1
- package/src/window/window.ts +119 -37
- package/test/native/audio-probes.cc +74 -36
- package/test/native/deferred-delete-probes.cc +17 -11
- package/test/native/drawable-probes.cc +70 -36
- package/test/native/exception-probes.cc +14 -9
- package/test/native/image-probes.cc +20 -10
- package/test/native/matrix-probes.cc +8 -4
- package/test/native/network-oracle.cc +17 -11
- package/test/native/render-texture-probes.cc +33 -24
- package/test/native/shader-probes.cc +9 -9
- package/test/native/shape-probes.cc +37 -23
- package/test/native/sprite-probes.cc +41 -25
- package/test/native/system-probes.cc +58 -35
- package/test/native/text-probes.cc +77 -44
- package/test/native/texture-probes.cc +43 -26
- package/test/native/time-probes.cc +18 -12
- package/test/native/transform-probes.cc +15 -10
- package/test/native/transformable-probes.cc +23 -17
- package/test/native/value-oracle.h +9 -4
- package/test/native/value-probes.cc +31 -23
- package/test/native/vertex-array-probes.cc +22 -14
- package/test/native/vertex-buffer-probes.cc +28 -17
- package/test/native/view-probes.cc +29 -17
- package/test/native/window-probes.cc +66 -33
- package/tools/ensure-sfml.cjs +9 -0
- package/tools/generate-network-constants.cjs +11 -6
- package/tools/generate-window-constants.cjs +31 -12
- package/docs/README.ja.md +0 -30
- package/docs/README.ko.md +0 -30
- package/docs/README.md +0 -30
- package/docs/README.zh-CN.md +0 -30
- package/docs/api.ja.md +0 -84
- package/docs/api.ko.md +0 -84
- package/docs/api.md +0 -81
- package/docs/api.zh-CN.md +0 -81
- package/docs/audio.ja.md +0 -249
- package/docs/audio.ko.md +0 -251
- package/docs/audio.md +0 -252
- package/docs/audio.zh-CN.md +0 -252
- package/docs/concepts.ja.md +0 -130
- package/docs/concepts.ko.md +0 -130
- package/docs/concepts.md +0 -131
- package/docs/concepts.zh-CN.md +0 -131
- package/docs/development.ja.md +0 -148
- package/docs/development.ko.md +0 -148
- package/docs/development.md +0 -150
- package/docs/development.zh-CN.md +0 -150
- package/docs/getting-started.ja.md +0 -154
- package/docs/getting-started.ko.md +0 -154
- package/docs/getting-started.md +0 -156
- package/docs/getting-started.zh-CN.md +0 -156
- package/docs/graphics.ja.md +0 -256
- package/docs/graphics.ko.md +0 -254
- package/docs/graphics.md +0 -255
- package/docs/graphics.zh-CN.md +0 -257
- package/docs/network.ja.md +0 -214
- package/docs/network.ko.md +0 -216
- package/docs/network.md +0 -218
- package/docs/network.zh-CN.md +0 -218
- package/docs/system.ja.md +0 -169
- package/docs/system.ko.md +0 -171
- package/docs/system.md +0 -173
- package/docs/system.zh-CN.md +0 -173
- package/docs/window.ja.md +0 -138
- package/docs/window.ko.md +0 -140
- package/docs/window.md +0 -141
- package/docs/window.zh-CN.md +0 -141
- package/native/exception-boundary.h +0 -97
- package/native/numeric-fields.h +0 -96
- package/native/shared-handle.h +0 -69
- package/test/tsconfig.json +0 -8
- package/tools/configure-lsp.cjs +0 -23
- package/tools/lib_downloader.cjs +0 -92
- package/tools/lib_downloader_urls.cjs +0 -23
|
@@ -32,7 +32,10 @@ export enum ReadinessType {
|
|
|
32
32
|
* @ko 이번 wait에서 얻은 준비 상태 비트 플래그를 받습니다.
|
|
33
33
|
*/
|
|
34
34
|
export type ReadyCallback = (readiness: number) => void;
|
|
35
|
-
interface Entry {
|
|
35
|
+
interface Entry {
|
|
36
|
+
mask: number;
|
|
37
|
+
callback?: ReadyCallback;
|
|
38
|
+
}
|
|
36
39
|
|
|
37
40
|
/**
|
|
38
41
|
* Wait asynchronously for a set of sockets to become ready,
|
|
@@ -69,7 +72,9 @@ export class SocketSelector {
|
|
|
69
72
|
* @ko 빈 선택기를 생성하거나 등록 집합, 콜백 및 준비 상태 스냅샷을 복사합니다.
|
|
70
73
|
*/
|
|
71
74
|
constructor(source?: SocketSelector) {
|
|
72
|
-
if (source !== undefined)
|
|
75
|
+
if (source !== undefined) {
|
|
76
|
+
this.assign(source);
|
|
77
|
+
}
|
|
73
78
|
}
|
|
74
79
|
/**
|
|
75
80
|
* Copy another selector's registrations and snapshot, returning this.
|
|
@@ -84,8 +89,12 @@ export class SocketSelector {
|
|
|
84
89
|
if (!(source instanceof SocketSelector)) {
|
|
85
90
|
throw new TypeError('Expected a SocketSelector');
|
|
86
91
|
}
|
|
87
|
-
if (source === this)
|
|
88
|
-
|
|
92
|
+
if (source === this) {
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
if (this.waiting) {
|
|
96
|
+
throw new Error('Finish the pending wait before assigning');
|
|
97
|
+
}
|
|
89
98
|
this.entries = new Map(source.entries);
|
|
90
99
|
this.ready = new Map(source.ready);
|
|
91
100
|
return this;
|
|
@@ -99,15 +108,24 @@ export class SocketSelector {
|
|
|
99
108
|
* @ko 활성 소켓과 준비 조건을 등록합니다. 중복되거나 열리지 않았으면 false를
|
|
100
109
|
* 반환하며 콜백은 명시적으로 dispatch해야 합니다.
|
|
101
110
|
*/
|
|
102
|
-
add(
|
|
103
|
-
|
|
104
|
-
|
|
111
|
+
add(
|
|
112
|
+
socket: Socket,
|
|
113
|
+
readiness = ReadinessType.Receive,
|
|
114
|
+
callback?: ReadyCallback,
|
|
115
|
+
): boolean {
|
|
116
|
+
if (!(socket instanceof Socket)) {
|
|
117
|
+
throw new TypeError('Expected a Socket');
|
|
118
|
+
}
|
|
105
119
|
uint(readiness, 3);
|
|
106
|
-
if (!readiness)
|
|
120
|
+
if (!readiness) {
|
|
121
|
+
throw new RangeError('Expected Receive and/or Send');
|
|
122
|
+
}
|
|
107
123
|
if (callback !== undefined && typeof callback !== 'function') {
|
|
108
124
|
throw new TypeError('Expected a callback');
|
|
109
125
|
}
|
|
110
|
-
if (!socket[socketAlive]() || this.entries.has(socket))
|
|
126
|
+
if (!socket[socketAlive]() || this.entries.has(socket)) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
111
129
|
this.entries.set(socket, { mask: readiness, callback });
|
|
112
130
|
this.cancel?.abort();
|
|
113
131
|
return true;
|
|
@@ -153,30 +171,48 @@ export class SocketSelector {
|
|
|
153
171
|
*/
|
|
154
172
|
async wait(timeout: NetworkTime = Time.Zero): Promise<boolean> {
|
|
155
173
|
const milliseconds = millisecondsOf(timeout);
|
|
156
|
-
if (this.waiting)
|
|
174
|
+
if (this.waiting) {
|
|
175
|
+
throw new Error('A selector wait is already pending');
|
|
176
|
+
}
|
|
157
177
|
this.ready.clear();
|
|
158
|
-
if (!this.entries.size)
|
|
178
|
+
if (!this.entries.size) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
159
181
|
this.waiting = true;
|
|
160
182
|
this.cancel = new AbortController();
|
|
161
183
|
const events = new EventEmitter();
|
|
162
184
|
const notify = () => events.emit('change');
|
|
163
185
|
const sockets = [ ...this.entries.keys() ];
|
|
164
|
-
for (const socket of sockets)
|
|
186
|
+
for (const socket of sockets) {
|
|
187
|
+
socket[socketEvents].on('change', notify);
|
|
188
|
+
}
|
|
165
189
|
try {
|
|
166
|
-
await waitFor(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
190
|
+
await waitFor(
|
|
191
|
+
events,
|
|
192
|
+
() => {
|
|
193
|
+
let count = 0;
|
|
194
|
+
for (const [ socket, entry ] of this.entries) {
|
|
195
|
+
const mask = socket[socketReady](entry.mask);
|
|
196
|
+
if (mask) {
|
|
197
|
+
this.ready.set(socket, mask);
|
|
198
|
+
count++;
|
|
199
|
+
} else {
|
|
200
|
+
this.ready.delete(socket);
|
|
201
|
+
}
|
|
172
202
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
203
|
+
return (
|
|
204
|
+
count !== 0 ||
|
|
205
|
+
[ ...this.entries.keys() ].every(socket => !socket[socketAlive]())
|
|
206
|
+
);
|
|
207
|
+
},
|
|
208
|
+
milliseconds,
|
|
209
|
+
this.cancel.signal,
|
|
210
|
+
);
|
|
177
211
|
return this.ready.size > 0;
|
|
178
212
|
} finally {
|
|
179
|
-
for (const socket of sockets)
|
|
213
|
+
for (const socket of sockets) {
|
|
214
|
+
socket[socketEvents].off('change', notify);
|
|
215
|
+
}
|
|
180
216
|
this.waiting = false;
|
|
181
217
|
this.cancel = undefined;
|
|
182
218
|
}
|
|
@@ -199,8 +235,12 @@ export class SocketSelector {
|
|
|
199
235
|
* @ko 최근 wait에서 준비된 항목의 등록된 콜백을 동기적으로 호출합니다.
|
|
200
236
|
*/
|
|
201
237
|
dispatchReadyCallbacks(): void {
|
|
202
|
-
const callbacks = [ ...this.ready ].map(([ socket, mask ]) =>
|
|
203
|
-
|
|
204
|
-
|
|
238
|
+
const callbacks = [ ...this.ready ].map(([ socket, mask ]) => ({
|
|
239
|
+
callback: this.entries.get(socket)?.callback,
|
|
240
|
+
mask,
|
|
241
|
+
}));
|
|
242
|
+
for (const { callback, mask } of callbacks) {
|
|
243
|
+
callback?.(mask);
|
|
244
|
+
}
|
|
205
245
|
}
|
|
206
246
|
}
|
package/src/network/socket.ts
CHANGED
|
@@ -95,9 +95,15 @@ function closeDatagram(handle: UdpHandle): void {
|
|
|
95
95
|
handle.off('close', cleanup);
|
|
96
96
|
};
|
|
97
97
|
const close = () => {
|
|
98
|
-
try {
|
|
98
|
+
try {
|
|
99
|
+
handle.close();
|
|
100
|
+
} catch {
|
|
101
|
+
cleanup();
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
retry = () => {
|
|
105
|
+
queueMicrotask(close);
|
|
99
106
|
};
|
|
100
|
-
retry = () => { queueMicrotask(close); };
|
|
101
107
|
handle.on('error', retry);
|
|
102
108
|
handle.once('close', cleanup);
|
|
103
109
|
close();
|
|
@@ -173,7 +179,9 @@ export abstract class Socket {
|
|
|
173
179
|
* false이면 현재 큐 상태에 따라 NotReady 또는 Partial을 반환합니다.
|
|
174
180
|
*/
|
|
175
181
|
setBlocking(blocking: boolean): void {
|
|
176
|
-
if (typeof blocking !== 'boolean')
|
|
182
|
+
if (typeof blocking !== 'boolean') {
|
|
183
|
+
throw new TypeError('Expected a boolean');
|
|
184
|
+
}
|
|
177
185
|
this.blocking = blocking;
|
|
178
186
|
this.changed();
|
|
179
187
|
}
|
|
@@ -183,20 +191,30 @@ export abstract class Socket {
|
|
|
183
191
|
* @ja 非同期待機によるブロッキング動作を使うかどうかを返します。
|
|
184
192
|
* @ko 비동기 대기를 통한 블로킹 동작을 사용하는지 반환합니다.
|
|
185
193
|
*/
|
|
186
|
-
isBlocking(): boolean {
|
|
194
|
+
isBlocking(): boolean {
|
|
195
|
+
return this.blocking;
|
|
196
|
+
}
|
|
187
197
|
/**
|
|
188
198
|
* Return the latest underlying error, or undefined if none is recorded.
|
|
189
199
|
* @zh 返回最近的底层错误;未记录错误时为 undefined。
|
|
190
200
|
* @ja 最新の下位層のエラーを返し、記録がなければ undefined を返します。
|
|
191
201
|
* @ko 최근 하위 계층 오류를 반환하며 기록된 오류가 없으면 undefined입니다.
|
|
192
202
|
*/
|
|
193
|
-
getLastError(): Error | undefined {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
203
|
+
getLastError(): Error | undefined {
|
|
204
|
+
return this.failure;
|
|
205
|
+
}
|
|
206
|
+
protected changed(): void {
|
|
207
|
+
this[socketEvents].emit('change');
|
|
208
|
+
}
|
|
209
|
+
protected async awaitReady(
|
|
210
|
+
check: () => boolean,
|
|
211
|
+
generation: number,
|
|
212
|
+
): Promise<boolean> {
|
|
197
213
|
if (!check() && this.blocking && this.epoch === generation) {
|
|
198
|
-
await waitFor(
|
|
199
|
-
this
|
|
214
|
+
await waitFor(
|
|
215
|
+
this[socketEvents],
|
|
216
|
+
() => check() || !this.blocking || this.epoch !== generation,
|
|
217
|
+
);
|
|
200
218
|
}
|
|
201
219
|
return this.epoch === generation && check();
|
|
202
220
|
}
|
|
@@ -206,14 +224,18 @@ export abstract class Socket {
|
|
|
206
224
|
* @ja サブクラスで上書き可能な準備条件を Selector に公開します。
|
|
207
225
|
* @ko 하위 클래스에서 재정의할 수 있는 준비 조건을 Selector에 제공합니다.
|
|
208
226
|
*/
|
|
209
|
-
[socketReady](mask: number): number {
|
|
227
|
+
[socketReady](mask: number): number {
|
|
228
|
+
return this.getReadiness(mask);
|
|
229
|
+
}
|
|
210
230
|
/**
|
|
211
231
|
* Expose overridable liveness state to Selector.
|
|
212
232
|
* @zh 供 Selector 查询子类可覆盖的存活状态。
|
|
213
233
|
* @ja サブクラスで上書き可能な生存状態を Selector に公開します。
|
|
214
234
|
* @ko 하위 클래스에서 재정의할 수 있는 활성 상태를 Selector에 제공합니다.
|
|
215
235
|
*/
|
|
216
|
-
[socketAlive](): boolean {
|
|
236
|
+
[socketAlive](): boolean {
|
|
237
|
+
return this.isAlive();
|
|
238
|
+
}
|
|
217
239
|
/**
|
|
218
240
|
* Provide subclasses with the underlying Node transport,
|
|
219
241
|
* or undefined if not yet created.
|
|
@@ -223,7 +245,9 @@ export abstract class Socket {
|
|
|
223
245
|
* @ko 하위 클래스에 기반 Node 전송 객체 접근을 제공합니다.
|
|
224
246
|
* 아직 생성되지 않았으면 undefined입니다.
|
|
225
247
|
*/
|
|
226
|
-
protected getNativeHandle(): SocketHandle | undefined {
|
|
248
|
+
protected getNativeHandle(): SocketHandle | undefined {
|
|
249
|
+
return this.transport;
|
|
250
|
+
}
|
|
227
251
|
/**
|
|
228
252
|
* Allocate a transport for the given address family without implicitly
|
|
229
253
|
* connecting or binding a port.
|
|
@@ -244,12 +268,17 @@ export abstract class Socket {
|
|
|
244
268
|
* 원시 파일 디스크립터는 인수하지 않습니다.
|
|
245
269
|
*/
|
|
246
270
|
protected create(handle: SocketHandle): void;
|
|
247
|
-
protected create(
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
271
|
+
protected create(
|
|
272
|
+
value: IpAddressType | SocketHandle = IpAddressType.IpV4,
|
|
273
|
+
): void {
|
|
274
|
+
const handle =
|
|
275
|
+
typeof value === 'number'
|
|
276
|
+
? this.allocateHandle(uint(value, 1))
|
|
277
|
+
: value;
|
|
251
278
|
this.validateHandle(handle);
|
|
252
|
-
if (handle === this.transport)
|
|
279
|
+
if (handle === this.transport) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
253
282
|
this.close();
|
|
254
283
|
this.failure = undefined;
|
|
255
284
|
this.transport = handle;
|
|
@@ -272,15 +301,21 @@ export abstract class Socket {
|
|
|
272
301
|
this.detach = undefined;
|
|
273
302
|
this.transport = undefined;
|
|
274
303
|
this.onClose();
|
|
275
|
-
if (handle instanceof UdpHandle)
|
|
276
|
-
|
|
304
|
+
if (handle instanceof UdpHandle) {
|
|
305
|
+
closeDatagram(handle);
|
|
306
|
+
} else if (handle) {
|
|
277
307
|
// 销毁期间可能产生迟到的错误;不再保留捕获当前实例的监听闭包。
|
|
278
308
|
const ignore = () => {};
|
|
279
309
|
handle.on('error', ignore);
|
|
280
310
|
handle.once('close', () => handle.off('error', ignore));
|
|
281
|
-
if (handle instanceof TcpHandle)
|
|
282
|
-
|
|
283
|
-
|
|
311
|
+
if (handle instanceof TcpHandle) {
|
|
312
|
+
handle.destroy();
|
|
313
|
+
} else {
|
|
314
|
+
try {
|
|
315
|
+
handle.close();
|
|
316
|
+
} catch {
|
|
317
|
+
handle.off('error', ignore);
|
|
318
|
+
}
|
|
284
319
|
}
|
|
285
320
|
}
|
|
286
321
|
this.changed();
|
|
@@ -295,7 +330,8 @@ export abstract class Socket {
|
|
|
295
330
|
* 리스너는 Node Server를 사용하도록 재정의할 수 있습니다.
|
|
296
331
|
*/
|
|
297
332
|
protected allocateHandle(addressType: IpAddressType): SocketHandle {
|
|
298
|
-
return this.type === SocketType.Tcp
|
|
333
|
+
return this.type === SocketType.Tcp
|
|
334
|
+
? new TcpHandle()
|
|
299
335
|
: createSocket(addressType === IpAddressType.IpV4 ? 'udp4' : 'udp6');
|
|
300
336
|
}
|
|
301
337
|
/**
|
|
@@ -308,8 +344,11 @@ export abstract class Socket {
|
|
|
308
344
|
* 전송 객체 타입을 검증합니다.
|
|
309
345
|
*/
|
|
310
346
|
protected validateHandle(handle: SocketHandle): void {
|
|
311
|
-
if (
|
|
312
|
-
|
|
347
|
+
if (
|
|
348
|
+
this.type === SocketType.Udp
|
|
349
|
+
? !(handle instanceof UdpHandle)
|
|
350
|
+
: !(handle instanceof TcpHandle || handle instanceof Server)
|
|
351
|
+
) {
|
|
313
352
|
throw new TypeError('Expected a matching Node socket transport');
|
|
314
353
|
}
|
|
315
354
|
}
|
|
@@ -325,17 +364,36 @@ export abstract class Socket {
|
|
|
325
364
|
protected onCreate(): void {
|
|
326
365
|
const handle = this.transport!;
|
|
327
366
|
const generation = this.epoch;
|
|
328
|
-
const current = () =>
|
|
329
|
-
|
|
367
|
+
const current = () =>
|
|
368
|
+
this.epoch === generation && this.transport === handle;
|
|
369
|
+
const notify = () => {
|
|
370
|
+
if (current()) {
|
|
371
|
+
this.changed();
|
|
372
|
+
}
|
|
373
|
+
};
|
|
330
374
|
const error = (error: Error) => {
|
|
331
|
-
if (!current())
|
|
332
|
-
|
|
375
|
+
if (!current()) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
this.failure = error;
|
|
379
|
+
this.changed();
|
|
333
380
|
};
|
|
334
|
-
const events = [
|
|
335
|
-
|
|
381
|
+
const events = [
|
|
382
|
+
'readable',
|
|
383
|
+
'drain',
|
|
384
|
+
'connect',
|
|
385
|
+
'end',
|
|
386
|
+
'close',
|
|
387
|
+
'listening',
|
|
388
|
+
];
|
|
389
|
+
for (const event of events) {
|
|
390
|
+
handle.on(event, notify);
|
|
391
|
+
}
|
|
336
392
|
handle.on('error', error);
|
|
337
393
|
this.detach = () => {
|
|
338
|
-
for (const event of events)
|
|
394
|
+
for (const event of events) {
|
|
395
|
+
handle.off(event, notify);
|
|
396
|
+
}
|
|
339
397
|
handle.off('error', error);
|
|
340
398
|
};
|
|
341
399
|
}
|
|
@@ -360,10 +418,19 @@ export abstract class Socket {
|
|
|
360
418
|
*/
|
|
361
419
|
protected isAlive(): boolean {
|
|
362
420
|
const handle = this.transport;
|
|
363
|
-
if (handle instanceof TcpHandle)
|
|
364
|
-
|
|
421
|
+
if (handle instanceof TcpHandle) {
|
|
422
|
+
return !handle.destroyed;
|
|
423
|
+
}
|
|
424
|
+
if (handle instanceof Server) {
|
|
425
|
+
return handle.listening;
|
|
426
|
+
}
|
|
365
427
|
if (handle instanceof UdpHandle) {
|
|
366
|
-
try {
|
|
428
|
+
try {
|
|
429
|
+
handle.address();
|
|
430
|
+
return true;
|
|
431
|
+
} catch {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
367
434
|
}
|
|
368
435
|
return false;
|
|
369
436
|
}
|
|
@@ -384,12 +451,25 @@ export abstract class Socket {
|
|
|
384
451
|
const handle = this.transport;
|
|
385
452
|
let ready = 0;
|
|
386
453
|
if (handle instanceof TcpHandle) {
|
|
387
|
-
if (
|
|
454
|
+
if (
|
|
455
|
+
handle.readableLength ||
|
|
456
|
+
handle.readableEnded ||
|
|
457
|
+
handle.destroyed
|
|
458
|
+
) {
|
|
388
459
|
ready += 1;
|
|
389
460
|
}
|
|
390
|
-
if (
|
|
391
|
-
handle.
|
|
392
|
-
|
|
461
|
+
if (
|
|
462
|
+
handle.remoteAddress &&
|
|
463
|
+
!handle.connecting &&
|
|
464
|
+
!handle.destroyed &&
|
|
465
|
+
handle.writable &&
|
|
466
|
+
!handle.writableNeedDrain
|
|
467
|
+
) {
|
|
468
|
+
ready += 2;
|
|
469
|
+
}
|
|
470
|
+
} else if (handle instanceof UdpHandle && this.isAlive()) {
|
|
471
|
+
ready = 2;
|
|
472
|
+
}
|
|
393
473
|
// eslint-disable-next-line no-bitwise
|
|
394
474
|
return ready & mask;
|
|
395
475
|
}
|
|
@@ -402,13 +482,20 @@ export abstract class Socket {
|
|
|
402
482
|
* @ko 쓰기 또는 세대 취소를 기다립니다.
|
|
403
483
|
* 닫을 때 하위 쓰기 콜백의 실행 여부에 의존하지 않습니다.
|
|
404
484
|
*/
|
|
405
|
-
protected async awaitWrite(
|
|
406
|
-
|
|
485
|
+
protected async awaitWrite(
|
|
486
|
+
completion: Promise<SocketStatus>,
|
|
487
|
+
generation: number,
|
|
488
|
+
): Promise<SocketStatus> {
|
|
407
489
|
const cancel = new AbortController();
|
|
408
|
-
const closed = waitFor(
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
490
|
+
const closed = waitFor(
|
|
491
|
+
this[socketEvents],
|
|
492
|
+
() => this.epoch !== generation,
|
|
493
|
+
0,
|
|
494
|
+
cancel.signal,
|
|
495
|
+
).then(() => SocketStatus.Disconnected);
|
|
496
|
+
try {
|
|
497
|
+
return await Promise.race([ completion, closed ]);
|
|
498
|
+
} finally {
|
|
412
499
|
cancel.abort();
|
|
413
500
|
}
|
|
414
501
|
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { createServer, Server, type Socket as NetSocket } from 'node:net';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
Socket,
|
|
4
|
+
SocketStatus as Status,
|
|
5
|
+
socketEvents,
|
|
6
|
+
SocketType,
|
|
7
|
+
type SocketHandle,
|
|
8
|
+
} from './socket';
|
|
4
9
|
import { IpAddress } from './ip-address';
|
|
5
10
|
import { TcpSocket, adoptTcp } from './tcp-socket';
|
|
6
11
|
import { SerialQueue, uint, waitFor } from './common';
|
|
@@ -28,7 +33,9 @@ export class TcpListener extends Socket {
|
|
|
28
33
|
* @ja まだ待ち受けていない TCP リスナーを作成します。
|
|
29
34
|
* @ko 아직 수신 대기하지 않는 TCP 리스너를 생성합니다.
|
|
30
35
|
*/
|
|
31
|
-
constructor() {
|
|
36
|
+
constructor() {
|
|
37
|
+
super(SocketType.Tcp);
|
|
38
|
+
}
|
|
32
39
|
/**
|
|
33
40
|
* Return the listening port, or 0 when not listening.
|
|
34
41
|
* @zh 返回监听端口;未监听时为 0。
|
|
@@ -48,19 +55,32 @@ export class TcpListener extends Socket {
|
|
|
48
55
|
* @ko 비동기로 바인딩하고 수신 대기합니다. port가 0이면 자동 할당하며,
|
|
49
56
|
* 기본적으로 모든 IPv4 인터페이스에 바인딩합니다.
|
|
50
57
|
*/
|
|
51
|
-
async listen(
|
|
52
|
-
|
|
58
|
+
async listen(
|
|
59
|
+
port: number,
|
|
60
|
+
address: IpAddress = IpAddress.Any,
|
|
61
|
+
): Promise<Status> {
|
|
53
62
|
uint(port, 65535);
|
|
54
|
-
if (!(address instanceof IpAddress))
|
|
63
|
+
if (!(address instanceof IpAddress)) {
|
|
64
|
+
throw new TypeError('Expected IpAddress');
|
|
65
|
+
}
|
|
55
66
|
this.create(address.getType());
|
|
56
67
|
const generation = this.epoch;
|
|
57
68
|
const server = this.transport!;
|
|
58
|
-
const ready = waitFor(
|
|
59
|
-
|
|
60
|
-
|
|
69
|
+
const ready = waitFor(
|
|
70
|
+
this[socketEvents],
|
|
71
|
+
() => this.listening || !!this.failure || generation !== this.epoch,
|
|
72
|
+
);
|
|
73
|
+
server.listen({
|
|
74
|
+
host: address.toString(),
|
|
75
|
+
port,
|
|
76
|
+
ipv6Only: address.isV6(),
|
|
77
|
+
});
|
|
61
78
|
await ready;
|
|
62
|
-
return generation !== this.epoch
|
|
63
|
-
|
|
79
|
+
return generation !== this.epoch
|
|
80
|
+
? Status.Disconnected
|
|
81
|
+
: this.listening
|
|
82
|
+
? Status.Done
|
|
83
|
+
: Status.Error;
|
|
64
84
|
}
|
|
65
85
|
/**
|
|
66
86
|
* Allocate a Server for the listener without creating a client transport
|
|
@@ -81,7 +101,9 @@ export class TcpListener extends Socket {
|
|
|
81
101
|
* @ko 이 리스너는 Node Server만 인수합니다.
|
|
82
102
|
*/
|
|
83
103
|
protected validateHandle(handle: SocketHandle): void {
|
|
84
|
-
if (!(handle instanceof Server))
|
|
104
|
+
if (!(handle instanceof Server)) {
|
|
105
|
+
throw new TypeError('Expected Node Server');
|
|
106
|
+
}
|
|
85
107
|
}
|
|
86
108
|
/**
|
|
87
109
|
* Set up the accept queue and listening state, supporting adoption
|
|
@@ -95,7 +117,8 @@ export class TcpListener extends Socket {
|
|
|
95
117
|
protected onCreate(): void {
|
|
96
118
|
const server = this.transport!;
|
|
97
119
|
const generation = this.epoch;
|
|
98
|
-
const current = () =>
|
|
120
|
+
const current = () =>
|
|
121
|
+
server === this.transport && generation === this.epoch;
|
|
99
122
|
this.listening = server.listening;
|
|
100
123
|
const connection = (socket: NetSocket) => {
|
|
101
124
|
if (!current() || this.pending.length >= 128) {
|
|
@@ -107,25 +130,35 @@ export class TcpListener extends Socket {
|
|
|
107
130
|
this.changed();
|
|
108
131
|
};
|
|
109
132
|
const error = (error: Error) => {
|
|
110
|
-
if (!current())
|
|
133
|
+
if (!current()) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
111
136
|
this.failure = error;
|
|
112
137
|
this.listening = false;
|
|
113
138
|
this.changed();
|
|
114
139
|
};
|
|
115
140
|
const listening = () => {
|
|
116
|
-
if (!current())
|
|
117
|
-
|
|
141
|
+
if (!current()) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
this.listening = true;
|
|
145
|
+
this.changed();
|
|
118
146
|
};
|
|
119
147
|
const closed = () => {
|
|
120
|
-
if (!current())
|
|
121
|
-
|
|
148
|
+
if (!current()) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.listening = false;
|
|
152
|
+
this.changed();
|
|
122
153
|
};
|
|
123
|
-
server
|
|
154
|
+
server
|
|
155
|
+
.on('connection', connection)
|
|
124
156
|
.on('error', error)
|
|
125
157
|
.on('listening', listening)
|
|
126
158
|
.on('close', closed);
|
|
127
159
|
this.detach = () => {
|
|
128
|
-
server
|
|
160
|
+
server
|
|
161
|
+
.off('connection', connection)
|
|
129
162
|
.off('error', error)
|
|
130
163
|
.off('listening', listening)
|
|
131
164
|
.off('close', closed);
|
|
@@ -137,7 +170,9 @@ export class TcpListener extends Socket {
|
|
|
137
170
|
* @ja 待ち受けを停止し、まだ accept されていない接続を閉じます。
|
|
138
171
|
* @ko 수신 대기를 중지하고 아직 accept하지 않은 연결을 닫습니다.
|
|
139
172
|
*/
|
|
140
|
-
close(): void {
|
|
173
|
+
close(): void {
|
|
174
|
+
super.close();
|
|
175
|
+
}
|
|
141
176
|
/**
|
|
142
177
|
* Clear connections from the previous generation that were not accepted.
|
|
143
178
|
* @zh 清空旧代际中尚未接受的连接。
|
|
@@ -146,7 +181,9 @@ export class TcpListener extends Socket {
|
|
|
146
181
|
*/
|
|
147
182
|
protected onClose(): void {
|
|
148
183
|
this.listening = false;
|
|
149
|
-
for (const socket of this.pending)
|
|
184
|
+
for (const socket of this.pending) {
|
|
185
|
+
socket.destroy();
|
|
186
|
+
}
|
|
150
187
|
this.pending = [];
|
|
151
188
|
}
|
|
152
189
|
/**
|
|
@@ -160,14 +197,22 @@ export class TcpListener extends Socket {
|
|
|
160
197
|
* 논블로킹 모드에서 연결이 없으면 NotReady를 반환합니다.
|
|
161
198
|
*/
|
|
162
199
|
accept(socket: TcpSocket): Promise<Status> {
|
|
163
|
-
if (!(socket instanceof TcpSocket))
|
|
200
|
+
if (!(socket instanceof TcpSocket)) {
|
|
201
|
+
throw new TypeError('Expected TcpSocket');
|
|
202
|
+
}
|
|
164
203
|
const generation = this.epoch;
|
|
165
204
|
return this.accepts.run(async () => {
|
|
166
|
-
await this.awaitReady(
|
|
167
|
-
|
|
168
|
-
|
|
205
|
+
await this.awaitReady(
|
|
206
|
+
() => this.pending.length > 0 || !this.listening,
|
|
207
|
+
generation,
|
|
208
|
+
);
|
|
209
|
+
if (generation !== this.epoch || !this.listening) {
|
|
210
|
+
return Status.Error;
|
|
211
|
+
}
|
|
169
212
|
const accepted = this.pending.shift();
|
|
170
|
-
if (!accepted)
|
|
213
|
+
if (!accepted) {
|
|
214
|
+
return Status.NotReady;
|
|
215
|
+
}
|
|
171
216
|
socket[adoptTcp](accepted);
|
|
172
217
|
return Status.Done;
|
|
173
218
|
});
|
|
@@ -178,7 +223,9 @@ export class TcpListener extends Socket {
|
|
|
178
223
|
* @ja 待ち受け状態を返します。派生クラスで拡張できます。
|
|
179
224
|
* @ko 수신 대기 상태를 반환하며 파생 클래스에서 확장할 수 있습니다.
|
|
180
225
|
*/
|
|
181
|
-
protected isAlive(): boolean {
|
|
226
|
+
protected isAlive(): boolean {
|
|
227
|
+
return this.listening;
|
|
228
|
+
}
|
|
182
229
|
/**
|
|
183
230
|
* Report receive readiness when the accept queue is not empty.
|
|
184
231
|
* @zh 接受队列非空时报告接收就绪。
|
|
@@ -189,5 +236,7 @@ export class TcpListener extends Socket {
|
|
|
189
236
|
// eslint-disable-next-line no-bitwise
|
|
190
237
|
return this.pending.length ? mask & 1 : 0;
|
|
191
238
|
}
|
|
192
|
-
protected getNativeHandle(): Server | undefined {
|
|
239
|
+
protected getNativeHandle(): Server | undefined {
|
|
240
|
+
return this.transport;
|
|
241
|
+
}
|
|
193
242
|
}
|