Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ctx_channel.go 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package hub
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "github.com/9seconds/mtg/conntypes"
  7. )
  8. const closeableChannelReadTimeout = 2 * time.Minute
  9. type ChannelReadCloser interface {
  10. Read() (conntypes.Packet, error)
  11. Close() error
  12. }
  13. type ctxChannel struct {
  14. channel chan conntypes.Packet
  15. ctx context.Context
  16. cancel context.CancelFunc
  17. }
  18. func (c *ctxChannel) Read() (conntypes.Packet, error) {
  19. timer := time.NewTimer(closeableChannelReadTimeout)
  20. defer timer.Stop()
  21. select {
  22. case <-timer.C:
  23. return nil, ErrTimeout
  24. case <-c.ctx.Done():
  25. return nil, ErrClosed
  26. case packet := <-c.channel:
  27. return packet, nil
  28. }
  29. }
  30. func (c *ctxChannel) write(packet conntypes.Packet) error {
  31. select {
  32. case <-c.ctx.Done():
  33. return ErrClosed
  34. case c.channel <- packet:
  35. return nil
  36. }
  37. }
  38. func (c *ctxChannel) Close() error {
  39. c.cancel()
  40. c.channel = nil
  41. return nil
  42. }
  43. func newCtxChannel(ctx context.Context) *ctxChannel {
  44. ctx, cancel := context.WithCancel(ctx)
  45. return &ctxChannel{
  46. channel: make(chan conntypes.Packet),
  47. ctx: ctx,
  48. cancel: cancel,
  49. }
  50. }