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 9, 2024
1 parent b6c0a56 commit e4fe77e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 2 deletions.
1 change: 0 additions & 1 deletion docs/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@

```json5
{
"id": "123123123123", //客户端ID,可选
"username": "user", //用户名
"password": "123456", //密码
}
Expand Down
120 changes: 120 additions & 0 deletions rpc/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package rpc

import (
"bytes"
"encoding/binary"
"errors"
"io"
"net"
"net/url"
)

type Client struct {
Url string

Encoding uint8 //JSON

id uint16
conn net.Conn
requests map[uint16]any
}

func (c *Client) Write(pack *Pack) error {
c.id++

c.conn.Write(pack.Header)

Check failure on line 25 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

pack.Header undefined (type *Pack has no field or method Header)
}

Check failure on line 26 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

missing return

func (c *Client) Write2(typ uint8, data any) error {
header := make([]byte, 8)

Check failure on line 29 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

header declared and not used

}

Check failure on line 31 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

missing return

func (c *Client) Request(request any) (response any, err error) {
header := make([]byte, 8)

Check failure on line 34 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

header declared and not used

}

Check failure on line 36 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

missing return

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

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

l := int(binary.BigEndian.Uint16(buf[6:])) //长度
if l > 0 {
b := make([]byte, l)
//_ = c.conn.SetReadDeadline(time.Now().Add(time.Second * 30))
n, err = io.ReadAtLeast(c.conn, b, l)
if err != nil {
break
}
if n != l {
//长度不够,费包
break
}
}

tp := buf[3] >> 4

Check failure on line 69 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

tp declared and not used
enc := buf[3] & 0xf

Check failure on line 70 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

enc declared and not used

var pack Pack
pack.Decode(b)

Check failure on line 73 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

undefined: b

}

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

}

func (c *Client) Open() error {
//rpc://username:[email protected]:719
u, err := url.Parse(c.Url)
if err != nil {
return err
}

if u.Scheme != "rpc" {
return errors.New("协议错误")
}

if u.User == nil {
return errors.New("缺少用户名和密码")
}

c.conn, err = net.Dial("tcp", u.Hostname())
if err != nil {
return err
}

pack := Pack{
Type: CONNECT,
Encoding: JSON,
Content: map[string]any{
"username": u.User.Username(),
"password": u.User.Password(),

Check failure on line 107 in rpc/client.go

View workflow job for this annotation

GitHub Actions / build

multiple-value u.User.Password() (value of type (string, bool)) in single-value context
},
}

err = c.Write(&pack)
if err != nil {
return err
}

//持续接收消息
go c.receive()

return nil
}
2 changes: 1 addition & 1 deletion rpc/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var ErrNotEnough = errors.New("长度不足")
type Encoder func(any) ([]byte, error)
type Decoder func([]byte, any) error

const MAGIC = "rgc"
const MAGIC = "rpc"

const (
DISCONNECT uint8 = iota
Expand Down
4 changes: 4 additions & 0 deletions rpc/stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package rpc

type Stream struct {
}

0 comments on commit e4fe77e

Please sign in to comment.