|
@@ -4,7 +4,6 @@ import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"errors"
|
|
"errors"
|
|
"flag"
|
|
"flag"
|
|
- "fmt"
|
|
|
|
"io"
|
|
"io"
|
|
"math/rand"
|
|
"math/rand"
|
|
"mfw/util"
|
|
"mfw/util"
|
|
@@ -128,23 +127,6 @@ func updateheartbeat(from string) {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-//查看所有服务提供者
|
|
|
|
-func viewallservice(p *util.Packet) {
|
|
|
|
- defer util.Catch()
|
|
|
|
- ret := []interface{}{}
|
|
|
|
- lock.Lock()
|
|
|
|
- for k, v := range allclient {
|
|
|
|
- ret = append(ret, map[string]interface{}{
|
|
|
|
- "handle": fmt.Sprintf("%v", v.canhandlerevent),
|
|
|
|
- "name": v.name,
|
|
|
|
- "myid": k,
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
- lock.Unlock()
|
|
|
|
- bs, _ := json.Marshal(ret)
|
|
|
|
- allclient[p.From].conn.Write(util.Enpacket("", p.From, p.Msgid, util.EVENT_RETURN, util.SENDTO_TYPE_P2P, bs))
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
//加入网络
|
|
//加入网络
|
|
func join(msg *util.Packet) {
|
|
func join(msg *util.Packet) {
|
|
data := map[string]interface{}{}
|
|
data := map[string]interface{}{}
|
|
@@ -195,35 +177,30 @@ func processmsg(msg *util.Packet) {
|
|
services := []int{}
|
|
services := []int{}
|
|
json.Unmarshal(msg.Raw, &services)
|
|
json.Unmarshal(msg.Raw, &services)
|
|
publishservice(from, services)
|
|
publishservice(from, services)
|
|
- case util.EVENT_VIEWALL_CLIENT: //
|
|
|
|
- go viewallservice(msg)
|
|
|
|
- case util.EVENT_REMOVE_CLIENT: //服务端强制删除节点
|
|
|
|
- removeClient(string(msg.Raw))
|
|
|
|
case util.EVENT_BYE: //客户端主动要求断开
|
|
case util.EVENT_BYE: //客户端主动要求断开
|
|
removeClient(from)
|
|
removeClient(from)
|
|
- default: //处理业务事件
|
|
|
|
- //识别发送类型
|
|
|
|
|
|
+ default: //处理业务事件,转发数据包
|
|
sttype := int(msg.SentToType)
|
|
sttype := int(msg.SentToType)
|
|
if sttype == util.SENDTO_TYPE_ALL { //发送给所有节点
|
|
if sttype == util.SENDTO_TYPE_ALL { //发送给所有节点
|
|
for service_machine_id, v := range allclient { //发所有,不支持的不处理
|
|
for service_machine_id, v := range allclient { //发所有,不支持的不处理
|
|
if service_machine_id == from { //广播不用发给自己
|
|
if service_machine_id == from { //广播不用发给自己
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
- _, err := v.conn.Write(msg.Raw)
|
|
|
|
|
|
+ _, err := v.conn.Write(msg.Bytes())
|
|
if err == nil {
|
|
if err == nil {
|
|
updateheartbeat(service_machine_id)
|
|
updateheartbeat(service_machine_id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else if sttype == util.SENDTO_TYPE_P2P { //点对点发消息,不用注册服务
|
|
} else if sttype == util.SENDTO_TYPE_P2P { //点对点发消息,不用注册服务
|
|
if v2, ok2 := allclient[msg.To]; ok2 {
|
|
if v2, ok2 := allclient[msg.To]; ok2 {
|
|
- _, err := v2.conn.Write(msg.Raw)
|
|
|
|
|
|
+ _, err := v2.conn.Write(msg.Bytes())
|
|
if err == nil {
|
|
if err == nil {
|
|
updateheartbeat(msg.To)
|
|
updateheartbeat(msg.To)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else if sttype == util.SENDTO_TYPE_P2P_BYNAME { //通过名称点对点发消息
|
|
} else if sttype == util.SENDTO_TYPE_P2P_BYNAME { //通过名称点对点发消息
|
|
if v2, err2 := findClientByName(msg.To); err2 == nil {
|
|
if v2, err2 := findClientByName(msg.To); err2 == nil {
|
|
- _, err := v2.conn.Write(msg.Raw)
|
|
|
|
|
|
+ _, err := v2.conn.Write(msg.Bytes())
|
|
if err == nil {
|
|
if err == nil {
|
|
updateheartbeat(v2.id)
|
|
updateheartbeat(v2.id)
|
|
}
|
|
}
|
|
@@ -241,7 +218,7 @@ func processmsg(msg *util.Packet) {
|
|
client, ok := allclient[service_machine_id]
|
|
client, ok := allclient[service_machine_id]
|
|
lock.Unlock()
|
|
lock.Unlock()
|
|
if ok {
|
|
if ok {
|
|
- _, err := client.conn.Write(msg.Raw)
|
|
|
|
|
|
+ _, err := client.conn.Write(msg.Bytes())
|
|
if err == nil {
|
|
if err == nil {
|
|
updateheartbeat(service_machine_id)
|
|
updateheartbeat(service_machine_id)
|
|
break
|
|
break
|
|
@@ -258,7 +235,7 @@ func processmsg(msg *util.Packet) {
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
if v2, ok2 := allclient[service_machine_id]; ok2 {
|
|
if v2, ok2 := allclient[service_machine_id]; ok2 {
|
|
- _, err := v2.conn.Write(msg.Raw)
|
|
|
|
|
|
+ _, err := v2.conn.Write(msg.Bytes())
|
|
if err == nil {
|
|
if err == nil {
|
|
updateheartbeat(service_machine_id)
|
|
updateheartbeat(service_machine_id)
|
|
}
|
|
}
|
|
@@ -288,11 +265,11 @@ func main() {
|
|
go gc()
|
|
go gc()
|
|
//启动服务
|
|
//启动服务
|
|
util.StartServer(&util.ServerConfig{
|
|
util.StartServer(&util.ServerConfig{
|
|
- ListenAddr: *addr,
|
|
|
|
- UseTls: *usetls,
|
|
|
|
- TlsCAFile: *tlsca,
|
|
|
|
- TlsCertFile: *tlscert,
|
|
|
|
- TlsKeyFile: *tlskey,
|
|
|
|
|
|
+ ListenAddr: *addr,
|
|
|
|
+ UseTls: *usetls,
|
|
|
|
+ TlsCAFile: *tlsca,
|
|
|
|
+ TlsCertFile: *tlscert,
|
|
|
|
+ TlsKeyFile: *tlskey,
|
|
ProcessEvent: func(data *util.Packet) {
|
|
ProcessEvent: func(data *util.Packet) {
|
|
//接受消息处理
|
|
//接受消息处理
|
|
processmsg(data)
|
|
processmsg(data)
|