Skip to content

Commit

Permalink
feat: support send request
Browse files Browse the repository at this point in the history
  • Loading branch information
chenqinghe committed Oct 11, 2022
1 parent 273d274 commit 0bc5882
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sockit

import (
"context"
"errors"
"github.com/sirupsen/logrus"
"net"
Expand All @@ -23,6 +24,9 @@ type Session struct {

lastPackTs time.Time // 最后一个收到包的时间

reqLock *sync.RWMutex
requests map[int64]chan Packet

manuallyClosed bool
closed chan struct{}
}
Expand All @@ -38,6 +42,8 @@ func NewSession(c Conn, mgr ConnManager, user User, handler Handler) *Session {
handler: handler,
dataLock: &sync.RWMutex{},
data: make(map[string]interface{}),
reqLock: &sync.RWMutex{},
requests: make(map[int64]chan Packet),
closed: make(chan struct{}),
}

Expand Down Expand Up @@ -67,7 +73,16 @@ func (s *Session) readPacket() {

s.lastPackTs = time.Now()

go s.handler.Handle(packet, s)
s.reqLock.RLock()
ch, ok := s.requests[packet.Id()]
s.reqLock.RUnlock()
if ok {
ch <- packet
close(ch)
continue
} else {
go s.handler.Handle(packet, s)
}
}
}

Expand Down Expand Up @@ -116,6 +131,37 @@ func (s *Session) SendPacket(p Packet) error {
return s.c.SendPacket(p)
}

func (s *Session) SendRequest(p Packet) (<-chan Packet, error) {
ch := make(chan Packet, 1)
s.reqLock.Lock()
s.requests[p.Id()] = ch

if err := s.SendPacket(p); err != nil {
return nil, err
}

return ch, nil
}

func (s *Session) SendRequestTimeout(p Packet, timeout time.Duration) (Packet, error) {
ch := make(chan Packet, 1)
s.reqLock.Lock()
s.requests[p.Id()] = ch

if err := s.SendPacket(p); err != nil {
return nil, err
}

timer := time.NewTimer(timeout)
select {
case p := <-ch:
timer.Stop()
return p, nil
case <-timer.C:
return nil, context.DeadlineExceeded
}
}

func (s *Session) LocalAddr() net.Addr {
return s.c.LocalAddr()
}
Expand Down

0 comments on commit 0bc5882

Please sign in to comment.