From 45f08adff3c69a258c5a48024af3e5a5ca8ee374 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 12 Oct 2024 12:32:38 +0300 Subject: [PATCH] Change tcpclient API --- examples/ping-pong/pp-client/main.go | 10 ++++++---- tcpclient/client.go | 14 +++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/examples/ping-pong/pp-client/main.go b/examples/ping-pong/pp-client/main.go index e2f11ad..9ae0285 100644 --- a/examples/ping-pong/pp-client/main.go +++ b/examples/ping-pong/pp-client/main.go @@ -1,14 +1,14 @@ package main import ( + "fmt" "time" "git.qowevisa.me/Qowevisa/tcpmachine/tcpclient" ) func main() { - conf := tcpclient.GetDefaultConfig() - client := tcpclient.CreateClient(conf) + client := tcpclient.CreateClient("127.0.0.1:10000") go func() { for { if client.IsConnected { @@ -21,6 +21,8 @@ func main() { time.Sleep(time.Second) } }() - go client.ErrorResolver(client.ErrorsChannel) - client.StartClient("127.0.0.1:10000") + err := client.StartClient() + if err != nil { + fmt.Printf("ERROR: StartClient: %v\n", err) + } } diff --git a/tcpclient/client.go b/tcpclient/client.go index 98996da..3d8defd 100644 --- a/tcpclient/client.go +++ b/tcpclient/client.go @@ -23,6 +23,7 @@ func GetDefaultConfig() ClientConfiguration { } type Client struct { + addr string exit chan bool Server net.Conn IsConnected bool @@ -32,8 +33,15 @@ type Client struct { ErrorResolver func(chan error) } -func CreateClient(conf ClientConfiguration) *Client { +func CreateClient(addr string, options ...ClientOption) *Client { + conf := GetDefaultConfig() + + for _, opt := range options { + opt(conf) + } + return &Client{ + addr: addr, Messages: make(chan []byte, 16), ErrorResolver: conf.ErrorResolver, ErrorsChannel: make(chan error, 8), @@ -41,8 +49,8 @@ func CreateClient(conf ClientConfiguration) *Client { } } -func (c *Client) StartClient(addr string) error { - server, err := net.Dial("tcp", addr) +func (c *Client) StartClient() error { + server, err := net.Dial("tcp", c.addr) if err != nil { return fmt.Errorf("net.Dial: %w", err) }