further generalizatio of synchronizing two streams of data.
This commit is contained in:
parent
650b40dfec
commit
ada8dee0aa
@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"cidebug/pkg/iowrappers"
|
"cidebug/pkg/iowrappers"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
@ -19,27 +18,7 @@ func handleConnection(tcpConn net.Conn, wsURL string) {
|
|||||||
}
|
}
|
||||||
defer wsConn.Close()
|
defer wsConn.Close()
|
||||||
|
|
||||||
waitChannel := make(chan bool)
|
iowrappers.SynchronizeStreams(wsConn, tcpConn)
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer func() {
|
|
||||||
waitChannel <- true
|
|
||||||
}()
|
|
||||||
_, err = io.Copy(tcpConn, wsConn)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("error %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer func() {
|
|
||||||
waitChannel <- true
|
|
||||||
}()
|
|
||||||
_, err = io.Copy(wsConn, tcpConn)
|
|
||||||
log.Printf("Error %v", err)
|
|
||||||
}()
|
|
||||||
|
|
||||||
<-waitChannel
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"cidebug/pkg/iowrappers"
|
"cidebug/pkg/iowrappers"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -42,29 +41,5 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request, tcpConn net.Conn) {
|
|||||||
}
|
}
|
||||||
defer wsConn.Close()
|
defer wsConn.Close()
|
||||||
|
|
||||||
waitChannel := make(chan bool)
|
iowrappers.SynchronizeStreams(tcpConn, wsConn)
|
||||||
|
|
||||||
// Read message from WebSocket
|
|
||||||
go func() {
|
|
||||||
defer func() {
|
|
||||||
waitChannel <- true
|
|
||||||
}()
|
|
||||||
_, err := io.Copy(tcpConn, wsConn)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error occurred %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer func() {
|
|
||||||
waitChannel <- true
|
|
||||||
}()
|
|
||||||
_, err := io.Copy(wsConn, tcpConn)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error occurred %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
<-waitChannel
|
|
||||||
log.Println("Connection closed")
|
|
||||||
}
|
}
|
||||||
|
31
pkg/iowrappers/sync.go
Normal file
31
pkg/iowrappers/sync.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package iowrappers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SynchronizeStreams(stream1, stream2 io.ReadWriter) {
|
||||||
|
waitChannel := make(chan bool)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
waitChannel <- true
|
||||||
|
}()
|
||||||
|
_, err := io.Copy(stream1, stream2)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
waitChannel <- true
|
||||||
|
}()
|
||||||
|
_, err := io.Copy(stream2, stream1)
|
||||||
|
log.Printf("Error %v", err)
|
||||||
|
}()
|
||||||
|
|
||||||
|
<-waitChannel
|
||||||
|
log.Println("Connection closed")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user