|
|
@@ -7,6 +7,7 @@ import (
|
|
|
"encoding/hex"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "git.qianqiusoft.com/public/glog"
|
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
|
|
|
"git.qianqiusoft.com/qianqiusoft/light-apiengine/entitys"
|
|
|
sysutils "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
|
|
|
@@ -20,26 +21,32 @@ const (
|
|
|
|
|
|
CMD_NEW = "new"
|
|
|
CMD_REMOVE = "remove"
|
|
|
+ CMD_PINGPONG = "pg"
|
|
|
+ CMD_PINGPONG_RESP = "pg_resp"
|
|
|
)
|
|
|
|
|
|
var TCPClient *TcpClient
|
|
|
|
|
|
type authPackage struct {
|
|
|
- Cmd string
|
|
|
- Content []byte
|
|
|
+ Cmd string
|
|
|
+ TokenStr string
|
|
|
+ Content []byte
|
|
|
}
|
|
|
|
|
|
func (ap *authPackage)toBytes()[]byte{
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
b := []byte(ap.Cmd)
|
|
|
- buf.Write(uint64ToBytes(len(b)))
|
|
|
+ buf.Write(uint32ToBytes(len(b)))
|
|
|
buf.Write(b)
|
|
|
|
|
|
- buf.Write(uint64ToBytes(len(ap.Content)))
|
|
|
+ buf.Write(uint32ToBytes(len(ap.Content)))
|
|
|
buf.Write(ap.Content)
|
|
|
|
|
|
- return buf.Bytes()
|
|
|
+ bytess := buf.Bytes()
|
|
|
+ fmt.Println(bytess)
|
|
|
+
|
|
|
+ return bytess
|
|
|
}
|
|
|
|
|
|
type TcpClient struct {
|
|
|
@@ -68,6 +75,7 @@ func(c *TcpClient)Start() {
|
|
|
if p := recover(); p != nil {
|
|
|
fmt.Println("ecover", p)
|
|
|
}
|
|
|
+ c.restart()
|
|
|
}()
|
|
|
|
|
|
var err error = nil
|
|
|
@@ -76,7 +84,6 @@ func(c *TcpClient)Start() {
|
|
|
c.conn, err = net.Dial("tcp", address)
|
|
|
if err != nil {
|
|
|
fmt.Println("Error dialing", err.Error())
|
|
|
- c.restart()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -87,7 +94,6 @@ func(c *TcpClient)Start() {
|
|
|
vresp, err := readString(c.conn)
|
|
|
if err != nil {
|
|
|
fmt.Println("Error dialing", err.Error())
|
|
|
- c.restart()
|
|
|
return
|
|
|
}
|
|
|
if vresp != "ok"{
|
|
|
@@ -95,6 +101,7 @@ func(c *TcpClient)Start() {
|
|
|
fmt.Println("verify is not ok", vresp)
|
|
|
return
|
|
|
}
|
|
|
+ fmt.Println("验证成功")
|
|
|
c.verified = true
|
|
|
|
|
|
// send
|
|
|
@@ -121,23 +128,22 @@ func(c *TcpClient)Start() {
|
|
|
fmt.Println("读取命令错误", err.Error())
|
|
|
break
|
|
|
}
|
|
|
- bytess, err := readBytes(c.conn)
|
|
|
- if err != nil {
|
|
|
+ if cmd == CMD_NEW{
|
|
|
+ err = c.newHandler()
|
|
|
+ } else if cmd == CMD_REMOVE{
|
|
|
+ err = c.removeHandler()
|
|
|
+ } else if cmd == CMD_PINGPONG_RESP{
|
|
|
+
|
|
|
+ } else {
|
|
|
+ fmt.Println("未知cmd", cmd)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if err != nil{
|
|
|
c.done <- true
|
|
|
- fmt.Println("读取token内容错误", err.Error())
|
|
|
+ fmt.Println("处理错误", err.Error())
|
|
|
break
|
|
|
}
|
|
|
- if cmd == "remove" {
|
|
|
- // 移除,此时bytess为tokenstring
|
|
|
- sysutils.GetGlobalTokenStore().Remove(string(bytess))
|
|
|
- } else if cmd == "new" {
|
|
|
- // 新建
|
|
|
- token, _ := bytesToToken(bytess)
|
|
|
- sysutils.GetGlobalTokenStore().Set(token.AccessToken, token)
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
- c.restart()
|
|
|
}()
|
|
|
}
|
|
|
|
|
|
@@ -176,7 +182,43 @@ func(c *TcpClient)Send(cmd string, bytess []byte){
|
|
|
|
|
|
// 发送token
|
|
|
func(c *TcpClient)SendToken(token *entitys.Token){
|
|
|
- c.Send(CMD_NEW, tokenToBytes(token))
|
|
|
+ glog.Infoln("发送新建token")
|
|
|
+ bytess := tokenToBytes(token)
|
|
|
+ fmt.Println(bytess)
|
|
|
+ c.Send(CMD_NEW, bytess)
|
|
|
+}
|
|
|
+
|
|
|
+// 处理创建
|
|
|
+func(c *TcpClient)newHandler()error{
|
|
|
+ fmt.Println("处理新建")
|
|
|
+ bytess, err := readBytes(c.conn)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("读取token内容错误", err.Error())
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ fmt.Println(len(bytess))
|
|
|
+ fmt.Println(bytess)
|
|
|
+ // 新建
|
|
|
+ token, err := bytesToToken(bytess)
|
|
|
+ if err != nil{
|
|
|
+ glog.Infoln("bytesToToken 错误", err.Error())
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ sysutils.GetGlobalTokenStore().Set(token.AccessToken, token)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// 处理删除
|
|
|
+func(c *TcpClient)removeHandler()error{
|
|
|
+ fmt.Println("处理删除")
|
|
|
+ bytess, err := readBytes(c.conn)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("读取token内容错误", err.Error())
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ // 移除,此时bytess为tokenstring
|
|
|
+ sysutils.GetGlobalTokenStore().Remove(string(bytess))
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
// 读取字符串
|
|
|
@@ -201,14 +243,11 @@ func readString(conn net.Conn)(string, error){
|
|
|
func writeString(conn net.Conn, str string)error{
|
|
|
if str == ""{
|
|
|
return errors.New("字符串为空")
|
|
|
- }else{
|
|
|
- fmt.Println("---->1", str)
|
|
|
}
|
|
|
|
|
|
bytess := []byte(str)
|
|
|
size := len(bytess)
|
|
|
|
|
|
- fmt.Println("---->2", str)
|
|
|
// 发送长度
|
|
|
err := writeUInt32(conn, uint32(size))
|
|
|
if err != nil{
|
|
|
@@ -216,7 +255,6 @@ func writeString(conn net.Conn, str string)error{
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- fmt.Println("---->3", str)
|
|
|
// 发送内容
|
|
|
n, err := conn.Write(bytess)
|
|
|
if err != nil{
|
|
|
@@ -226,7 +264,6 @@ func writeString(conn net.Conn, str string)error{
|
|
|
if n != size{
|
|
|
return errors.New("发送长度不是" + strconv.Itoa(int(size)))
|
|
|
}
|
|
|
- fmt.Println("---->4", str)
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
@@ -304,15 +341,33 @@ func writeUInt64(conn net.Conn, v uint64)error{
|
|
|
// 读取uint64
|
|
|
func readStringByBytes(bytess []byte)(string, int, error) {
|
|
|
size := binary.BigEndian.Uint32(bytess)
|
|
|
+ fmt.Println("----->", size)
|
|
|
return string(bytess[4 : 4+size]), int(size), nil
|
|
|
}
|
|
|
|
|
|
+// int转bytes
|
|
|
+func uint32ToBytes(v int)[]byte{
|
|
|
+ b := make([]byte, 4)
|
|
|
+ binary.BigEndian.PutUint32(b, uint32(v))
|
|
|
+
|
|
|
+ return b
|
|
|
+}
|
|
|
+
|
|
|
+// int转bytes
|
|
|
+func uint64ToBytes(v int)[]byte{
|
|
|
+ b := make([]byte, 8)
|
|
|
+ binary.BigEndian.PutUint32(b, uint32(v))
|
|
|
+
|
|
|
+ return b
|
|
|
+}
|
|
|
+
|
|
|
// 转token
|
|
|
func bytesToToken(content []byte)(*entitys.Token, error){
|
|
|
token := &entitys.Token{}
|
|
|
var index int = 0
|
|
|
var size int
|
|
|
var err error = nil
|
|
|
+ fmt.Println("读取userid")
|
|
|
token.UserId, size, err = readStringByBytes(content)
|
|
|
if err != nil{
|
|
|
fmt.Println("读取userid错误")
|
|
|
@@ -320,6 +375,7 @@ func bytesToToken(content []byte)(*entitys.Token, error){
|
|
|
}
|
|
|
index += 4 + size
|
|
|
|
|
|
+ fmt.Println("读取AccessToken")
|
|
|
token.AccessToken, size, err = readStringByBytes(content[index:])
|
|
|
if err != nil{
|
|
|
fmt.Println("读取AccessToken错误")
|
|
|
@@ -327,6 +383,7 @@ func bytesToToken(content []byte)(*entitys.Token, error){
|
|
|
}
|
|
|
index += 4 + size
|
|
|
|
|
|
+ fmt.Println("读取RefreshToken")
|
|
|
token.RefreshToken, size, err = readStringByBytes(content[index:])
|
|
|
if err != nil{
|
|
|
fmt.Println("读取RefreshToken错误")
|
|
|
@@ -334,6 +391,7 @@ func bytesToToken(content []byte)(*entitys.Token, error){
|
|
|
}
|
|
|
index += 4 + size
|
|
|
|
|
|
+ fmt.Println("读取LoginID")
|
|
|
token.LoginID, size, err = readStringByBytes(content[index:])
|
|
|
if err != nil{
|
|
|
fmt.Println("读取LoginID错误")
|
|
|
@@ -341,9 +399,13 @@ func bytesToToken(content []byte)(*entitys.Token, error){
|
|
|
}
|
|
|
index += 4 + size
|
|
|
|
|
|
+ fmt.Println("读取timestamp")
|
|
|
token.TimeStamp = binary.BigEndian.Uint64(content[index:])
|
|
|
- index += 4
|
|
|
+ index += 8
|
|
|
+
|
|
|
+ fmt.Println("---->index", index)
|
|
|
|
|
|
+ fmt.Println("读取ServerIp")
|
|
|
token.ServerIp, size, err = readStringByBytes(content[index:])
|
|
|
if err != nil{
|
|
|
fmt.Println("读取ServerIp错误")
|
|
|
@@ -351,6 +413,7 @@ func bytesToToken(content []byte)(*entitys.Token, error){
|
|
|
}
|
|
|
index += 4 + size
|
|
|
|
|
|
+ fmt.Println("读取Domain")
|
|
|
token.Domain, size, err = readStringByBytes(content[index:])
|
|
|
if err != nil{
|
|
|
fmt.Println("读取Domain错误")
|
|
|
@@ -361,44 +424,47 @@ func bytesToToken(content []byte)(*entitys.Token, error){
|
|
|
return token, nil
|
|
|
}
|
|
|
|
|
|
-// int转bytes
|
|
|
-func uint64ToBytes(v int)[]byte{
|
|
|
- b := make([]byte, 4)
|
|
|
- binary.BigEndian.PutUint32(b, uint32(v))
|
|
|
-
|
|
|
- return b
|
|
|
-}
|
|
|
-
|
|
|
-// 转bytes
|
|
|
+// 转bytes,包括token开头
|
|
|
func tokenToBytes(token *entitys.Token)[]byte{
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
|
|
t := []byte(token.UserId)
|
|
|
- buf.Write(uint64ToBytes(len(t)))
|
|
|
+ buf.Write(uint32ToBytes(len(t)))
|
|
|
buf.Write(t)
|
|
|
|
|
|
t = []byte(token.AccessToken)
|
|
|
- buf.Write(uint64ToBytes(len(t)))
|
|
|
+ buf.Write(uint32ToBytes(len(t)))
|
|
|
buf.Write(t)
|
|
|
|
|
|
t = []byte(token.RefreshToken)
|
|
|
- buf.Write(uint64ToBytes(len(t)))
|
|
|
+ buf.Write(uint32ToBytes(len(t)))
|
|
|
buf.Write(t)
|
|
|
|
|
|
t = []byte(token.LoginID)
|
|
|
- buf.Write(uint64ToBytes(len(t)))
|
|
|
+ buf.Write(uint32ToBytes(len(t)))
|
|
|
buf.Write(t)
|
|
|
|
|
|
buf.Write(uint64ToBytes(int(token.TimeStamp)))
|
|
|
|
|
|
+ fmt.Println(token.ServerIp)
|
|
|
t = []byte(token.ServerIp)
|
|
|
- buf.Write(uint64ToBytes(len(t)))
|
|
|
+ buf.Write(uint32ToBytes(len(t)))
|
|
|
buf.Write(t)
|
|
|
|
|
|
+ fmt.Println(token.Domain)
|
|
|
t = []byte(token.Domain)
|
|
|
- buf.Write(uint64ToBytes(len(t)))
|
|
|
+ buf.Write(uint32ToBytes(len(t)))
|
|
|
buf.Write(t)
|
|
|
|
|
|
+ bytess := buf.Bytes()
|
|
|
+ buf = bytes.NewBuffer([]byte{}) // 这里用reset是错误的
|
|
|
+
|
|
|
+ tokenstrbytes := []byte(token.AccessToken)
|
|
|
+ buf.Write(uint32ToBytes(len(tokenstrbytes)))
|
|
|
+ buf.Write(tokenstrbytes)
|
|
|
+ buf.Write(uint32ToBytes(len(bytess)))
|
|
|
+ buf.Write(bytess)
|
|
|
+
|
|
|
return buf.Bytes()
|
|
|
}
|
|
|
|