Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Changed detection of running processes. #8785

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/instanceserver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"@google-cloud/agones-sdk": "1.30.0",
"@swc/core": "1.3.41",
"cross-env": "7.0.3",
"detect-port": "^1.5.1",
"ffmpeg-static": "^5.1.0",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.21",
Expand Down
18 changes: 11 additions & 7 deletions packages/instanceserver/src/WebRTCFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ All portions of the code written by the Ethereal Engine team are Copyright © 20
Ethereal Engine. All Rights Reserved.
*/

import detect from 'detect-port'
import { exec } from 'child_process'
import { createWorker } from 'mediasoup'
import {
DataConsumer,
Expand Down Expand Up @@ -65,6 +65,7 @@ import {
MediasoupTransportObjectsState,
MediasoupTransportState
} from '@etherealengine/engine/src/networking/systems/MediasoupTransportState'
import { promisify } from 'util'
import { InstanceServerState } from './InstanceServerState'
import { MediasoupInternalWebRTCDataChannelState } from './MediasoupInternalWebRTCDataChannelState'
import { getUserIdFromPeerID } from './NetworkFunctions'
Expand All @@ -79,10 +80,11 @@ const logger = multiLogger.child({ component: 'instanceserver:webrtc' })

const portsInUse: number[] = []

const getNewOffset = async (ipAddress, startPort, i, offset) => {
const inUse = await detect(startPort + i + offset, ipAddress)
if (inUse !== startPort + i + offset || portsInUse.indexOf(startPort + i + offset) >= 0)
return getNewOffset(ipAddress, startPort, i, offset + 1)
const execAsync = promisify(exec)

const getNewOffset = async (ipAddress, startPort, i, offset, lsofResult) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function is now synchronous so should be specified as such

if (lsofResult.indexOf(`:${startPort + i + offset}`) > -1)
return getNewOffset(ipAddress, startPort, i, offset + 1, lsofResult)
else return offset
}

Expand All @@ -93,6 +95,7 @@ export async function startWebRTC() {
const workers = [] as Worker[]
//This is used in case ports in the range to use are in use by something else
let offset = 0
const lsofResult = await execAsync(`lsof -i -P -n | cat`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this work on windows and mac?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a very good point 🧐

for (let i = 0; i < cores.length; i++) {
const newWorker = await createWorker({
logLevel: 'debug',
Expand All @@ -105,10 +108,11 @@ export async function startWebRTC() {

const webRtcServerOptions = JSON.parse(JSON.stringify(localConfig.mediasoup.webRtcServerOptions))
offset = await getNewOffset(
webRtcServerOptions.listenInfos[0].ipAddress,
webRtcServerOptions.listenInfos[0].ip,
webRtcServerOptions.listenInfos[0].port,
i,
offset
offset,
lsofResult.stdout
)
for (const listenInfo of webRtcServerOptions.listenInfos) listenInfo.port += i + offset
portsInUse.push(webRtcServerOptions.listenInfos[0].port)
Expand Down