mirror of
https://github.com/ehang-io/nps.git
synced 2025-07-03 13:10:42 +00:00
10/14
This commit is contained in:
parent
3904f0c797
commit
45d94b4406
@ -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.
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user