Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
改个文字
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Aug 18, 2024
1 parent 8ef36e5 commit 508a85c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
33 changes: 18 additions & 15 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
)

const HEADER_SIZE = 8
const CLIENT_BUFFER_SIZE = 1024

type Client struct {
Expand All @@ -20,9 +21,9 @@ type Client struct {
conn net.Conn
requests map[uint16]any

inHeader [8]byte
outHeader [8]byte
buffer [CLIENT_BUFFER_SIZE]byte
inHeader [HEADER_SIZE]byte
outHeader [HEADER_SIZE]byte
//buffer [CLIENT_BUFFER_SIZE]byte
}

func (c *Client) Write(pack *Pack) error {
Expand All @@ -42,29 +43,32 @@ func (c *Client) Request(request any) (response any, err error) {
}

Check failure on line 43 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

missing return

func (c *Client) receive() {
buf := make([]byte, 8)
//buf := make([]byte, 8)
buf := make([]byte, CLIENT_BUFFER_SIZE)
for {
//_ = c.conn.SetReadDeadline(time.Time{})
//n, err := c.conn.Read(buf)
n, err := io.ReadAtLeast(c.conn, buf, 8)
n, err := io.ReadAtLeast(c.conn, c.inHeader[:], HEADER_SIZE)
if err != nil {
break
}
if n < 8 {
if n < HEADER_SIZE {
break
}

if bytes.Compare(buf[:3], []byte(MAGIC)) != 0 {
if bytes.Compare(c.inHeader[:3], []byte(MAGIC)) != 0 {
break
}

l := int(binary.BigEndian.Uint16(buf[6:])) //长度
code := buf[3] >> 4

Check failure on line 62 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

code declared and not used
encoding := buf[3] & 0xf

Check failure on line 63 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

encoding declared and not used

l := int(binary.BigEndian.Uint16(c.inHeader[6:])) //长度
if l > 0 {
var b []byte
if l > CLIENT_BUFFER_SIZE {
b = make([]byte, l)
} else {
b = c.buffer[:] //使用默认buffer
b = buf
}

//_ = c.conn.SetReadDeadline(time.Now().Add(time.Second * 30))
Expand All @@ -73,13 +77,13 @@ func (c *Client) receive() {
break
}
if n != l {
//长度不够,费包
//长度不够,废包
break
}
}

tp := buf[3] >> 4
enc := buf[3] & 0xf
parse

Check failure on line 84 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

undefined: parse

}

var pack Pack
pack.Decode(b)

Check failure on line 89 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

undefined: b
Expand All @@ -88,7 +92,6 @@ func (c *Client) receive() {

//关闭连接
_ = c.conn.Close()

}

func (c *Client) Open() error {
Expand Down
11 changes: 11 additions & 0 deletions rpc/device.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package rpc

const (
DEVICE_LIST uint8 = iota
DEVICE_CREATE
DEVICE_UPDATE
DEVICE_DELETE
DEVICE_READ
DEVICE_WRITE
DEVICE_EVENT
DEVICE_ACTION
)

type DeviceId struct {
Id string `json:"id"`
}
Expand Down
1 change: 1 addition & 0 deletions rpc/incoming.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rpc
14 changes: 14 additions & 0 deletions rpc/mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package rpc

const (
MOD_GATEWAY uint8 = iota
MOD_PRODUCT
MOD_DEVICE
MOD_OTA
MOD_SERIAL
MOD_TCP
MOD_GPIO
MOD_FS
MOD_CAMERA
MOD_GPS
)
1 change: 1 addition & 0 deletions rpc/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rpc

0 comments on commit 508a85c

Please sign in to comment.