Skip to content

Commit a572858

Browse files
committed
Added a read deadline to the server when not in debug mode which will detect client closure and remove the copied dll file
1 parent ddf1384 commit a572858

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

discon-server/websocket.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"path/filepath"
1717
"sync"
1818
"sync/atomic"
19+
"time"
1920
"unsafe"
2021

2122
"github.com/gorilla/websocket"
@@ -36,7 +37,7 @@ func ServeWs(w http.ResponseWriter, r *http.Request, debug bool) {
3637
// Get unique identifier for this connection
3738
connID := connectionID.Add(1)
3839

39-
// Read library path and function name from post parameters
40+
// Read controller path and function name from post parameters
4041
params, err := url.ParseQuery(r.URL.RawQuery)
4142
if err != nil {
4243
http.Error(w, "Error parsing url parameters: "+err.Error(), http.StatusInternalServerError)
@@ -46,25 +47,29 @@ func ServeWs(w http.ResponseWriter, r *http.Request, debug bool) {
4647
proc := params.Get("proc")
4748

4849
if debug {
49-
log.Printf("Received request to load function '%s' from shared library '%s'\n", proc, path)
50+
log.Printf("Received request to load function '%s' from shared controller '%s'\n", proc, path)
5051
}
5152

52-
// Check if library exists at path
53+
// Check if controller exists at path
5354
_, err = os.Stat(path)
5455
if os.IsNotExist(err) {
55-
http.Error(w, "Library not found at '"+path+"'", http.StatusInternalServerError)
56+
http.Error(w, "Controller not found at '"+path+"'", http.StatusInternalServerError)
5657
return
5758
}
5859

5960
// Create a copy of the shared library with a number suffix so multiple instances
6061
// of the same library can be shared at the same time
6162
tmpPath, err := duplicateLibrary(path, connID)
6263
if err != nil {
63-
http.Error(w, "Error duplicating library: "+err.Error(), http.StatusInternalServerError)
64+
http.Error(w, "Error duplicating controller: "+err.Error(), http.StatusInternalServerError)
6465
return
6566
}
6667
defer os.Remove(tmpPath)
6768

69+
if debug {
70+
log.Printf("Duplicated controller to '%s'\n", tmpPath)
71+
}
72+
6873
// Load the shared library
6974
libraryPath := C.CString(tmpPath)
7075
defer C.free(unsafe.Pointer(libraryPath))
@@ -97,6 +102,14 @@ func ServeWs(w http.ResponseWriter, r *http.Request, debug bool) {
97102
// Loop while receiving messages over socket
98103
for {
99104

105+
// If not debug, set read deadline to 5 seconds
106+
// This will disconnect the client if no message is received in 5 seconds
107+
// which allows the controller to be unloaded and deleted
108+
if !debug {
109+
ws.SetReadDeadline(time.Now().Add(time.Second * 5))
110+
}
111+
112+
// Read message from websocket
100113
messageType, b, err := ws.ReadMessage()
101114
if err != nil {
102115
log.Println("read:", err)

0 commit comments

Comments
 (0)