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
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

registry.go 779B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package hub
  2. import (
  3. "context"
  4. "sync"
  5. "github.com/9seconds/mtg/conntypes"
  6. )
  7. type registry struct {
  8. conns map[string]*ctxChannel
  9. ctx context.Context
  10. mutex sync.RWMutex
  11. }
  12. func (r *registry) Register(id conntypes.ConnID) ChannelReadCloser {
  13. channel := newCtxChannel(r.ctx)
  14. r.mutex.Lock()
  15. r.conns[string(id[:])] = channel
  16. r.mutex.Unlock()
  17. return channel
  18. }
  19. func (r *registry) Unregister(id conntypes.ConnID) {
  20. r.mutex.Lock()
  21. defer r.mutex.Unlock()
  22. if channel, ok := r.conns[string(id[:])]; ok {
  23. channel.Close()
  24. delete(r.conns, string(id[:]))
  25. }
  26. }
  27. func (r *registry) getChannel(id conntypes.ConnID) (*ctxChannel, bool) {
  28. r.mutex.RLock()
  29. defer r.mutex.RUnlock()
  30. if value, ok := r.conns[string(id[:])]; ok {
  31. return value, true
  32. }
  33. return nil, false
  34. }