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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ctxrwc.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package wrappers
  2. import (
  3. "context"
  4. "io"
  5. "github.com/juju/errors"
  6. )
  7. // CtxReadWriteCloser wraps underlying connection and does management of the
  8. // context and its cancel function.
  9. type CtxReadWriteCloser struct {
  10. ctx context.Context
  11. conn io.ReadWriteCloser
  12. cancel context.CancelFunc
  13. }
  14. // Read reads from connection
  15. func (c *CtxReadWriteCloser) Read(p []byte) (int, error) {
  16. select {
  17. case <-c.ctx.Done():
  18. return 0, errors.Annotate(c.ctx.Err(), "Read is failed because of closed context")
  19. default:
  20. n, err := c.conn.Read(p)
  21. if err != nil {
  22. c.cancel()
  23. }
  24. return n, err
  25. }
  26. }
  27. // Write writes into connection.
  28. func (c *CtxReadWriteCloser) Write(p []byte) (int, error) {
  29. select {
  30. case <-c.ctx.Done():
  31. return 0, errors.Annotate(c.ctx.Err(), "Write is failed because of closed context")
  32. default:
  33. n, err := c.conn.Write(p)
  34. if err != nil {
  35. c.cancel()
  36. }
  37. return n, err
  38. }
  39. }
  40. // Close closes underlying connection.
  41. func (c *CtxReadWriteCloser) Close() error {
  42. return c.conn.Close()
  43. }
  44. // NewCtxRWC returns ReadWriteCloser which respects given context,
  45. // cancellation etc.
  46. func NewCtxRWC(ctx context.Context, cancel context.CancelFunc, conn io.ReadWriteCloser) io.ReadWriteCloser {
  47. return &CtxReadWriteCloser{
  48. conn: conn,
  49. ctx: ctx,
  50. cancel: cancel,
  51. }
  52. }