Joyit пре 4 недеља
родитељ
комит
389a257e8e
4 измењених фајлова са 45 додато и 34 уклоњено
  1. 0 25
      config/const.go
  2. 36 0
      const.go
  3. 5 5
      hub.go
  4. 4 4
      type.go

+ 0 - 25
config/const.go

@@ -15,31 +15,6 @@ const (
 	MAX_SYSTEM_ERROR_CODE = 127 //系统信息最大值
 )
 
-// 转换 id 到对应的消息
-func IdMsg(id int) string {
-	switch id {
-	case NEXT_SUBSCRIBE:
-		return "NEXT SUBSCRIBE"
-	case NEXT_MIDDLE:
-		return "NEXT MIDDLE"
-	case CONVERT_FAILED:
-		return "CONVERT FAILED"
-	case FORBIDDEN:
-		return "FORBIDDEN"
-	case SYSTEM_ERROR:
-		return "SYSTEM ERROR"
-	case GET_TIMEOUT:
-		return "GET TIMEOUT"
-	case NO_MATCH_CONNECT:
-		return "NO MATCH CONNECT"
-	case NO_MATCH_FILTER:
-		return "NO MATCH FILTER"
-	case NO_MATCH_CMD:
-		return "NO MATCH CMD"
-	}
-	return "UNKNOWN"
-}
-
 const (
 	// ID 号最高值,高于这个值的ID号为系统内部使用
 	ID_MAX = 65500

+ 36 - 0
const.go

@@ -0,0 +1,36 @@
+package tinymq
+
+import (
+	"fmt"
+
+	"git.me9.top/git/tinymq/config"
+)
+
+// 定义成功与失败的值
+const STATE_OK = 1
+const STATE_FAILED = 0
+
+// 转换 id 到对应的消息
+func IdMsg(id uint8) string {
+	switch id {
+	case config.NEXT_SUBSCRIBE:
+		return "NEXT SUBSCRIBE"
+	case config.NEXT_MIDDLE:
+		return "NEXT MIDDLE"
+	case config.CONVERT_FAILED:
+		return "CONVERT FAILED"
+	case config.FORBIDDEN:
+		return "FORBIDDEN"
+	case config.SYSTEM_ERROR:
+		return "SYSTEM ERROR"
+	case config.GET_TIMEOUT:
+		return "GET TIMEOUT"
+	case config.NO_MATCH_CONNECT:
+		return "NO MATCH CONNECT"
+	case config.NO_MATCH_FILTER:
+		return "NO MATCH FILTER"
+	case config.NO_MATCH_CMD:
+		return "NO MATCH CMD"
+	}
+	return fmt.Sprintf("UNKNOWN %d", id)
+}

+ 5 - 5
hub.go

@@ -276,7 +276,7 @@ func (h *Hub) sendRequest(gd *GetData) (count int) {
 						go h.outResponse(&ResponseData{
 							Id:    id,
 							State: config.GET_TIMEOUT,
-							Data:  fmt.Appendf(nil, "[%s] %s %s", config.IdMsg(config.GET_TIMEOUT), conn.channel, gd.Cmd),
+							Data:  fmt.Appendf(nil, "[%s] %s %s", IdMsg(config.GET_TIMEOUT), conn.channel, gd.Cmd),
 							conn:  conn,
 						})
 						// 检查是否已经很久时间没有使用连接了
@@ -395,7 +395,7 @@ func (h *Hub) requestFromNet(request *RequestData) {
 						if err != nil {
 							log.Println(err.Error())
 							state = config.CONVERT_FAILED
-							byteData = fmt.Appendf(nil, "[%s] %s %s", config.IdMsg(config.CONVERT_FAILED), request.conn.channel, request.Cmd)
+							byteData = fmt.Appendf(nil, "[%s] %s %s", IdMsg(config.CONVERT_FAILED), request.conn.channel, request.Cmd)
 						}
 					}
 				}
@@ -420,7 +420,7 @@ func (h *Hub) requestFromNet(request *RequestData) {
 	request.conn.sendResponse <- &ResponseData{
 		Id:    request.Id,
 		State: config.NO_MATCH_CMD,
-		Data:  fmt.Appendf(nil, "[%s] Channel: %s, Cmd: %s", config.IdMsg(config.NO_MATCH_CMD), channel, cmd),
+		Data:  fmt.Appendf(nil, "[%s] Channel: %s, Cmd: %s", IdMsg(config.NO_MATCH_CMD), channel, cmd),
 	}
 }
 
@@ -494,7 +494,7 @@ func (h *Hub) GetOneWithStruct(gd *GetData) (response *ResponseData) {
 	if gd.Filter == nil {
 		return &ResponseData{
 			State: config.NO_MATCH_FILTER,
-			Data:  fmt.Appendf(nil, "[%s] not filter function", config.IdMsg(config.NO_MATCH_FILTER)),
+			Data:  fmt.Appendf(nil, "[%s] not filter function", IdMsg(config.NO_MATCH_FILTER)),
 		}
 	}
 	gd.Max = 1
@@ -505,7 +505,7 @@ func (h *Hub) GetOneWithStruct(gd *GetData) (response *ResponseData) {
 	if response == nil {
 		return &ResponseData{
 			State: config.NO_MATCH_CONNECT,
-			Data:  fmt.Appendf(nil, "[%s] Filter: %s, Cmd: %s", config.IdMsg(config.NO_MATCH_CONNECT), GetFunctionName(gd.Filter), gd.Cmd),
+			Data:  fmt.Appendf(nil, "[%s] Filter: %s, Cmd: %s", IdMsg(config.NO_MATCH_CONNECT), GetFunctionName(gd.Filter), gd.Cmd),
 		}
 	}
 	return

+ 4 - 4
type.go

@@ -12,10 +12,6 @@ import (
 	"time"
 )
 
-// 定义成功与失败的值
-const STATE_OK = 1
-const STATE_FAILED = 0
-
 // 中间件函数
 // 如果返回为空,表示处理完成,通过
 // 如果返回 NEXT_MIDDLE,表示需要下一个中间件函数处理;如果没有下一函数则默认通过
@@ -112,6 +108,10 @@ func (r *ResponseData) Conn() *Line {
 	return r.conn
 }
 
+func (r *ResponseData) String() string {
+	return fmt.Sprintf("id: %d, state: [%d]%s, data: %s", r.Id, r.State, IdMsg(r.State), string(r.Data))
+}
+
 type PingData struct {
 	Id uint16
 }