Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
gldsly committed May 20, 2020
0 parents commit 8d71ad0
Show file tree
Hide file tree
Showing 9 changed files with 1,289 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
*.log
*.log*
*.exe

104 changes: 104 additions & 0 deletions connmgr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
socket长连接管理相关方法和函数
*/

package main

import (
"log"
"net"
"sync"
"time"
)

var (
// OnlineClient 客户端网络地址和连接句柄映射
OnlineClient = make(map[string]*TerminalInfo)
// ClientAddrToken 客户端令牌和网络地址映射
ClientAddrToken = make(map[string]string)
connTableLock sync.Mutex
)

// TerminalInfo 终端连接信息
type TerminalInfo struct {
Addr string
TimeStamp int64
Conn net.Conn
CarNumber string
}

// addClient 添加已连接设备
func addClient(token string, t *TerminalInfo) {
connTableLock.Lock()
defer func() {
log.Println("新的客户端已添加:", token)
connTableLock.Unlock()
}()

OnlineClient[token] = t
ClientAddrToken[t.Addr] = token
}

// removeClient 移除已连接客户端
func removeClient(token string) {
connTableLock.Lock()
defer func() {
connTableLock.Unlock()
}()
term, ok := OnlineClient[token]

if !ok || term == nil {
return
}
conn := term.Conn
log.Printf("[GPS Server] 连接: %s 车牌号: %s 连接断开...", conn.RemoteAddr().String(), term.CarNumber)
delete(ClientAddrToken, conn.RemoteAddr().String())
_ = conn.Close()
delete(OnlineClient, token)
}

// updateClientState 更新客户端的时间
func updateClientState(token string) {
connTableLock.Lock()
defer func() {
connTableLock.Unlock()
}()
OnlineClient[token].TimeStamp = time.Now().Unix()
}

// removeTimeoutClient 移除超时的客户端
func removeTimeoutClient() {

log.Println("[GPS Server]心跳 监控 进程启动...")
var count = 0
for range time.Tick(time.Duration(1) * time.Second) {
nowTime := time.Now().Unix()
if count >= 60 {
count = 0
}

// 检测已连接客户端是否超时
if count%30 == 0 {
// log.Println("[GPS Server] 开始检测客户端连接状态...")
for token, val := range OnlineClient {
if int(nowTime-val.TimeStamp) > 70 {
// log.Println("关闭的客户端:", token, int(nowTime-val.TimeStamp))
removeClient(token)
}
}
}

// 删除超时车辆缓存
if count%60 == 0 {
// log.Println("[GPS Server] 开始清除车辆缓存超时数据...")
for key, cache := range cacheVehicleLocationData {
if int(nowTime-cache.TimeStamp) > 86400 {
// log.Println("删除的车辆:", key, nowTime, cache.TimeStamp, 86400)
delete(cacheVehicleLocationData, key)
}
}
}
count++
}

}
47 changes: 47 additions & 0 deletions defs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"errors"
"sync"
)

var (
errEOF = errors.New("eof")
serialNumber uint16 = 0
lock sync.Mutex
)

// 转义数据标志位
const (
DecodeMsg = iota
EncodeMsg
)

// SplitPackContent 数据分包时:消息头带的分包信息
type SplitPackContent struct {
Total uint16
CurNum uint16 `comment:"从1开始计数"`
}

// messageAttr 消息头属性
type messageHeadAttr struct {
BodyLength int
CryptoMethod string
SplitPack string
Other string
}

// messageHead 消息头
type messageHead struct {
ID string
Attr messageHeadAttr
Phone string
SerialNum uint16
SPC *SplitPackContent
}

// ProtocolData jt/t808-2013 协议规定包数据
type ProtocolData struct {
Head *messageHead
Body []byte
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module jtt808

go 1.14

require (
github.com/imroc/biu v0.0.0-20170329141542-0376ce6761c0
golang.org/x/text v0.3.2
)
Loading

0 comments on commit 8d71ad0

Please sign in to comment.