server_session.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package quic
  2. import (
  3. "fmt"
  4. "github.com/lucas-clemente/quic-go/internal/protocol"
  5. "github.com/lucas-clemente/quic-go/internal/utils"
  6. )
  7. type serverSession struct {
  8. quicSession
  9. config *Config
  10. logger utils.Logger
  11. }
  12. var _ packetHandler = &serverSession{}
  13. func newServerSession(sess quicSession, config *Config, logger utils.Logger) packetHandler {
  14. return &serverSession{
  15. quicSession: sess,
  16. config: config,
  17. logger: logger,
  18. }
  19. }
  20. func (s *serverSession) handlePacket(p *receivedPacket) {
  21. if err := s.handlePacketImpl(p); err != nil {
  22. s.logger.Debugf("error handling packet from %s: %s", p.remoteAddr, err)
  23. }
  24. }
  25. func (s *serverSession) handlePacketImpl(p *receivedPacket) error {
  26. hdr := p.hdr
  27. // Probably an old packet that was sent by the client before the version was negotiated.
  28. // It is safe to drop it.
  29. if hdr.IsLongHeader && hdr.Version != s.quicSession.GetVersion() {
  30. return nil
  31. }
  32. if hdr.IsLongHeader {
  33. switch hdr.Type {
  34. case protocol.PacketTypeInitial, protocol.PacketTypeHandshake:
  35. // nothing to do here. Packet will be passed to the session.
  36. default:
  37. // Note that this also drops 0-RTT packets.
  38. return fmt.Errorf("Received unsupported packet type: %s", hdr.Type)
  39. }
  40. }
  41. s.quicSession.handlePacket(p)
  42. return nil
  43. }
  44. func (s *serverSession) GetPerspective() protocol.Perspective {
  45. return protocol.PerspectiveServer
  46. }