Skip to content

Commit

Permalink
Added package comment for wsconnadapter package
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielcoderX committed Sep 4, 2023
1 parent 8153880 commit 64a2815
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions wsconnadapter/wsconnadapter.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package wsconnadapter provides an adapter for representing WebSocket connections as net.Conn objects.
// It allows you to use WebSocket connections as if they were standard network connections.
package wsconnadapter

import (
Expand All @@ -9,22 +11,23 @@ import (
"time"
)

// an adapter for representing WebSocket connection as a net.Conn
// some caveats apply: https://github.com/gorilla/websocket/issues/441

// Adapter represents an adapter for representing WebSocket connection as a net.Conn.
// Some caveats apply: https://github.com/gorilla/websocket/issues/441
type Adapter struct {
conn *websocket.Conn
readMutex sync.Mutex
writeMutex sync.Mutex
reader io.Reader
}

// New creates a new Adapter from a WebSocket connection.
func New(conn *websocket.Conn) *Adapter {
return &Adapter{
conn: conn,
}
}

// Read reads data from the WebSocket connection.
func (a *Adapter) Read(b []byte) (int, error) {
// Read() can be called concurrently, and we mutate some internal state here
a.readMutex.Lock()
Expand Down Expand Up @@ -57,6 +60,7 @@ func (a *Adapter) Read(b []byte) (int, error) {
return bytesRead, err
}

// Write writes data to the WebSocket connection.
func (a *Adapter) Write(b []byte) (int, error) {
a.writeMutex.Lock()
defer a.writeMutex.Unlock()
Expand All @@ -72,18 +76,22 @@ func (a *Adapter) Write(b []byte) (int, error) {
return bytesWritten, err
}

// Close closes the WebSocket connection.
func (a *Adapter) Close() error {
return a.conn.Close()
}

// LocalAddr returns the local network address.
func (a *Adapter) LocalAddr() net.Addr {
return a.conn.LocalAddr()
}

// RemoteAddr returns the remote network address.
func (a *Adapter) RemoteAddr() net.Addr {
return a.conn.RemoteAddr()
}

// SetDeadline sets the read and write deadlines for the connection.
func (a *Adapter) SetDeadline(t time.Time) error {
if err := a.SetReadDeadline(t); err != nil {
return err
Expand All @@ -92,10 +100,12 @@ func (a *Adapter) SetDeadline(t time.Time) error {
return a.SetWriteDeadline(t)
}

// SetReadDeadline sets the read deadline for the connection.
func (a *Adapter) SetReadDeadline(t time.Time) error {
return a.conn.SetReadDeadline(t)
}

// SetWriteDeadline sets the write deadline for the connection.
func (a *Adapter) SetWriteDeadline(t time.Time) error {
return a.conn.SetWriteDeadline(t)
}

0 comments on commit 64a2815

Please sign in to comment.