summaryrefslogtreecommitdiff
path: root/internal/server/session
diff options
context:
space:
mode:
Diffstat (limited to 'internal/server/session')
-rw-r--r--internal/server/session/session.go30
1 files changed, 26 insertions, 4 deletions
diff --git a/internal/server/session/session.go b/internal/server/session/session.go
index 2b7121a..88a19e7 100644
--- a/internal/server/session/session.go
+++ b/internal/server/session/session.go
@@ -25,6 +25,7 @@ type SessionManager interface {
type Session struct {
manager SessionManager
addr *net.TCPAddr
+ cancel context.CancelFunc
writeQueue chan packet.Packet
issuedTime time.Time
@@ -36,14 +37,21 @@ type Session struct {
mu sync.Mutex
}
-func NewSession(manager SessionManager, addr *net.TCPAddr, id snowflake.ID, pubKey ed25519.PublicKey) *Session {
+func NewSession(
+ manager SessionManager,
+ addr *net.TCPAddr, cancel context.CancelFunc,
+ id snowflake.ID, pubKey ed25519.PublicKey,
+) *Session {
session := &Session{
- writeQueue: make(chan packet.Packet, 10),
- PubKey: pubKey,
manager: manager,
addr: addr,
- id: id,
+ cancel: cancel,
+ writeQueue: make(chan packet.Packet, 10),
+ issuedTime: time.Time{},
challenge: make([]byte, 32),
+ PubKey: pubKey,
+ id: id,
+ mu: sync.Mutex{},
}
session.Challenge() // Make sure an initial nonce is generated
return session
@@ -89,3 +97,17 @@ func (s *Session) Read(ctx context.Context) (packet.Packet, bool) {
return packet.Packet{}, false
}
}
+
+func (s *Session) Close() {
+ timeout := 1 * time.Second
+ ctx, cancel := context.WithTimeout(context.Background(), timeout)
+ payload := &packet.Error{
+ Error: "new connection from another location, closing this one",
+ PktType: packet.PacketError,
+ }
+ pkt := packet.NewPacket(packet.NewMsgPackEncoder(payload))
+ s.Write(ctx, pkt)
+ cancel()
+
+ s.cancel()
+}