| // |
| 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 msg |
| 16 |
| 17 | import ( |
| 18 | "context" |
| 19 | "io" |
| 20 | "net" |
| 21 | "reflect" |
| 22 |
| 23 | "github.com/fatedier/frp/pkg/proto/wire" |
| 24 | ) |
| 25 |
| 26 | type ReadWriter interface { |
| 27 | ReadMsg() (Message, error) |
| 28 | ReadMsgInto(Message) error |
| 29 | WriteMsg(Message) error |
| 30 | } |
| 31 |
| 32 | type Conn struct { |
| 33 | net.Conn |
| 34 | rw ReadWriter |
| 35 | } |
| 36 |
| 37 | func NewConn(conn net.Conn, rw ReadWriter) *Conn { |
| 38 | return &Conn{ |
| 39 | Conn: conn, |
| 40 | rw: rw, |
| 41 | } |
| 42 | } |
| 43 |
| 44 | func (c *Conn) ReadMsg() (Message, error) { |
| 45 | return c.rw.ReadMsg() |
| 46 | } |
| 47 |
| 48 | func (c *Conn) ReadMsgInto(m Message) error { |
| 49 | return c.rw.ReadMsgInto(m) |
| 50 | } |
| 51 |
| 52 | func (c *Conn) WriteMsg(m Message) error { |
| 53 | return c.rw.WriteMsg(m) |
| 54 | } |
| 55 |
| 56 | func (c *Conn) Context() context.Context { |
| 57 | if getter, ok := c.Conn.(interface{ Context() context.Context }); ok { |
| 58 | return getter.Context() |
| 59 | } |
| 60 | return context.Background() |
| 61 | } |
| 62 |
| 63 | func (c *Conn) WithContext(ctx context.Context) { |
| 64 | if setter, ok := c.Conn.(interface{ WithContext(context.Context) }); ok { |
| 65 | setter.WithContext(ctx) |
| 66 | } |
| 67 | } |
| 68 |
| 69 | type V1ReadWriter struct { |
| 70 | rw io.ReadWriter |
| 71 | } |
| 72 |
| 73 | func NewV1ReadWriter(rw io.ReadWriter) ReadWriter { |
| 74 | return &V1ReadWriter{rw: rw} |
| 75 | } |
| 76 |
| 77 | // NewReadWriter wraps rw with the message codec for the selected wire protocol. |
| 78 | // An empty protocol keeps the historical v1 behavior for tests and older call sites. |
| 79 | func NewReadWriter(rw io.ReadWriter, wireProtocol string) ReadWriter { |
| 80 | switch wireProtocol { |
| 81 | case wire.ProtocolV2: |
| 82 | return NewV2ReadWriter(rw) |
| 83 | case "", wire.ProtocolV1: |
| 84 | return NewV1ReadWriter(rw) |
| 85 | default: |
| 86 | return NewV1ReadWriter(rw) |
| 87 | } |
| 88 | } |
| 89 |
| 90 | func (rw *V1ReadWriter) ReadMsg() (Message, error) { |
| 91 | return ReadMsg(rw.rw) |
| 92 | } |
| 93 |
| 94 | func (rw *V1ReadWriter) ReadMsgInto(m Message) error { |
| 95 | return ReadMsgInto(rw.rw, m) |
| 96 | } |
| 97 |
| 98 | func (rw *V1ReadWriter) WriteMsg(m Message) error { |
| 99 | return WriteMsg(rw.rw, m) |
| 100 | } |
| 101 |
| 102 | func AsyncHandler(f func(Message)) func(Message) { |
| 103 | return func(m Message) { |
| 104 | go f(m) |
| 105 | } |
| 106 | } |
| 107 |
| 108 | // Dispatcher is used to send messages to net.Conn or register handlers for messages read from net.Conn. |
| 109 | type Dispatcher struct { |
| 110 | rw ReadWriter |
| 111 |
| 112 | sendCh chan Message |
| 113 | doneCh chan struct{} |
| 114 | msgHandlers map[reflect.Type]func(Message) |
| 115 | } |
| 116 |
| 117 | func NewDispatcher(rw ReadWriter) *Dispatcher { |
| 118 | return &Dispatcher{ |
| 119 | rw: rw, |
| 120 | sendCh: make(chan Message, 100), |
| 121 | doneCh: make(chan struct{}), |
| 122 | msgHandlers: make(map[reflect.Type]func(Message)), |
| 123 | } |
| 124 | } |
| 125 |
| 126 | // Run will block until io.EOF or some error occurs. |
| 127 | func (d *Dispatcher) Run() { |
| 128 | go d.sendLoop() |
| 129 | go d.readLoop() |
| 130 | } |
| 131 |
| 132 | func (d *Dispatcher) sendLoop() { |
| 133 | for { |
| 134 | select { |
| 135 | case <-d.doneCh: |
| 136 | return |
| 137 | case m := <-d.sendCh: |
| 138 | _ = d.rw.WriteMsg(m) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 |
| 143 | func (d *Dispatcher) readLoop() { |
| 144 | for { |
| 145 | m, err := d.rw.ReadMsg() |
| 146 | if err != nil { |
| 147 | close(d.doneCh) |
| 148 | return |
| 149 | } |
| 150 |
| 151 | if handler, ok := d.msgHandlers[reflect.TypeOf(m)]; ok { |
| 152 | handler(m) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 |
| 157 | func (d *Dispatcher) Send(m Message) error { |
| 158 | select { |
| 159 | case <-d.doneCh: |
| 160 | return io.EOF |
| 161 | case d.sendCh <- m: |
| 162 | return nil |
| 163 | } |
| 164 | } |
| 165 |
| 166 | func (d *Dispatcher) RegisterHandler(msg Message, handler func(Message)) { |
| 167 | d.msgHandlers[reflect.TypeOf(msg)] = handler |
| 168 | } |
| 169 |
| 170 | func (d *Dispatcher) Done() chan struct{} { |
| 171 | return d.doneCh |
| 172 | } |