/
announce.go
156 lines (126 loc) · 3.25 KB
/
announce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package app
import (
// "fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/midoks/go-p2p-server/internal/geoip"
"github.com/midoks/go-p2p-server/internal/hub"
"github.com/midoks/go-p2p-server/internal/logger"
"github.com/midoks/go-p2p-server/internal/mem"
"github.com/midoks/go-p2p-server/internal/queue"
"github.com/midoks/go-p2p-server/internal/tools"
"github.com/midoks/go-p2p-server/internal/tools/uniqid"
)
type AnPeer struct {
Id string `json:"id"`
}
var mu sync.RWMutex
//接收announce信息
func p2pChannel(c *gin.Context) {
postJson := make(map[string]interface{})
c.BindJSON(&postJson)
uniqidId := uniqid.New(uniqid.Params{"", false})
uniqidId = uniqidId + tools.RandId()
gPeers := []AnPeer{}
if channel, ok := postJson["channel"]; ok {
key := channel.(string)
if peer, ok := mem.GetChannel(key); ok {
for _, p := range peer {
gPeers = append(gPeers, AnPeer{Id: p})
}
mem.SetChannel(key, uniqidId)
} else {
mem.SetChannel(key, uniqidId)
}
}
go func() {
ipAddr := c.ClientIP()
if ipAddr == "127.0.0.1" {
ipAddr = tools.GetNetworkIp()
}
lat, lng := geoip.GetLatLongByIpAddr(ipAddr)
//客服端经纬度->保存到redis
mem.SetPeerLatLang(uniqidId, lat, lng)
to_lat, to_lng, err := mem.GetServerLatLang()
if err != nil {
logger.Errorf("announce.GetServerLatLang error: %v", err)
to_lat, to_lng = 0, 0
}
if !strings.HasPrefix(uniqidId, "p2p") {
queue.PushText("join", uniqidId, lat, lng, to_lat, to_lng)
}
}()
c.JSON(http.StatusOK, gin.H{
"ret": 0,
"data": gin.H{
"id": uniqidId,
"peers": gPeers,
"report_interval": 3,
"v": uniqidId,
},
})
}
//接收announce信息
func p2pChannelPeers(c *gin.Context) {
postJson := make(map[string]interface{}) //注意该结构接受的内容
c.ShouldBind(&postJson)
channel_id := c.Param("channel_id")
peers := c.Param("peer")
gPeers := []AnPeer{}
if peer, ok := mem.GetChannel(channel_id); ok {
for _, p := range peer {
gPeers = append(gPeers, AnPeer{Id: p})
}
}
mem.SetPeerHeartbeat(peers, 60*time.Second)
if c, ok := hub.GetClient(peers); ok {
c.UpdateTs()
}
c.JSON(http.StatusOK, gin.H{
"ret": 0,
"data": gin.H{
"id": peers,
"peers": gPeers,
"report_interval": 3,
"v": peers,
},
})
}
//接收announce信息
func p2pChannelStats(c *gin.Context) {
json := make(map[string]interface{})
c.ShouldBind(&json)
channel_id := c.Param("channel_id")
peers := c.Param("peer")
//延长缓冲时间
mem.SetPeerHeartbeat(peers, 60*time.Second)
if c, ok := hub.GetClient(peers); ok {
c.UpdateTs()
}
//查找缓冲中的peer
gPeers := []AnPeer{}
if peer, ok := mem.GetChannel(channel_id); ok {
for _, p := range peer {
gPeers = append(gPeers, AnPeer{Id: p})
}
}
// if !strings.HasPrefix(peers, "p2p") {
// ipAddr := c.ClientIP()
// if ipAddr == "127.0.0.1" {
// ipAddr = tools.GetNetworkIp()
// }
// lat, lng := geoip.GetLatLongByIpAddr(ipAddr)
// to_lat, to_lng, _ := mem.GetServerLatLang()
// queue.PushText("join", peers, lat, lng, to_lat, to_lng)
// }
c.JSON(http.StatusOK, gin.H{
"ret": 0,
"id": peers,
"name": "stats",
"peers": gPeers,
"data": gin.H{},
})
}