Skip to content

Commit ce16f7c

Browse files
committed
dove
1 parent 899c3cd commit ce16f7c

File tree

4 files changed

+226
-14
lines changed

4 files changed

+226
-14
lines changed

dove/api/base.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
syntax = "proto3";
2+
package dove;
3+
option go_package = "./dove";
4+
5+
message Dove {
6+
uint64 id = 1;
7+
bytes data = 2;
8+
uint64 seq= 3;
9+
int64 timestamp= 4;
10+
}

dove/api/dove/base.pb.go

Lines changed: 168 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dove/dove.go

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,57 @@
11
package dove
22

33
import (
4+
"errors"
5+
"fmt"
6+
"github.com/golang/protobuf/proto"
7+
api "github.com/hwholiday/ghost/dove/api/dove"
8+
"github.com/hwholiday/ghost/dove/network"
49
"log"
510
"net"
611
"sync"
7-
8-
"github.com/hwholiday/learning_tools/dove/network"
912
)
1013

1114
const (
1215
DefaultWsPort = ":8081"
1316
)
1417
const (
15-
DefaultConnAcceptCrcId int = 1
16-
DefaultConnCloseCrcId int = 2
18+
DefaultConnAcceptCrcId uint64 = 1
19+
DefaultConnCloseCrcId uint64 = 2
1720
)
1821

1922
type HandleFunc func(cli network.Conn, reqData interface{})
2023

2124
type Dove interface {
22-
RegisterHandleFunc(id int, fn HandleFunc)
25+
RegisterHandleFunc(id uint64, fn HandleFunc)
2326
ListenWs() error
2427
}
2528

2629
type dove struct {
2730
rw sync.RWMutex
2831
manger *manager
29-
HandleFuncMap map[int]HandleFunc
32+
HandleFuncMap map[uint64]HandleFunc
3033
}
3134

3235
func NewDove() Dove {
3336
h := dove{
3437
manger: Manager(),
35-
HandleFuncMap: make(map[int]HandleFunc),
38+
HandleFuncMap: make(map[uint64]HandleFunc),
3639
}
3740
return &h
3841
}
3942

40-
func (h *dove) RegisterHandleFunc(id int, fn HandleFunc) {
43+
func (h *dove) verifyID(id uint64) error {
44+
if id == DefaultConnCloseCrcId || id == DefaultConnAcceptCrcId {
45+
return errors.New("please don't use default id")
46+
}
47+
return nil
48+
}
49+
50+
func (h *dove) RegisterHandleFunc(id uint64, fn HandleFunc) {
51+
if err := h.verifyID(id); err != nil {
52+
log.Printf("[Dove] RegisterHandleFunc : %s \n", err.Error())
53+
return
54+
}
4155
h.rw.Lock()
4256
defer h.rw.Unlock()
4357
if _, ok := h.HandleFuncMap[id]; ok {
@@ -50,6 +64,14 @@ func (h *dove) RegisterHandleFunc(id int, fn HandleFunc) {
5064
func (h *dove) ListenWs() error {
5165
return nil
5266
}
67+
func (h *dove) triggerHandle(client network.Conn, id uint64, req []byte) {
68+
fn, ok := h.HandleFuncMap[id]
69+
if !ok {
70+
log.Printf("[Dove] accept HandleFuncMap not register id : %d \n", req)
71+
return
72+
}
73+
fn(client, req)
74+
}
5375

5476
func (h *dove) accept(conn net.Conn) error {
5577
client, err := network.NewConn(network.WithConn(conn))
@@ -59,17 +81,30 @@ func (h *dove) accept(conn net.Conn) error {
5981
if err = h.manger.Add(client.Cache().Get(network.Identity).String(), client); err != nil {
6082
return err
6183
}
84+
h.triggerHandle(client, DefaultConnAcceptCrcId, nil)
6285
for {
6386
byt, err := client.Read()
6487
if err != nil {
6588
h.manger.Del(client.Cache().Get(network.Identity).String())
89+
h.triggerHandle(client, DefaultConnCloseCrcId, nil)
6690
return err
6791
}
68-
fn, ok := h.HandleFuncMap[1]
69-
if !ok {
70-
log.Printf("[Dove] HandleFunc not register id : %d \n")
92+
req, err := parseByt(byt)
93+
if err != nil {
94+
log.Printf("[Dove] accept parseByt %s \n", err.Error())
7195
continue
7296
}
73-
fn(client, byt)
97+
h.triggerHandle(client, req.GetId(), req.GetData())
98+
}
99+
}
100+
101+
func parseByt(byt []byte) (*api.Dove, error) {
102+
var req api.Dove
103+
if len(byt) < 8 {
104+
return nil, fmt.Errorf("byt len is : %d minimum length is 8\n", len(byt))
105+
}
106+
if err := proto.Unmarshal(byt, &req); err != nil {
107+
return nil, err
74108
}
109+
return &req, nil
75110
}

dove/manager.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package dove
22

33
import (
44
"errors"
5+
"github.com/hwholiday/ghost/dove/network"
56
"sync"
67
"sync/atomic"
7-
8-
"github.com/hwholiday/learning_tools/dove/network"
98
)
109

1110
var ErrExceedsLengthLimit = errors.New("exceeds length limit")

0 commit comments

Comments
 (0)