Skip to content

Commit

Permalink
Fix: add new signal func for udp support
Browse files Browse the repository at this point in the history
  • Loading branch information
Musixal committed Oct 13, 2024
1 parent e41897e commit abff19f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/server/transport/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *TcpTransport) udpListener(localAddr string, remoteAddr string) {
// seconds (or until the payload channel becomes idle). The timer will close the TCP connection once it
// times out. Further testing is needed to confirm this strategy's effect on overall performance and congestion handling.
}

delete(activeConnections, key)

} else {
Expand Down Expand Up @@ -147,7 +147,7 @@ func (s *TcpTransport) handleUDPLoop(udpChan chan *LocalUDPConn) {

case tunnelConn := <-s.tunnelChannel:
// Send the target addr over the connection
if err := utils.SendBinaryString(tunnelConn, localConn.remoteAddr, utils.SG_UDP); err != nil {
if err := utils.SendBinaryTransportString(tunnelConn, localConn.remoteAddr, utils.SG_UDP); err != nil {
s.logger.Errorf("%v", err)
tunnelConn.Close()
continue loop
Expand Down
79 changes: 77 additions & 2 deletions internal/utils/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,82 @@ import (
"github.com/quic-go/quic-go"
)

func SendBinaryString(conn interface{}, message string, transport byte) error {
func SendBinaryString(conn interface{}, message string) error {
// Header size
const headerSize = 2

// Create a buffer with the appropriate size for the message
buf := make([]byte, headerSize+len(message))

// Encode the length of the message as a big-endian 2-byte unsigned integer
binary.BigEndian.PutUint16(buf[:headerSize], uint16(len(message)))

// Copy the message into the buffer after the length
copy(buf[headerSize:], message)

switch c := conn.(type) {
case net.Conn:
// Send the buffer over the connection
if _, err := c.Write(buf); err != nil {
return fmt.Errorf("failed to send message: %w", err)
}
case quic.Stream:
if _, err := c.Write(buf); err != nil {
return fmt.Errorf("failed to send message: %w", err)
}
default:
// Handle unsupported connection types
return fmt.Errorf("unsupported connection type: %T", conn)
}
// Successful
return nil
}

func ReceiveBinaryString(conn interface{}) (string, error) {
// Header size
const headerSize = 2

// Create a buffer to read the first 2 bytes (the length of the message)
lenBuf := make([]byte, headerSize)

switch c := conn.(type) {
case net.Conn:
// Read exactly 2 bytes for the message length
if _, err := io.ReadFull(c, lenBuf); err != nil {
return "", fmt.Errorf("failed to read message length from net.Conn: %w", err)
}
case quic.Stream:
if _, err := io.ReadFull(c, lenBuf); err != nil {
return "", fmt.Errorf("failed to read message length from quic.Stream: %w", err)
}
default:
return "", fmt.Errorf("unsupported connection type: %T", conn)
}

// Decode the length of the message from the 2-byte buffer
messageLength := binary.BigEndian.Uint16(lenBuf[:2])

// Create a buffer of the appropriate size to hold the message
messageBuf := make([]byte, messageLength)

switch c := conn.(type) {
case net.Conn:
if _, err := io.ReadFull(c, messageBuf); err != nil {
return "", fmt.Errorf("failed to read message from net.Conn: %w", err)
}
case quic.Stream:
if _, err := io.ReadFull(c, messageBuf); err != nil {
return "", fmt.Errorf("failed to read message from quic.Stream: %w", err)
}
default:
return "", fmt.Errorf("unsupported connection type: %T", conn)
}

// Convert the message buffer to a string and return it
return string(messageBuf), nil
}

func SendBinaryTransportString(conn interface{}, message string, transport byte) error {
// Header size
const headerSize = 3

Expand Down Expand Up @@ -43,7 +118,7 @@ func SendBinaryString(conn interface{}, message string, transport byte) error {
return nil
}

func ReceiveBinaryString(conn interface{}) (string, byte, error) {
func ReceiveBinaryTransportString(conn interface{}) (string, byte, error) {
// Header size
const headerSize = 3

Expand Down

0 comments on commit abff19f

Please sign in to comment.