minor session optimisations

This commit is contained in:
wish
2024-01-11 23:03:53 +11:00
parent 2685476022
commit af29ee637e

View File

@@ -86,13 +86,11 @@ func NewSession(server *Server, conn net.Conn) *Session {
// Start starts the session packet send and recv loop(s). // Start starts the session packet send and recv loop(s).
func (s *Session) Start() { func (s *Session) Start() {
go func() { s.logger.Debug("New connection", zap.String("RemoteAddr", s.rawConn.RemoteAddr().String()))
s.logger.Debug("New connection", zap.String("RemoteAddr", s.rawConn.RemoteAddr().String())) // Unlike the sign and entrance server,
// Unlike the sign and entrance server, // the client DOES NOT initalize the channel connection with 8 NULL bytes.
// the client DOES NOT initalize the channel connection with 8 NULL bytes. go s.sendLoop()
go s.sendLoop() go s.recvLoop()
s.recvLoop()
}()
} }
// QueueSend queues a packet (raw []byte) to be sent. // QueueSend queues a packet (raw []byte) to be sent.
@@ -150,16 +148,19 @@ func (s *Session) QueueAck(ackHandle uint32, data []byte) {
} }
func (s *Session) sendLoop() { func (s *Session) sendLoop() {
var pkt packet
for { for {
if s.closed { if s.closed {
return return
} }
pkt := <-s.sendPackets for len(s.sendPackets) > 0 {
err := s.cryptConn.SendPacket(append(pkt.data, []byte{0x00, 0x10}...)) pkt = <-s.sendPackets
if err != nil { err := s.cryptConn.SendPacket(append(pkt.data, []byte{0x00, 0x10}...))
s.logger.Warn("Failed to send packet") if err != nil {
s.logger.Warn("Failed to send packet")
}
} }
time.Sleep(10 * time.Millisecond) time.Sleep(5 * time.Millisecond)
} }
} }
@@ -178,14 +179,13 @@ func (s *Session) recvLoop() {
s.logger.Info(fmt.Sprintf("[%s] Disconnected", s.Name)) s.logger.Info(fmt.Sprintf("[%s] Disconnected", s.Name))
logoutPlayer(s) logoutPlayer(s)
return return
} } else if err != nil {
if err != nil {
s.logger.Warn("Error on ReadPacket, exiting recv loop", zap.Error(err)) s.logger.Warn("Error on ReadPacket, exiting recv loop", zap.Error(err))
logoutPlayer(s) logoutPlayer(s)
return return
} }
s.handlePacketGroup(pkt) s.handlePacketGroup(pkt)
time.Sleep(10 * time.Millisecond) time.Sleep(5 * time.Millisecond)
} }
} }