|
|
|
@ -62,12 +62,12 @@ func (para *FLXNetworkCmdSender) InitPara() {
|
|
|
|
|
// log.AddFilter("stdout", log4go.DEBUG, log4go.NewConsoleLogWriter())
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
type CustomMessage struct {
|
|
|
|
|
type Message struct {
|
|
|
|
|
Content []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 实现tao.Message接口方法
|
|
|
|
|
func (m CustomMessage) Serialize() ([]byte, error) {
|
|
|
|
|
func (m Message) Serialize() ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
@ -78,7 +78,7 @@ func (m CustomMessage) Serialize() ([]byte, error) {
|
|
|
|
|
return m.Content, nil // 直接返回原始字节
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m CustomMessage) MessageNumber() int32 {
|
|
|
|
|
func (m Message) MessageNumber() int32 {
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
@ -176,6 +176,7 @@ func ConnectToServer() {
|
|
|
|
|
MPerIDNetID[v] = strid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
IsConnect = true
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(UnSendToServerData); {
|
|
|
|
|
AsClicent.Write(UnSendToServerData[i])
|
|
|
|
@ -199,15 +200,40 @@ func ConnectToServer() {
|
|
|
|
|
onMessage := tao.OnMessageOption(func(msg tao.Message, c tao.WriteCloser) {
|
|
|
|
|
Remote_IP := strings.Split((c.(*tao.ClientConn)).Name(), ":")[0]
|
|
|
|
|
IsConnect = true
|
|
|
|
|
fmt.Println(msg.(CustomMessage).Content, Remote_IP)
|
|
|
|
|
fmt.Println(msg.(Message).Content, Remote_IP)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
AsClicent = tao.NewClientConn(0, c, onConnect, onError, onClose, onMessage)
|
|
|
|
|
|
|
|
|
|
AsClicent.Start()
|
|
|
|
|
return // 连接成功后退出循环
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// ChatMessage is the message number of chat message.
|
|
|
|
|
ChatMessage int32 = 1
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DeserializeMessage deserializes bytes into Message.
|
|
|
|
|
func DeserializeMessage(data []byte) (message tao.Message, err error) {
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
log.Error("DeserializeMessage:", r)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
if data == nil {
|
|
|
|
|
return nil, tao.ErrNilData
|
|
|
|
|
}
|
|
|
|
|
content := (data)
|
|
|
|
|
msg := Message{
|
|
|
|
|
Content: content,
|
|
|
|
|
}
|
|
|
|
|
return msg, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func StartTCP() {
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
@ -218,6 +244,7 @@ func StartTCP() {
|
|
|
|
|
|
|
|
|
|
MIPNetID = make(map[string]int64)
|
|
|
|
|
MPerIDNetID = make(map[string]int64)
|
|
|
|
|
tao.Register(ChatMessage, DeserializeMessage, nil)
|
|
|
|
|
go ConnectToServer()
|
|
|
|
|
|
|
|
|
|
// //TCP连接
|
|
|
|
@ -255,8 +282,8 @@ func StartTCP() {
|
|
|
|
|
|
|
|
|
|
// onMessage := tao.OnMessageOption(func(msg tao.Message, c tao.WriteCloser) {
|
|
|
|
|
// Remote_IP := strings.Split((c.(*tao.ClientConn)).Name(), ":")[0]
|
|
|
|
|
// // AsClicent_DatagramReceived(Remote_IP, (c.(*tao.ClientConn)).NetID(), msg.(CustomMessage).Content)
|
|
|
|
|
// fmt.Println(msg.(CustomMessage).Content, Remote_IP)
|
|
|
|
|
// // AsClicent_DatagramReceived(Remote_IP, (c.(*tao.ClientConn)).NetID(), msg.(Message).Content)
|
|
|
|
|
// fmt.Println(msg.(Message).Content, Remote_IP)
|
|
|
|
|
// })
|
|
|
|
|
// AsClicent = tao.NewClientConn(0, c, onConnect, onError, onClose, onMessage)
|
|
|
|
|
// AsClicent.Start()
|
|
|
|
@ -288,7 +315,7 @@ func CTcpServerStart() {
|
|
|
|
|
|
|
|
|
|
var SendLock sync.RWMutex
|
|
|
|
|
|
|
|
|
|
var UnSendToServerData = make([]CustomMessage, 0)
|
|
|
|
|
var UnSendToServerData = make([]Message, 0)
|
|
|
|
|
|
|
|
|
|
// / <summary>发送命令</summary>
|
|
|
|
|
// / <param name="buf">命令内容</param>
|
|
|
|
@ -311,53 +338,53 @@ func (para *FLXNetworkCmdSender) SendToServer(dataMsg []byte) {
|
|
|
|
|
}
|
|
|
|
|
// dataMsg = FullCheckSum(dataMsg)
|
|
|
|
|
|
|
|
|
|
if para.IsSendToServer {
|
|
|
|
|
if para.IsConnect {
|
|
|
|
|
|
|
|
|
|
// _, err := para.CurrentSocket.Write([]byte{0x7e})
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// para.IsConnect = false // 立即标记为断开
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// _, err := para.CurrentSocket.Write(dataMsg)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// fmt.Println("tcp发送失败", err.Error())
|
|
|
|
|
// }
|
|
|
|
|
// fmt.Println(dataMsg)
|
|
|
|
|
if IsConnect {
|
|
|
|
|
SendLock.Lock()
|
|
|
|
|
|
|
|
|
|
// 伪代码示例:需确保遍历所有连接
|
|
|
|
|
|
|
|
|
|
msg := CustomMessage{Content: dataMsg}
|
|
|
|
|
// CTcpServer.Broadcast(msg)
|
|
|
|
|
|
|
|
|
|
error := AsClicent.Write(msg)
|
|
|
|
|
if error != nil {
|
|
|
|
|
if !Common.Contain(UnSendToServerData, msg) {
|
|
|
|
|
UnSendToServerData = append(UnSendToServerData, msg)
|
|
|
|
|
}
|
|
|
|
|
log.Error("SendToServer发送卡号失败:", msg)
|
|
|
|
|
}
|
|
|
|
|
// if para.IsSendToServer {
|
|
|
|
|
if para.IsConnect {
|
|
|
|
|
|
|
|
|
|
// _, err := para.CurrentSocket.Write([]byte{0x7e})
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// para.IsConnect = false // 立即标记为断开
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// _, err := para.CurrentSocket.Write(dataMsg)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// fmt.Println("tcp发送失败", err.Error())
|
|
|
|
|
// }
|
|
|
|
|
// fmt.Println(dataMsg)
|
|
|
|
|
if IsConnect {
|
|
|
|
|
SendLock.Lock()
|
|
|
|
|
|
|
|
|
|
// 伪代码示例:需确保遍历所有连接
|
|
|
|
|
|
|
|
|
|
msg := Message{Content: dataMsg}
|
|
|
|
|
// CTcpServer.Broadcast(msg)
|
|
|
|
|
|
|
|
|
|
SendLock.Unlock()
|
|
|
|
|
} else {
|
|
|
|
|
msg := CustomMessage{Content: dataMsg}
|
|
|
|
|
error := AsClicent.Write(msg)
|
|
|
|
|
if error != nil {
|
|
|
|
|
if !Common.Contain(UnSendToServerData, msg) {
|
|
|
|
|
UnSendToServerData = append(UnSendToServerData, msg)
|
|
|
|
|
}
|
|
|
|
|
log.Error("SendToServer发送卡号失败:", msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SendLock.Unlock()
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
fmt.Printf("服务器没有连接!")
|
|
|
|
|
if para.TryConnect(0) {
|
|
|
|
|
para.CurrentSocket.Write(dataMsg)
|
|
|
|
|
msg := Message{Content: dataMsg}
|
|
|
|
|
if !Common.Contain(UnSendToServerData, msg) {
|
|
|
|
|
UnSendToServerData = append(UnSendToServerData, msg)
|
|
|
|
|
}
|
|
|
|
|
log.Error("SendToServer发送卡号失败:", msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
fmt.Printf("服务器没有连接!")
|
|
|
|
|
if para.TryConnect(0) {
|
|
|
|
|
para.CurrentSocket.Write(dataMsg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// }
|
|
|
|
|
if !para.IsSendToBackUpServer {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|