This commit is contained in:
unknown 2019-10-14 18:54:25 +08:00
parent 3904f0c797
commit 45d94b4406
3 changed files with 57 additions and 3 deletions

View File

@ -28,6 +28,7 @@ var (
CLIENT_CONNECTION_NOT_EXIST = errors.New("the client connection is not exist")
BRIDGE_NOT_EXIST = errors.New("the client connection is not exist")
REQUEST_EOF = errors.New("the request has finished")
CLIENT_ID_NOT_EXIST = errors.New("the request has finished")
)
// Plugin interface, all plugins must implement those functions.

View File

@ -1,6 +1,12 @@
package core
import "io"
import (
"bytes"
"encoding/binary"
"encoding/json"
"io"
"net"
)
func CopyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
buf := CopyBuff.Get()
@ -28,3 +34,38 @@ func CopyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
}
return written, err
}
func SendInfo(conn net.Conn, t interface{}) (int, error) {
/*
The task info is formed as follows:
+----+-----+---------+
|type| len | content |
+----+---------------+
| 4 | 4 | ... |
+----+---------------+
*/
raw := bytes.NewBuffer([]byte{})
b, err := json.Marshal(t)
if err != nil {
return 0, err
}
lenBytes, err := GetLenBytes(b)
if err != nil {
return 0, err
}
binary.Write(raw, binary.LittleEndian, lenBytes)
return conn.Write(raw.Bytes())
}
// get the assembled amount data(len 4 and content)
func GetLenBytes(buf []byte) (b []byte, err error) {
raw := bytes.NewBuffer([]byte{})
if err = binary.Write(raw, binary.LittleEndian, int32(len(buf))); err != nil {
return
}
if err = binary.Write(raw, binary.LittleEndian, buf); err != nil {
return
}
b = raw.Bytes()
return
}

View File

@ -35,10 +35,22 @@ func (proxy *Proxy) Run(ctx context.Context, config map[string]string) error {
return core.BRIDGE_NOT_EXIST
}
clientCtxConn := ctx.Value(core.CLIENT_CONNECTION)
if clientCtxConn == nil {
return core.CLIENT_CONNECTION_NOT_EXIST
}
clientId := ctx.Value(core.CLIENT_ID)
if clientId == nil {
return core.CLIENT_ID_NOT_EXIST
}
brg := bg.(*bridge.Bridge)
brg.GetConnByClientId()
go core.CopyBuffer()
severConn, err := brg.GetConnByClientId(clientId.(int))
if err != nil {
return err
}
go core.CopyBuffer(severConn, clientCtxConn.(net.Conn))
core.CopyBuffer(clientCtxConn.(net.Conn), severConn)
return nil
}