| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package transport |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "reflect" |
| 20 | "sync" |
| 21 |
| 22 | "github.com/fatedier/golib/errors" |
| 23 |
| 24 | "github.com/fatedier/frp/pkg/msg" |
| 25 | ) |
| 26 |
| 27 | type MessageTransporter interface { |
| 28 | Send(msg.Message) error |
| 29 | // Recv(ctx context.Context, laneKey string, msgType string) (Message, error) |
| 30 | // Do will first send msg, then recv msg with the same laneKey and specified msgType. |
| 31 | Do(ctx context.Context, req msg.Message, laneKey, recvMsgType string) (msg.Message, error) |
| 32 | // Dispatch will dispatch message to related channel registered in Do function by its message type and laneKey. |
| 33 | Dispatch(m msg.Message, laneKey string) bool |
| 34 | // Same with Dispatch but with specified message type. |
| 35 | DispatchWithType(m msg.Message, msgType, laneKey string) bool |
| 36 | } |
| 37 |
| 38 | type MessageSender interface { |
| 39 | Send(msg.Message) error |
| 40 | } |
| 41 |
| 42 | func NewMessageTransporter(sender MessageSender) MessageTransporter { |
| 43 | return &transporterImpl{ |
| 44 | sender: sender, |
| 45 | registry: make(map[string]map[string]chan msg.Message), |
| 46 | } |
| 47 | } |
| 48 |
| 49 | type transporterImpl struct { |
| 50 | sender MessageSender |
| 51 |
| 52 | // First key is message type and second key is lane key. |
| 53 | // Dispatch will dispatch message to related channel by its message type |
| 54 | // and lane key. |
| 55 | registry map[string]map[string]chan msg.Message |
| 56 | mu sync.RWMutex |
| 57 | } |
| 58 |
| 59 | func (impl *transporterImpl) Send(m msg.Message) error { |
| 60 | return impl.sender.Send(m) |
| 61 | } |
| 62 |
| 63 | func (impl *transporterImpl) Do(ctx context.Context, req msg.Message, laneKey, recvMsgType string) (msg.Message, error) { |
| 64 | ch := make(chan msg.Message, 1) |
| 65 | defer close(ch) |
| 66 | unregisterFn := impl.registerMsgChan(ch, laneKey, recvMsgType) |
| 67 | defer unregisterFn() |
| 68 |
| 69 | if err := impl.Send(req); err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 |
| 73 | select { |
| 74 | case <-ctx.Done(): |
| 75 | return nil, ctx.Err() |
| 76 | case resp := <-ch: |
| 77 | return resp, nil |
| 78 | } |
| 79 | } |
| 80 |
| 81 | func (impl *transporterImpl) DispatchWithType(m msg.Message, msgType, laneKey string) bool { |
| 82 | var ch chan msg.Message |
| 83 | impl.mu.RLock() |
| 84 | byLaneKey, ok := impl.registry[msgType] |
| 85 | if ok { |
| 86 | ch = byLaneKey[laneKey] |
| 87 | } |
| 88 | impl.mu.RUnlock() |
| 89 |
| 90 | if ch == nil { |
| 91 | return false |
| 92 | } |
| 93 |
| 94 | if err := errors.PanicToError(func() { |
| 95 | ch <- m |
| 96 | }); err != nil { |
| 97 | return false |
| 98 | } |
| 99 | return true |
| 100 | } |
| 101 |
| 102 | func (impl *transporterImpl) Dispatch(m msg.Message, laneKey string) bool { |
| 103 | msgType := reflect.TypeOf(m).Elem().Name() |
| 104 | return impl.DispatchWithType(m, msgType, laneKey) |
| 105 | } |
| 106 |
| 107 | func (impl *transporterImpl) registerMsgChan(recvCh chan msg.Message, laneKey string, msgType string) (unregister func()) { |
| 108 | impl.mu.Lock() |
| 109 | byLaneKey, ok := impl.registry[msgType] |
| 110 | if !ok { |
| 111 | byLaneKey = make(map[string]chan msg.Message) |
| 112 | impl.registry[msgType] = byLaneKey |
| 113 | } |
| 114 | byLaneKey[laneKey] = recvCh |
| 115 | impl.mu.Unlock() |
| 116 |
| 117 | unregister = func() { |
| 118 | impl.mu.Lock() |
| 119 | delete(byLaneKey, laneKey) |
| 120 | impl.mu.Unlock() |
| 121 | } |
| 122 | return |
| 123 | } |