Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 41x 41x 321x 321x 321x 321x 41x 41x 41x 41x 41x 642x 83x 83x 638x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 321x 642x 238x 238x 238x 238x 238x 238x 642x 41x 41x 475x 475x 475x 475x 41x 41x 558x 558x 558x 558x 41x 41x 321x 321x 321x 321x 41x 41x 321x 321x 321x 321x 41x 41x 41x 41x 41x 41x 41x 41x 3459x 3459x 3459x 3459x 3459x 3459x 3459x 2x 2x 3459x 3459x 3459x 3459x 41x 41x 7307x 164x 164x 7143x 7143x 7143x 7307x 254x 254x 6889x 6889x 6889x 6889x 6899x 6895x 4412x 4412x 6895x 6895x 6895x 6895x 6895x 6895x 6895x 6895x 6895x 6895x 6895x 2483x 2483x 2483x 2483x 2483x 2483x 2467x 2483x 16x 16x 2483x 6895x 6889x 6889x 6889x 7307x 41x 41x 321x 321x 41x 41x 475x 475x 475x 475x 475x 475x 475x 475x 475x 475x 41x 41x 559x 559x 41x 41x 238x 238x 238x 41x 41x 2486x 2486x 2486x 2486x 2486x 2486x 2486x 2486x 2486x 2486x 2486x 41x 41x 321x 83x 83x 83x 238x 238x 317x 317x 164x 164x 321x 41x 41x 83x 83x 83x 83x 83x 41x 41x 238x 238x 238x 238x 41x 642x 642x 642x 41x 83x 83x 83x 83x 83x 83x 83x 41x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 238x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 83x 83x 83x 41x 41x 41x 41x | // Copyright (c) ZeroC, Inc.
import { TCPConnectionInfo } from "./Connection.js";
import { SocketOperation } from "./SocketOperation.js";
import { Timer } from "./Timer.js";
import {
ConnectionLostException,
ConnectionRefusedException,
ConnectFailedException,
InitializationException,
SocketException,
} from "./LocalExceptions.js";
import net from "net";
const StateNeedConnect = 0;
const StateConnectPending = 1;
const StateConnected = 2;
class TcpTransceiver {
constructor(instance, addr, sourceAddr) {
if (addr == null || addr.host === undefined || addr.port === undefined) {
throw new InitializationException("invalid address provided to TcpTransceiver constructor");
}
this._logger = instance.logger();
this._readBuffers = [];
this._readPosition = 0;
this._fd = null;
this._addr = addr;
this._sourceAddr = sourceAddr;
this._desc = "local address = <not connected>\nremote address = " + addressToString(this._addr);
this._state = StateNeedConnect;
this._registered = false;
this._exception = null;
}
setCallbacks(connectedCallback, bytesAvailableCallback, bytesWrittenCallback) {
this._connectedCallback = connectedCallback;
this._bytesAvailableCallback = bytesAvailableCallback;
this._bytesWrittenCallback = bytesWrittenCallback;
}
//
// Returns SocketOperation.None when initialization is complete.
//
initialize() {
if (this._exception) {
throw this._exception;
}
if (this._state === StateNeedConnect) {
this._state = StateConnectPending;
try {
this._fd = net.createConnection({
port: this._addr.port,
host: this._addr.host,
localAddress: this._sourceAddr,
});
} catch (err) {
this._exception = translateError(this._state, err, addressToString(this._addr));
throw this._exception;
}
this._fd.on("connect", () => this.socketConnected());
this._fd.on("data", buf => this.socketBytesAvailable(buf));
// The error callback can be triggered from the socket write(). We don't want it to be dispatched right
// away from within the write() so we delay the call with setImmediate. We do the same for close as a
// precaution. See also issue #6226.
this._fd.on("close", hadError => Timer.setImmediate(() => this.socketClosed(hadError)));
this._fd.on("error", err => Timer.setImmediate(() => this.socketError(err)));
return SocketOperation.Connect; // Waiting for connect to complete.
} else if (this._state === StateConnectPending) {
this._desc = fdToString(this._fd, this._addr);
this._state = StateConnected;
}
console.assert(this._state === StateConnected);
return SocketOperation.None;
}
register() {
this._registered = true;
this._fd.resume();
if (this._exception) {
this._bytesAvailableCallback();
}
}
unregister() {
if (this._fd === null) {
console.assert(this._exception); // Socket creation failed.
return;
}
this._registered = false;
this._fd.pause();
}
close() {
if (this._fd === null) {
// Socket creation failed or not yet initialized, the later can happen if the connection creation throws
// before calling transceiver initialize.
return;
}
this._fd.end();
}
destroy() {
if (this._fd === null) {
// Socket creation failed or not yet initialized, the later can happen if the connection creation throws
// before calling transceiver initialize.
return;
}
this._fd.destroy();
this._fd = null;
}
/**
* Write the given byte buffer to the socket.
*
* @param byteBuffer the byte buffer to write.
* @returns Whether or not the write operation completed synchronously.
*/
write(byteBuffer) {
if (this._exception) {
throw this._exception;
}
console.assert(byteBuffer.remaining > 0);
const slice = byteBuffer.b.slice(byteBuffer.position, byteBuffer.position + byteBuffer.remaining);
let sync = true;
sync = this._fd.write(Buffer.from(slice), null, () => {
if (!sync) {
this._bytesWrittenCallback();
}
});
byteBuffer.position += byteBuffer.remaining;
return sync;
}
read(byteBuffer, moreData) {
if (this._exception) {
throw this._exception;
}
moreData.value = false;
if (this._readBuffers.length === 0) {
return false; // No data available.
}
let avail = this._readBuffers[0].length - this._readPosition;
console.assert(avail > 0);
while (byteBuffer.remaining > 0) {
if (avail > byteBuffer.remaining) {
avail = byteBuffer.remaining;
}
this._readBuffers[0].copy(
Buffer.from(byteBuffer.b),
byteBuffer.position,
this._readPosition,
this._readPosition + avail,
);
byteBuffer.position += avail;
this._readPosition += avail;
if (this._readPosition === this._readBuffers[0].length) {
//
// We've exhausted the current read buffer.
//
this._readPosition = 0;
this._readBuffers.shift();
if (this._readBuffers.length === 0) {
break; // No more data - we're done.
} else {
avail = this._readBuffers[0].length;
}
}
}
moreData.value = this._readBuffers.length > 0;
return byteBuffer.remaining === 0;
}
type() {
return "tcp";
}
getInfo(adapterName, connectionId) {
console.assert(this._fd !== null);
return new TCPConnectionInfo(
adapterName,
connectionId,
this._fd.localAddress,
this._fd.localPort,
this._fd.remoteAddress,
this._fd.remotePort,
);
}
toString() {
return this._desc;
}
socketConnected() {
console.assert(this._connectedCallback !== null);
this._connectedCallback();
}
socketBytesAvailable(buf) {
console.assert(this._bytesAvailableCallback !== null);
//
// TODO: Should we set a limit on how much data we can read?
// We can call _fd.pause() to temporarily stop reading.
//
if (buf.length > 0) {
this._readBuffers.push(buf);
this._bytesAvailableCallback();
}
}
socketClosed(hadError) {
if (hadError) {
console.assert(this._exception !== null && this._exception !== undefined);
return; // Already handled by the error callback.
}
this._exception = new ConnectionLostException(addressToString(this._addr));
if (this._state < StateConnected) {
this._connectedCallback();
} else if (this._registered) {
this._bytesAvailableCallback();
}
}
socketError(err) {
this._exception = translateError(this._state, err, addressToString(this._addr));
if (this._state < StateConnected) {
this._connectedCallback();
} else if (this._registered) {
this._bytesAvailableCallback();
}
}
}
function fdToString(fd, targetAddr) {
if (fd === null) {
return "<closed>";
}
return addressesToString(fd.localAddress, fd.localPort, fd.remoteAddress, fd.remotePort, targetAddr);
}
function addressToString(addr) {
return `${addr.host}:${addr.port}`;
}
function translateError(state, err, address) {
if (state < StateConnected) {
if (connectionRefused(err.code)) {
return new ConnectionRefusedException(address, { cause: err });
} else if (connectionFailed(err.code)) {
return new ConnectFailedException(address, { cause: err });
}
} else if (connectionLost(err.code)) {
return new ConnectionLostException(address, { cause: err });
}
return new SocketException(`Socket error on ${address}.`, { cause: err });
}
function addressesToString(localHost, localPort, remoteHost, remotePort, targetAddr) {
remoteHost = remoteHost === undefined ? null : remoteHost;
targetAddr = targetAddr === undefined ? null : targetAddr;
const s = [];
s.push("local address = ");
s.push(localHost + ":" + localPort);
if (remoteHost === null && targetAddr !== null) {
remoteHost = targetAddr.host;
remotePort = targetAddr.port;
}
if (remoteHost === null) {
s.push("\nremote address = <not connected>");
} else {
s.push("\nremote address = ");
s.push(remoteHost + ":" + remotePort);
}
return s.join("");
}
const ECONNABORTED = "ECONNABORTED";
const ECONNREFUSED = "ECONNREFUSED";
const ECONNRESET = "ECONNRESET";
const EHOSTUNREACH = "EHOSTUNREACH";
const ENETUNREACH = "ENETUNREACH";
const ENOTCONN = "ENOTCONN";
const EPIPE = "EPIPE";
const ESHUTDOWN = "ESHUTDOWN";
const ETIMEDOUT = "ETIMEDOUT";
function connectionRefused(err) {
return err == ECONNREFUSED;
}
function connectionFailed(err) {
return (
err == ECONNREFUSED ||
err == ETIMEDOUT ||
err == ENETUNREACH ||
err == EHOSTUNREACH ||
err == ECONNRESET ||
err == ESHUTDOWN ||
err == ECONNABORTED
);
}
function connectionLost(err) {
return err == ECONNRESET || err == ENOTCONN || err == ESHUTDOWN || err == ECONNABORTED || err == EPIPE;
}
export { TcpTransceiver };
|