Skip to content

Commit a509d8f

Browse files
committed
Use of app.Ctx as default context for background go light-threads like
WHIP/HTTP/...
1 parent 5e6b1e8 commit a509d8f

6 files changed

Lines changed: 28 additions & 18 deletions

File tree

src/bindings/app.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,15 @@ func (a *App) Startup(ctx context.Context) {
5858
// Perform your setup here
5959

6060
go func() {
61-
62-
if err := oninit.Execute(a.assets); err != nil {
61+
if err := oninit.Execute(a.ctx, a.assets); err != nil {
6362
time.Sleep(time.Second * 5) // Add some time to load the UI
6463
wailsRuntime.EventsEmit(ctx, "ERROR", err.Error())
6564
log.Println(err)
6665
}
67-
6866
}()
6967

7068
go func () {
71-
if err := streaming_signal.InitWhipServer(streaming_signal.WhipConfig); err != nil {
69+
if err := streaming_signal.InitWhipServer(a.ctx, streaming_signal.WhipConfig); err != nil {
7270
time.Sleep(time.Second * 5) // Add some time to load the UI
7371
wailsRuntime.EventsEmit(ctx, "ERROR", err.Error())
7472
log.Println(err)

src/net/http/http.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
package http
33

44
import (
5+
"context"
56
"embed"
67
"errors"
78
"fmt"
89
"io/fs"
10+
"net"
911
"net/http"
1012
"strings"
1113

1214
"github.com/PiterWeb/RemoteController/src/cli"
1315
)
1416

15-
func InitHTTPAssets(serverMux *http.ServeMux, assets embed.FS) error {
17+
func InitHTTPAssets(ctx context.Context,serverMux *http.ServeMux, assets embed.FS) error {
1618

1719
config := cli.GetConfig()
1820
clientPort := config.GetHTTPPort()
@@ -36,6 +38,9 @@ func InitHTTPAssets(serverMux *http.ServeMux, assets embed.FS) error {
3638
httpServer := &http.Server{
3739
Handler: serverMux,
3840
Addr: addr,
41+
BaseContext: func(l net.Listener) context.Context {
42+
return ctx
43+
},
3944
}
4045

4146
err = httpServer.ListenAndServe()

src/net/webrtc/streaming_signal/whip.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"log"
10+
"net"
1011
"net/http"
1112
"sync/atomic"
1213
"time"
@@ -34,7 +35,7 @@ var WhipConfig = &whipConfig{
3435
ICEServers: atomic.Pointer[[]webrtc.ICEServer]{},
3536
}
3637

37-
func InitWhipServer(config *whipConfig) error {
38+
func InitWhipServer(ctx context.Context, config *whipConfig) error {
3839

3940
var answerChan <-chan string = config.AnswerChan
4041
var offerChan chan<- string = config.OfferChan
@@ -141,6 +142,9 @@ func InitWhipServer(config *whipConfig) error {
141142
Addr: fmt.Sprintf("127.0.0.1:%d", config.Port),
142143
WriteTimeout: time.Second * 30,
143144
ReadTimeout: time.Second * 30,
145+
BaseContext: func(l net.Listener) context.Context {
146+
return ctx
147+
},
144148
}
145149

146150
err := httpServer.ListenAndServe()

src/oninit/oninit_darwin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package oninit
22

33
import (
4+
"context"
45
"embed"
56
"log"
6-
7+
78
LRPSignals "github.com/PiterWeb/LibreRemotePlaySignals/v1"
89
"github.com/PiterWeb/RemoteController/src/cli"
9-
1010
)
1111

12-
func Execute(assets embed.FS) error {
12+
func Execute(ctx context.Context, assets embed.FS) error {
1313

1414
ips_channel := make(chan []string, 1)
1515
defer close(ips_channel)
1616

1717
options := LRPSignals.ServerOptions{
1818
Port: cli.GetConfig().GetEasyConnectPort(),
1919
}
20-
20+
2121
log.Printf("Easy Connect Server started on port %d\n", options.Port)
22-
22+
2323
err := LRPSignals.InitServer(options, ips_channel)
2424

2525
return err

src/oninit/oninit_linux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package oninit
22

33
import (
4+
"context"
45
"embed"
56
"errors"
67
"log"
@@ -12,7 +13,7 @@ import (
1213
"github.com/PiterWeb/RemoteController/src/net/websocket"
1314
)
1415

15-
func Execute(assets embed.FS) error {
16+
func Execute(ctx context.Context, assets embed.FS) error {
1617

1718
httpServerMux := http.NewServeMux()
1819

@@ -22,7 +23,7 @@ func Execute(assets embed.FS) error {
2223
errChan := make(chan error, 2)
2324

2425
go func() {
25-
err := net_http.InitHTTPAssets(httpServerMux, assets)
26+
err := net_http.InitHTTPAssets(ctx, httpServerMux, assets)
2627

2728
if err != nil {
2829
errChan <- err

src/oninit/oninit_windows.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package oninit
22

33
import (
4+
"context"
45
"embed"
56
"log"
67
"net/http"
8+
79
LRPSignals "github.com/PiterWeb/LibreRemotePlaySignals/v1"
8-
net_http "github.com/PiterWeb/RemoteController/src/net/http"
910
"github.com/PiterWeb/RemoteController/src/cli"
1011
"github.com/PiterWeb/RemoteController/src/devices/gamepad"
12+
net_http "github.com/PiterWeb/RemoteController/src/net/http"
1113
)
1214

13-
func Execute(assets embed.FS) error {
15+
func Execute(ctx context.Context, assets embed.FS) error {
1416
err := gamepad.InitViGEm()
1517

1618
if err != nil {
@@ -23,19 +25,19 @@ func Execute(assets embed.FS) error {
2325
errChan := make(chan error, 2)
2426

2527
go func() {
26-
err := net_http.InitHTTPAssets(httpServerMux, assets)
28+
err := net_http.InitHTTPAssets(ctx, httpServerMux, assets)
2729

2830
if err != nil {
2931
errChan <- err
3032
}
3133
}()
32-
34+
3335
go func() {
3436

3537
options := LRPSignals.ServerOptions{
3638
Port: cli.GetConfig().GetEasyConnectPort(),
3739
}
38-
40+
3941
log.Printf("Easy Connect Server started on port %d\n", options.Port)
4042
err := LRPSignals.InitServer(options, ips_channel)
4143
if err != nil {

0 commit comments

Comments
 (0)