From 69388c956e2ffd6d941eaf47f1ec03eada94d527 Mon Sep 17 00:00:00 2001 From: zhshch2002 Date: Tue, 24 May 2022 10:45:54 +0800 Subject: [PATCH] remove: listener_channe, signal, taskgroup --- listener_channel/listener-channel.go | 61 ---------------------------- signal/signal.go | 19 --------- taskgroup/taskgroup.go | 45 -------------------- trace/tcp_ipv4.go | 6 +-- trace/tcp_ipv6.go | 6 +-- 5 files changed, 2 insertions(+), 135 deletions(-) delete mode 100644 listener_channel/listener-channel.go delete mode 100644 signal/signal.go delete mode 100644 taskgroup/taskgroup.go diff --git a/listener_channel/listener-channel.go b/listener_channel/listener-channel.go deleted file mode 100644 index e6ef8c1..0000000 --- a/listener_channel/listener-channel.go +++ /dev/null @@ -1,61 +0,0 @@ -package listener_channel - -import ( - "golang.org/x/net/context" - "net" - "time" -) - -type ReceivedMessage struct { - N *int - Peer net.Addr - Msg []byte - Err error -} - -type ListenerChannel struct { - ctx context.Context - cancel context.CancelFunc - Conn net.PacketConn - Messages chan ReceivedMessage -} - -func New(conn net.PacketConn) *ListenerChannel { - ctx, cancel := context.WithCancel(context.Background()) - results := make(chan ReceivedMessage, 50) - - return &ListenerChannel{Conn: conn, ctx: ctx, cancel: cancel, Messages: results} -} - -func (l *ListenerChannel) Start() { - for { - select { - case <-l.ctx.Done(): - return - default: - } - - reply := make([]byte, 1500) - err := l.Conn.SetReadDeadline(time.Now().Add(2 * time.Second)) - if err != nil { - l.Messages <- ReceivedMessage{Err: err} - continue - } - - n, peer, err := l.Conn.ReadFrom(reply) - if err != nil { - l.Messages <- ReceivedMessage{Err: err} - continue - } - l.Messages <- ReceivedMessage{ - N: &n, - Peer: peer, - Err: nil, - Msg: reply, - } - } -} - -func (l *ListenerChannel) Stop() { - l.cancel() -} diff --git a/signal/signal.go b/signal/signal.go deleted file mode 100644 index 6095a25..0000000 --- a/signal/signal.go +++ /dev/null @@ -1,19 +0,0 @@ -package signal - -type Signal struct { - sigChan chan struct{} -} - -func New() *Signal { - return &Signal{sigChan: make(chan struct{}, 1)} -} - -func (s *Signal) Signal() { - if len(s.sigChan) == 0 { - s.sigChan <- struct{}{} - } -} - -func (s *Signal) Chan() chan struct{} { - return s.sigChan -} diff --git a/taskgroup/taskgroup.go b/taskgroup/taskgroup.go deleted file mode 100644 index ca2e472..0000000 --- a/taskgroup/taskgroup.go +++ /dev/null @@ -1,45 +0,0 @@ -package taskgroup - -import ( - "sync" -) - -type TaskGroup struct { - count int - mu sync.Mutex - done []chan struct{} -} - -func New() *TaskGroup { - return &TaskGroup{ - count: 0, - mu: sync.Mutex{}, - done: []chan struct{}{}, - } -} - -func (t *TaskGroup) Add() { - t.mu.Lock() - defer t.mu.Unlock() - t.count++ -} - -func (t *TaskGroup) Done() { - t.mu.Lock() - defer t.mu.Unlock() - if t.count-1 == 0 { - for _, doneChannel := range t.done { - doneChannel <- struct{}{} - } - t.done = []chan struct{}{} - } - t.count-- -} - -func (t *TaskGroup) Wait() { - doneChannel := make(chan struct{}) - t.mu.Lock() - t.done = append(t.done, doneChannel) - t.mu.Unlock() - <-doneChannel -} diff --git a/trace/tcp_ipv4.go b/trace/tcp_ipv4.go index 0188b01..be7ced1 100644 --- a/trace/tcp_ipv4.go +++ b/trace/tcp_ipv4.go @@ -10,7 +10,6 @@ import ( "github.com/google/gopacket" "github.com/google/gopacket/layers" - "github.com/xgadget-lab/nexttrace/listener_channel" "github.com/xgadget-lab/nexttrace/util" "golang.org/x/net/context" "golang.org/x/net/icmp" @@ -110,10 +109,7 @@ func (t *TCPTracer) listenICMP() { // @title listenTCP // @description 监听TCP的响应数据包 func (t *TCPTracer) listenTCP() { - lc := listener_channel.New(t.tcp) - - defer lc.Stop() - + lc := NewPacketListener(t.tcp, t.ctx) go lc.Start() for { diff --git a/trace/tcp_ipv6.go b/trace/tcp_ipv6.go index 252cbee..b05c8d3 100644 --- a/trace/tcp_ipv6.go +++ b/trace/tcp_ipv6.go @@ -10,7 +10,6 @@ import ( "github.com/google/gopacket" "github.com/google/gopacket/layers" - "github.com/xgadget-lab/nexttrace/listener_channel" "github.com/xgadget-lab/nexttrace/util" "golang.org/x/net/context" "golang.org/x/net/icmp" @@ -111,10 +110,7 @@ func (t *TCPTracerv6) listenICMP() { // @title listenTCP // @description 监听TCP的响应数据包 func (t *TCPTracerv6) listenTCP() { - lc := listener_channel.New(t.tcp) - - defer lc.Stop() - + lc := NewPacketListener(t.tcp, t.ctx) go lc.Start() for {