.
git-svn-id: svn://47.119.165.148/zhub@63 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
100
zsub/zsub.go
100
zsub/zsub.go
@@ -1,7 +1,11 @@
|
||||
package zsub
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -12,13 +16,14 @@ var (
|
||||
type ZSub struct {
|
||||
sync.Mutex
|
||||
topics map[string]*ZTopic
|
||||
timers map[string]*ZTimer
|
||||
}
|
||||
|
||||
|
||||
type ZConn struct { //ZConn
|
||||
conn *net.Conn
|
||||
groupid string
|
||||
topics []string
|
||||
timers []string // 订阅、定时调度分别创建各自连接
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -77,7 +82,7 @@ func (s ZSub) unsubscribe(c *ZConn, topic string) { // 取消订阅 zconn{}
|
||||
|
||||
/*
|
||||
发送主题消息
|
||||
1、写入主题消息列表(zdb)
|
||||
1、写入主题消息列表(_zdb)
|
||||
2、回复消息写入成功
|
||||
3、推送主题消息
|
||||
*/
|
||||
@@ -90,6 +95,95 @@ func (s ZSub) publish(topic string, message string) {
|
||||
}
|
||||
|
||||
for _, zgroup := range ztopic.groups {
|
||||
zgroup.chMsg <- message
|
||||
zgroup.chMsg <- message // 不同主题消费独立进行
|
||||
}
|
||||
}
|
||||
|
||||
func (s ZSub) close(c *ZConn) {
|
||||
// 订阅
|
||||
for _, topic := range c.topics {
|
||||
s.unsubscribe(c, topic)
|
||||
}
|
||||
|
||||
// 延时
|
||||
|
||||
// timer conn close
|
||||
for _, topic := range c.timers { // fixme: 数据逻辑交叉循环
|
||||
timer := s.timers[topic]
|
||||
if timer != nil {
|
||||
timer.close(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ================== ZHub 服务 =====================================
|
||||
func ServerStart(host string, port int) {
|
||||
listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
log.Printf("_zdb started listen on: %s:%d \n", host, port)
|
||||
|
||||
// 启动消息监听处理
|
||||
go func() {
|
||||
for {
|
||||
v, ok := <-chanMessages
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
// 事件消费
|
||||
msgAccept(v)
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
conn, err := listen.Accept()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
fmt.Println("conn start: ", conn.RemoteAddr())
|
||||
|
||||
go zsub.acceptHandler(&ZConn{conn: &conn})
|
||||
}
|
||||
}
|
||||
|
||||
// 连接处理
|
||||
func (s ZSub) acceptHandler(c *ZConn) {
|
||||
defer func() {
|
||||
s.close(c) // 关闭连接
|
||||
}()
|
||||
|
||||
reader := bufio.NewReader(*c.conn)
|
||||
for {
|
||||
rcmd := make([]string, 0)
|
||||
line, _, err := reader.ReadLine()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
switch string(line[:1]) {
|
||||
case "*":
|
||||
n, _ := strconv.Atoi(string(line[1:]))
|
||||
for i := 0; i < n; i++ {
|
||||
reader.ReadLine()
|
||||
v, _, _ := reader.ReadLine()
|
||||
rcmd = append(rcmd, string(v))
|
||||
}
|
||||
default:
|
||||
rcmd = append(rcmd, string(line))
|
||||
}
|
||||
|
||||
if len(rcmd) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 接收消息 zdb fixme: 细节暴露太多
|
||||
chanMessages <- Message{Conn: c, Rcmd: rcmd}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user