conn.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Package oscarclient connects in-process protocol servers, such as TOC, to
  2. // the OSCAR server as ordinary OSCAR clients.
  3. package oscarclient
  4. import (
  5. "context"
  6. "errors"
  7. "log/slog"
  8. "net"
  9. "sync"
  10. "github.com/mk6i/open-oscar-server/wire"
  11. )
  12. // ErrSignedOff indicates that the OSCAR server refused or ended a connection
  13. // with a signoff frame.
  14. var ErrSignedOff = errors.New("signed off by OSCAR server")
  15. // queueLimit is how many received SNACs a connection holds for its consumer,
  16. // the same as an OSCAR session's message queue.
  17. const queueLimit = 1000
  18. // FLAPConn is a client's FLAP connection to an OSCAR service, either BOS or
  19. // a chat room.
  20. type FLAPConn struct {
  21. conn net.Conn
  22. flapc *wire.FlapClient
  23. logger *slog.Logger
  24. // msgs holds SNACs received from the server until the consumer takes
  25. // them. Run never waits on it, so it keeps reading from the server even
  26. // while the consumer is busy; otherwise the server's writes would block,
  27. // and with them its reads of the SNACs the client sends.
  28. msgs chan wire.SNACMessage
  29. closed chan struct{}
  30. closeOnce sync.Once
  31. }
  32. // newFLAPConn wraps a FLAP connection that has finished signing on to an
  33. // OSCAR service.
  34. func newFLAPConn(conn net.Conn, flapc *wire.FlapClient, logger *slog.Logger) *FLAPConn {
  35. return &FLAPConn{
  36. conn: conn,
  37. flapc: flapc,
  38. logger: logger,
  39. msgs: make(chan wire.SNACMessage, queueLimit),
  40. closed: make(chan struct{}),
  41. }
  42. }
  43. // Run reads SNACs from the server into Messages until the connection closes
  44. // from either end or ctx is done, then closes the connection. The consumer
  45. // runs it on a goroutine of its own, from the moment the connection is
  46. // returned: until Run is reading, the server's writes to the connection
  47. // block, and with them its reads of the SNACs the client sends.
  48. func (c *FLAPConn) Run(ctx context.Context) {
  49. stop := context.AfterFunc(ctx, c.Close)
  50. defer stop()
  51. defer c.Close()
  52. for {
  53. frame, err := c.flapc.ReceiveFLAP()
  54. if err != nil {
  55. return
  56. }
  57. switch frame.FrameType {
  58. case wire.FLAPFrameData:
  59. msg, err := decodeSNAC(frame.Payload)
  60. if err != nil {
  61. c.logger.Debug("unable to decode SNAC from OSCAR server", "err", err.Error())
  62. continue
  63. }
  64. select {
  65. case c.msgs <- msg:
  66. default:
  67. // like the OSCAR server, disconnect a client that falls too far behind
  68. c.logger.Warn("client isn't keeping up with the OSCAR server, disconnecting")
  69. return
  70. }
  71. case wire.FLAPFrameSignoff:
  72. return
  73. }
  74. }
  75. }
  76. // SendSNAC sends a SNAC to the OSCAR server.
  77. func (c *FLAPConn) SendSNAC(frame wire.SNACFrame, body any) error {
  78. return c.flapc.SendSNAC(frame, body)
  79. }
  80. // Messages returns the SNACs received from the server in arrival order.
  81. func (c *FLAPConn) Messages() <-chan wire.SNACMessage {
  82. return c.msgs
  83. }
  84. // Closed returns a channel that is closed when the connection closes.
  85. func (c *FLAPConn) Closed() <-chan struct{} {
  86. return c.closed
  87. }
  88. // Close closes the connection. It is safe to call more than once.
  89. func (c *FLAPConn) Close() {
  90. c.closeOnce.Do(func() {
  91. close(c.closed)
  92. _ = c.conn.Close()
  93. })
  94. }