mirror of
https://github.com/unitoftime/rtcnet.git
synced 2025-09-26 20:31:17 +08:00
Cleanup logging
This commit is contained in:
14
conn.go
14
conn.go
@@ -1,14 +1,14 @@
|
||||
package rtcnet
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"time"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/pion/datachannel"
|
||||
"github.com/pion/webrtc/v3"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
@@ -27,7 +27,7 @@ func newConn(peer *webrtc.PeerConnection) *Conn {
|
||||
peerConn: peer,
|
||||
errorChan: make(chan error, 16), //TODO! - Sizing
|
||||
}
|
||||
trace("conn: new: ", c)
|
||||
trace("conn: new: ")
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func (c *Conn) Write(b []byte) (int, error) {
|
||||
func (c *Conn) Close() error {
|
||||
var closeErr error
|
||||
c.closeOnce.Do(func() {
|
||||
trace("conn: closing: ", c)
|
||||
trace("conn: closing: ")
|
||||
c.closed.Store(true)
|
||||
|
||||
err1 := c.dataChannel.Close()
|
||||
@@ -67,7 +67,9 @@ func (c *Conn) Close() error {
|
||||
|
||||
if err1 != nil || err2 != nil || err3 != nil {
|
||||
closeErr = errors.Join(errors.New("failed to close: (datachannel, peerconn, raw)"), err1, err2, err3)
|
||||
logErr("conn: closing error: ", closeErr)
|
||||
logger.Error().
|
||||
Err(closeErr).
|
||||
Msg("Closing rtc connection")
|
||||
}
|
||||
})
|
||||
return closeErr
|
||||
|
59
dial.go
59
dial.go
@@ -1,11 +1,11 @@
|
||||
package rtcnet
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pion/webrtc/v3"
|
||||
)
|
||||
@@ -28,14 +28,15 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
// },
|
||||
},
|
||||
}
|
||||
|
||||
trace("Dial: Starting WebRTC negotiation")
|
||||
|
||||
api := getSettingsEngineApi()
|
||||
|
||||
peerConnection, err := api.NewPeerConnection(config)
|
||||
if err != nil {
|
||||
logErr("Dial: NewPeerConnection", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: NewPeerConnection")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -59,7 +60,9 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
}
|
||||
err := sendMsg(wSock, sigMsg)
|
||||
if err != nil {
|
||||
logErr("Dial: Receive Peer OnIceCandidate", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: Receive Peer OnIceCandidate")
|
||||
conn.pushErrorData(err)
|
||||
return
|
||||
}
|
||||
@@ -93,7 +96,9 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
|
||||
err := peerConnection.SetRemoteDescription(sdp)
|
||||
if err != nil {
|
||||
logErr("Dial: SetRemoteDescription", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: SetRemoteDescription")
|
||||
conn.pushErrorData(err)
|
||||
return
|
||||
}
|
||||
@@ -108,7 +113,9 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
}
|
||||
err := sendMsg(wSock, sigMsg)
|
||||
if err != nil {
|
||||
logErr("Dial: Failed Websocket Send: Pending Candidate Msg", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: Failed Websocket Send: Pending Candidate Msg")
|
||||
conn.pushErrorData(err)
|
||||
return
|
||||
}
|
||||
@@ -118,13 +125,15 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
trace("Dial: RtcCandidateMsg")
|
||||
err := peerConnection.AddICECandidate(msg.Candidate.CandidateInit)
|
||||
if err != nil {
|
||||
logErr("Dial: AddIceCandidate", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: AddIceCandidate")
|
||||
conn.pushErrorData(err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// Warning: no valid message included
|
||||
trace("Dial: ws received unknown message ", string(buf[:n]))
|
||||
trace("Dial: ws received unknown message " + string(buf[:n]))
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -138,7 +147,9 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
}
|
||||
dataChannel, err := peerConnection.CreateDataChannel("data", &dataChannelOptions)
|
||||
if err != nil {
|
||||
logErr("Dial: CreateDataChannel", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: CreateDataChannel")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -146,7 +157,7 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
// Set the handler for Peer connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
|
||||
trace("Dial: Peer Connection State has changed: ", s.String())
|
||||
trace("Dial: Peer Connection State has changed: " + s.String())
|
||||
|
||||
// if s == webrtc.PeerConnectionStateClosed {
|
||||
// // This means the webrtc was closed by one side. Just close it on the other side
|
||||
@@ -154,7 +165,7 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
// }
|
||||
|
||||
if s == webrtc.PeerConnectionStateFailed {
|
||||
trace("Dial: PeerConnectionStateFailed", nil)
|
||||
trace("Dial: PeerConnectionStateFailed")
|
||||
|
||||
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
|
||||
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
|
||||
@@ -196,7 +207,9 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
// Create an offer to send to the other process
|
||||
offer, err := peerConnection.CreateOffer(nil)
|
||||
if err != nil {
|
||||
logErr("Dial: CreateOffer", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: CreateOffer")
|
||||
return nil, err
|
||||
}
|
||||
// fmt.Println("CreateOffer")
|
||||
@@ -205,7 +218,9 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
// Note: this will start the gathering of ICE candidates
|
||||
err = peerConnection.SetLocalDescription(offer)
|
||||
if err != nil {
|
||||
logErr("Dial: SetLocalDescription", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: SetLocalDescription")
|
||||
return nil, err
|
||||
}
|
||||
// fmt.Println("SetLocalDesc")
|
||||
@@ -215,14 +230,18 @@ func Dial(address string, tlsConfig *tls.Config, ordered bool) (*Conn, error) {
|
||||
}
|
||||
err = sendMsg(wSock, sigMsg)
|
||||
if err != nil {
|
||||
logErr("Dial: websocket.Send RtcSdp Offer", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: websocket.Send RtcSdp Offer")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Wait until the webrtc connection is finished getting setup
|
||||
select {
|
||||
case err := <-conn.errorChan:
|
||||
logErr("Dial: error exit", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Dial: error exit")
|
||||
return nil, err // There was an error in setup
|
||||
case <-connFinish:
|
||||
trace("Dial: normal exit")
|
||||
@@ -235,7 +254,9 @@ func sendMsg(conn net.Conn, msg signalMsg) error {
|
||||
// log.Print("sendMsg: ", msg)
|
||||
msgDat, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
logErr("sendMsg: Marshal", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("sendMsg: Marshal")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -243,7 +264,9 @@ func sendMsg(conn net.Conn, msg signalMsg) error {
|
||||
|
||||
_, err = conn.Write(msgDat)
|
||||
if err != nil {
|
||||
logErr("sendMsg: conn write", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("sendMsg: conn write")
|
||||
return err
|
||||
}
|
||||
|
||||
|
40
listener.go
40
listener.go
@@ -1,12 +1,12 @@
|
||||
package rtcnet
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/pion/webrtc/v3"
|
||||
)
|
||||
@@ -125,7 +125,7 @@ func (l *Listener) attemptWebRtcNegotiation(wsConn net.Conn) {
|
||||
// Set the handler for Peer connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
|
||||
trace("Listener: Peer Connection State has changed: ", s.String())
|
||||
trace("Listener: Peer Connection State has changed: " + s.String())
|
||||
|
||||
if s == webrtc.PeerConnectionStateClosed {
|
||||
// This means the webrtc was closed by one side. Just close it on the other side
|
||||
@@ -142,7 +142,9 @@ func (l *Listener) attemptWebRtcNegotiation(wsConn net.Conn) {
|
||||
// TODO - Do some cancellation
|
||||
err := peerConnection.Close()
|
||||
if err != nil {
|
||||
logErr("PeerConnectionStateFailed: ", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("PeerConnectionStateFailed: ")
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -188,7 +190,9 @@ func (l *Listener) attemptWebRtcNegotiation(wsConn net.Conn) {
|
||||
if err != nil {
|
||||
// TODO: Are there any cases where we might get an error here but its not fatal?
|
||||
// Assume the websocket is closed and break
|
||||
logErr("error reading websocket", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("error reading websocket")
|
||||
break
|
||||
}
|
||||
|
||||
@@ -209,7 +213,9 @@ func (l *Listener) attemptWebRtcNegotiation(wsConn net.Conn) {
|
||||
|
||||
err := peerConnection.SetRemoteDescription(sdp)
|
||||
if err != nil {
|
||||
logErr("Listener: SetRemoteDescription", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Listener: SetRemoteDescription")
|
||||
l.pendingAcceptErrors <- fmt.Errorf("RtcSdpMsg Recv - Failed to set remote description: %w", err)
|
||||
return
|
||||
}
|
||||
@@ -217,7 +223,9 @@ func (l *Listener) attemptWebRtcNegotiation(wsConn net.Conn) {
|
||||
// Create an answer to send to the other process
|
||||
answer, err := peerConnection.CreateAnswer(nil)
|
||||
if err != nil {
|
||||
logErr("Listener: CreateAnswer", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Listener: CreateAnswer")
|
||||
l.pendingAcceptErrors <- fmt.Errorf("RtcSdpMsg Recv - Failed to create answer: %w", err)
|
||||
return
|
||||
}
|
||||
@@ -227,7 +235,9 @@ func (l *Listener) attemptWebRtcNegotiation(wsConn net.Conn) {
|
||||
}
|
||||
err = sendMsg(wsConn, sigMsg)
|
||||
if err != nil {
|
||||
logErr("Listener: Websocket Send Answer", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Listener: Websocket Send Answer")
|
||||
l.pendingAcceptErrors <- fmt.Errorf("RtcSdpMsg Recv - Failed to send SDP answer: %w", err)
|
||||
return
|
||||
}
|
||||
@@ -235,7 +245,9 @@ func (l *Listener) attemptWebRtcNegotiation(wsConn net.Conn) {
|
||||
// Sets the LocalDescription, and starts our UDP listeners
|
||||
err = peerConnection.SetLocalDescription(answer)
|
||||
if err != nil {
|
||||
logErr("Listener: SetLocalDescription", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Listener: SetLocalDescription")
|
||||
l.pendingAcceptErrors <- fmt.Errorf("RtcSdpMsg Recv - Failed to set local SDP: %w", err)
|
||||
return
|
||||
}
|
||||
@@ -248,7 +260,9 @@ func (l *Listener) attemptWebRtcNegotiation(wsConn net.Conn) {
|
||||
}
|
||||
err := sendMsg(wsConn, sigMsg)
|
||||
if err != nil {
|
||||
logErr("Listener: Websocket Send Pending Candidate Message", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Listener: Websocket Send Pending Candidate Message")
|
||||
l.pendingAcceptErrors <- fmt.Errorf("RtcSdpMsg Recv - Failed to send RtcCandidate: %w", err)
|
||||
return
|
||||
}
|
||||
@@ -258,13 +272,15 @@ func (l *Listener) attemptWebRtcNegotiation(wsConn net.Conn) {
|
||||
// log.Debug().Msg("Listener: RtcCandidateMsg")
|
||||
err := peerConnection.AddICECandidate(msg.Candidate.CandidateInit)
|
||||
if err != nil {
|
||||
logErr("Listener: AddICECandidate", err)
|
||||
logger.Error().
|
||||
Err(err).
|
||||
Msg("Listener: AddICECandidate")
|
||||
l.pendingAcceptErrors <- fmt.Errorf("RtcCandidateMsg Recv - Failed to add candidate: %w", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// Warning: no valid message included
|
||||
trace("Listener: ws received unknown message: ", string(buf[:n]))
|
||||
trace("Listener: ws received unknown message: " + string(buf[:n]))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
18
log.go
Normal file
18
log.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package rtcnet
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// By default use the global logger
|
||||
var logger = log.Logger
|
||||
|
||||
func SetLogger(newLogger zerolog.Logger) {
|
||||
logger = newLogger
|
||||
}
|
||||
|
||||
// Old helper functions. Note I left this here so that I could easily just disable it. But I should probably remove this and use zerolog log levels
|
||||
func trace(msg string) {
|
||||
logger.Trace().Msg(msg)
|
||||
}
|
40
rtcnet.go
40
rtcnet.go
@@ -2,8 +2,6 @@ package rtcnet
|
||||
|
||||
import (
|
||||
"github.com/pion/webrtc/v3"
|
||||
|
||||
"log"
|
||||
)
|
||||
|
||||
// Notes: https://webrtcforthecurious.com/docs/01-what-why-and-how/
|
||||
@@ -39,41 +37,3 @@ type candidateMsg struct {
|
||||
CandidateInit webrtc.ICECandidateInit
|
||||
}
|
||||
|
||||
// Logger
|
||||
type Logger interface {
|
||||
Log(...any)
|
||||
LogErr(string, error)
|
||||
}
|
||||
|
||||
var DefaultLogger Logger
|
||||
|
||||
// func init() {
|
||||
// UseDefaultLogger()
|
||||
// }
|
||||
|
||||
// A basic default logger implementation
|
||||
type dLog struct {
|
||||
}
|
||||
func UseDefaultLogger() {
|
||||
DefaultLogger = dLog{}
|
||||
}
|
||||
func (l dLog) Log(msg ...any) {
|
||||
log.Println(msg...)
|
||||
}
|
||||
|
||||
func (l dLog) LogErr(msg string, err error) {
|
||||
log.Printf("%s: %s\n", msg, err)
|
||||
}
|
||||
|
||||
// Actual log functions
|
||||
func trace(msg ...any) {
|
||||
if DefaultLogger != nil {
|
||||
DefaultLogger.Log(msg...)
|
||||
}
|
||||
}
|
||||
|
||||
func logErr(msg string, err error) {
|
||||
if DefaultLogger != nil {
|
||||
DefaultLogger.LogErr(msg, err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user