MCPcopy Create free account
hub / github.com/Marus/cortex-debug / getAnyFreePort

Function getAnyFreePort

src/common.ts:497–521  ·  view source on GitHub ↗
(preferred: number)

Source from the content-addressed store, hash-verified

495}
496
497export function getAnyFreePort(preferred: number): Promise<number> {
498 return new Promise(async (resolve, reject) => {
499 function findFreePorts() {
500 const portFinderOpts = { min: 60000, max: 62000, retrieve: 1, consecutive: false };
501 TcpPortScanner.findFreePorts(portFinderOpts, GDBServer.LOCALHOST).then((ports) => {
502 resolve(ports[0]);
503 }).catch((e) => {
504 reject(e);
505 });
506 }
507
508 if (preferred > 0) {
509 TcpPortScanner.isPortInUseEx(preferred, GDBServer.LOCALHOST, TcpPortScanner.AvoidPorts).then((inuse) => {
510 if (!inuse) {
511 TcpPortScanner.EmitAllocated([preferred]);
512 resolve(preferred);
513 } else {
514 findFreePorts();
515 }
516 });
517 } else {
518 findFreePorts();
519 }
520 });
521}
522
523export function parseHexOrDecInt(str: string): number {
524 return str.startsWith('0x') ? parseInt(str.substring(2), 16) : parseInt(str, 10);

Callers 1

startServerMethod · 0.90

Calls 3

findFreePortsFunction · 0.85
EmitAllocatedMethod · 0.80
isPortInUseExMethod · 0.45

Tested by

no test coverage detected