Skip to content

Commit

Permalink
#17 add additional code to the sender and reader
Browse files Browse the repository at this point in the history
  • Loading branch information
thegodenage committed Apr 22, 2024
1 parent afbe4f8 commit ceed722
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions internal/proxy/tcp.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package proxy

import (
"context"
"fmt"
"io"
"net"
)

type Sender interface {
Accept() error
Accept(rw io.ReadWriter) error
}

// TCPReceiver is responsible for the reading
Expand Down Expand Up @@ -41,6 +43,9 @@ func (r *TCPReceiver) Run() error {
continue
}

if err := r.sender.Accept(conn); err != nil {
fmt.Printf("failed to accept connection with sender: %s", err.Error())
}
}

}
Expand All @@ -54,22 +59,45 @@ type TCPSender struct {
// TCP connection to desired location
pipes map[string]any
remoteAddr string

bytesChan chan []byte
}

func NewTCPSender(remoteAddr string) *TCPSender {
var _ Sender = (*TCPSender)(nil)

func NewTCPSender(listenAddr, remoteAddr string) *TCPSender {
return &TCPSender{
pipes: make(map[string]any),
remoteAddr: remoteAddr,
bytesChan: make(chan []byte),
}
}

func (s *TCPSender) Run() error {
rAddr, err := net.ResolveTCPAddr("tcp", r.remoteAddr)
func (s *TCPSender) Start(ctx context.Context) error {
rAddr, err := net.ResolveTCPAddr("tcp", s.remoteAddr)
if err != nil {
return fmt.Errorf("resolving remote tcp addr: '%s': %w", r.remoteAddr, err)
return fmt.Errorf("resolving remote tcp addr: '%s': %w", s.remoteAddr, err)
}

go s.startRemoteSender(ctx, rAddr)

return nil
}

type senderConstruct struct {
locationIPAddr *net.IPAddr
func (s *TCPSender) Accept(rw io.ReadWriter) error {
bytes, err := io.ReadAll(rw)
if err != nil {
return fmt.Errorf("read all bytes from read writer: %w", err)
}

s.bytesChan <- bytes

return nil
}

func (s *TCPSender) startRemoteSender(ctx context.Context, remoteAddr *net.TCPAddr) {
net.DialTCP("tcp")
for bytes := range s.bytesChan {

}
}

0 comments on commit ceed722

Please sign in to comment.