From 534a7e962ad6258df277f1f8214f9124975ebcae Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Thu, 21 Sep 2023 15:46:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20client.Run=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=BC=A0=E5=85=A5=20block=20=E5=8F=82=E6=95=B0=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E5=AE=A2=E6=88=B7=E7=AB=AF=E4=BB=A5=E9=98=BB=E5=A1=9E?= =?UTF-8?q?=E7=9A=84=E6=A8=A1=E5=BC=8F=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/client/client.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/server/client/client.go b/server/client/client.go index 5b341a6..8a83afa 100644 --- a/server/client/client.go +++ b/server/client/client.go @@ -32,17 +32,21 @@ type Client struct { closed bool // 是否已关闭 pool *concurrent.Pool[*Packet] // 数据包缓冲池 loop *writeloop.WriteLoop[*Packet] // 写入循环 + block chan struct{} // 以阻塞方式运行 } -// Run 运行客户端 -// - 当客户端已运行时,会先关闭客户端再重新运行 -func (slf *Client) Run() error { +// Run 运行客户端,当客户端已运行时,会先关闭客户端再重新运行 +// - block 以阻塞方式运行 +func (slf *Client) Run(block ...bool) error { slf.mutex.Lock() if !slf.closed { slf.mutex.Unlock() slf.Close() slf.mutex.Lock() } + if len(block) > 0 && block[0] { + slf.block = make(chan struct{}) + } var runState = make(chan error) go func(runState chan<- error) { defer func() { @@ -77,6 +81,9 @@ func (slf *Client) Run() error { slf.mutex.Unlock() slf.OnConnectionOpenedEvent(slf) + if slf.block != nil { + <-slf.block + } return nil } @@ -100,6 +107,9 @@ func (slf *Client) Close(err ...error) { } else { slf.OnConnectionClosedEvent(slf, nil) } + if slf.block != nil { + slf.block <- struct{}{} + } } // WriteWS 向连接中写入指定 websocket 数据类型