add
This commit is contained in:
parent
a71309c8cc
commit
831a5ee51d
91
client.go
91
client.go
@ -99,13 +99,15 @@ type Client struct {
|
|||||||
listenConn *net.UDPConn
|
listenConn *net.UDPConn
|
||||||
tcplistenConn *net.TCPListener
|
tcplistenConn *net.TCPListener
|
||||||
|
|
||||||
localAddrToConnMap map[string]*ClientConn
|
localAddrToConnMap sync.Map
|
||||||
localIdToConnMap map[string]*ClientConn
|
localIdToConnMap sync.Map
|
||||||
|
|
||||||
sendPacket uint64
|
sendPacket uint64
|
||||||
recvPacket uint64
|
recvPacket uint64
|
||||||
sendPacketSize uint64
|
sendPacketSize uint64
|
||||||
recvPacketSize uint64
|
recvPacketSize uint64
|
||||||
|
localAddrToConnMapSize int
|
||||||
|
localIdToConnMapSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientConn struct {
|
type ClientConn struct {
|
||||||
@ -159,6 +161,14 @@ func (p *Client) SendPacket() uint64 {
|
|||||||
return p.sendPacket
|
return p.sendPacket
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Client) LocalIdToConnMapSize() int {
|
||||||
|
return p.localIdToConnMapSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Client) LocalAddrToConnMapSize() int {
|
||||||
|
return p.localAddrToConnMapSize
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Client) Run() error {
|
func (p *Client) Run() error {
|
||||||
|
|
||||||
conn, err := icmp.ListenPacket("ip4:icmp", "")
|
conn, err := icmp.ListenPacket("ip4:icmp", "")
|
||||||
@ -184,9 +194,6 @@ func (p *Client) Run() error {
|
|||||||
p.listenConn = listener
|
p.listenConn = listener
|
||||||
}
|
}
|
||||||
|
|
||||||
p.localAddrToConnMap = make(map[string]*ClientConn)
|
|
||||||
p.localIdToConnMap = make(map[string]*ClientConn)
|
|
||||||
|
|
||||||
if p.tcpmode > 0 {
|
if p.tcpmode > 0 {
|
||||||
go p.AcceptTcp()
|
go p.AcceptTcp()
|
||||||
} else {
|
} else {
|
||||||
@ -273,8 +280,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn, targetAddr string) {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
clientConn := &ClientConn{tcpaddr: tcpsrcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false,
|
clientConn := &ClientConn{tcpaddr: tcpsrcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false,
|
||||||
fm: fm}
|
fm: fm}
|
||||||
p.localAddrToConnMap[tcpsrcaddr.String()] = clientConn
|
p.addClientConn(uuid, tcpsrcaddr.String(), clientConn)
|
||||||
p.localIdToConnMap[uuid] = clientConn
|
|
||||||
loggo.Info("client accept new local tcp %s %s", uuid, tcpsrcaddr.String())
|
loggo.Info("client accept new local tcp %s %s", uuid, tcpsrcaddr.String())
|
||||||
|
|
||||||
loggo.Info("start connect remote tcp %s %s", uuid, tcpsrcaddr.String())
|
loggo.Info("start connect remote tcp %s %s", uuid, tcpsrcaddr.String())
|
||||||
@ -476,12 +482,11 @@ func (p *Client) Accept() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
clientConn := p.localAddrToConnMap[srcaddr.String()]
|
clientConn := p.getClientConnByAddr(srcaddr.String())
|
||||||
if clientConn == nil {
|
if clientConn == nil {
|
||||||
uuid := UniqueId()
|
uuid := UniqueId()
|
||||||
clientConn = &ClientConn{ipaddr: srcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false}
|
clientConn = &ClientConn{ipaddr: srcaddr, id: uuid, activeRecvTime: now, activeSendTime: now, close: false}
|
||||||
p.localAddrToConnMap[srcaddr.String()] = clientConn
|
p.addClientConn(uuid, srcaddr.String(), clientConn)
|
||||||
p.localIdToConnMap[uuid] = clientConn
|
|
||||||
loggo.Info("client accept new local udp %s %s", uuid, srcaddr.String())
|
loggo.Info("client accept new local udp %s %s", uuid, srcaddr.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -524,14 +529,12 @@ func (p *Client) processPacket(packet *Packet) {
|
|||||||
|
|
||||||
loggo.Debug("processPacket %s %s %d", packet.my.Id, packet.src.String(), len(packet.my.Data))
|
loggo.Debug("processPacket %s %s %d", packet.my.Id, packet.src.String(), len(packet.my.Data))
|
||||||
|
|
||||||
clientConn := p.localIdToConnMap[packet.my.Id]
|
clientConn := p.getClientConnById(packet.my.Id)
|
||||||
if clientConn == nil {
|
if clientConn == nil {
|
||||||
loggo.Debug("processPacket no conn %s ", packet.my.Id)
|
loggo.Debug("processPacket no conn %s ", packet.my.Id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := clientConn.ipaddr
|
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
clientConn.activeRecvTime = now
|
clientConn.activeRecvTime = now
|
||||||
|
|
||||||
@ -545,6 +548,7 @@ func (p *Client) processPacket(packet *Packet) {
|
|||||||
|
|
||||||
clientConn.fm.OnRecvFrame(f)
|
clientConn.fm.OnRecvFrame(f)
|
||||||
} else {
|
} else {
|
||||||
|
addr := clientConn.ipaddr
|
||||||
_, err := p.listenConn.WriteToUDP(packet.my.Data, addr)
|
_, err := p.listenConn.WriteToUDP(packet.my.Data, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
loggo.Info("WriteToUDP Error read udp %s", err)
|
loggo.Info("WriteToUDP Error read udp %s", err)
|
||||||
@ -558,10 +562,8 @@ func (p *Client) processPacket(packet *Packet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Client) close(clientConn *ClientConn) {
|
func (p *Client) close(clientConn *ClientConn) {
|
||||||
if p.localIdToConnMap[clientConn.id] != nil {
|
p.deleteClientConn(clientConn.id, clientConn.ipaddr.String())
|
||||||
delete(p.localIdToConnMap, clientConn.id)
|
p.deleteClientConn(clientConn.id, clientConn.tcpaddr.String())
|
||||||
delete(p.localAddrToConnMap, clientConn.ipaddr.String())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Client) checkTimeoutConn() {
|
func (p *Client) checkTimeoutConn() {
|
||||||
@ -570,8 +572,16 @@ func (p *Client) checkTimeoutConn() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tmp := make(map[string]*ClientConn)
|
||||||
|
p.localIdToConnMap.Range(func(key, value interface{}) bool {
|
||||||
|
id := key.(string)
|
||||||
|
clientConn := value.(*ClientConn)
|
||||||
|
tmp[id] = clientConn
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, conn := range p.localIdToConnMap {
|
for _, conn := range tmp {
|
||||||
diffrecv := now.Sub(conn.activeRecvTime)
|
diffrecv := now.Sub(conn.activeRecvTime)
|
||||||
diffsend := now.Sub(conn.activeSendTime)
|
diffsend := now.Sub(conn.activeSendTime)
|
||||||
if diffrecv > time.Second*(time.Duration(p.timeout)) || diffsend > time.Second*(time.Duration(p.timeout)) {
|
if diffrecv > time.Second*(time.Duration(p.timeout)) || diffsend > time.Second*(time.Duration(p.timeout)) {
|
||||||
@ -579,7 +589,7 @@ func (p *Client) checkTimeoutConn() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for id, conn := range p.localIdToConnMap {
|
for id, conn := range tmp {
|
||||||
if conn.close {
|
if conn.close {
|
||||||
loggo.Info("close inactive conn %s %s", id, conn.ipaddr.String())
|
loggo.Info("close inactive conn %s %s", id, conn.ipaddr.String())
|
||||||
p.close(conn)
|
p.close(conn)
|
||||||
@ -588,7 +598,6 @@ func (p *Client) checkTimeoutConn() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Client) ping() {
|
func (p *Client) ping() {
|
||||||
if p.sendPacket == 0 {
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
b, _ := now.MarshalBinary()
|
b, _ := now.MarshalBinary()
|
||||||
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, "", "", (uint32)(MyMsg_PING), b,
|
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, "", "", (uint32)(MyMsg_PING), b,
|
||||||
@ -597,12 +606,21 @@ func (p *Client) ping() {
|
|||||||
0)
|
0)
|
||||||
loggo.Info("ping %s %s %d %d %d %d", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence)
|
loggo.Info("ping %s %s %d %d %d %d", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence)
|
||||||
p.sequence++
|
p.sequence++
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Client) showNet() {
|
func (p *Client) showNet() {
|
||||||
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s",
|
p.localAddrToConnMapSize = 0
|
||||||
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024)
|
p.localIdToConnMap.Range(func(key, value interface{}) bool {
|
||||||
|
p.localAddrToConnMapSize++
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
p.localIdToConnMapSize = 0
|
||||||
|
p.localIdToConnMap.Range(func(key, value interface{}) bool {
|
||||||
|
p.localIdToConnMapSize++
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s %d/%dConnections",
|
||||||
|
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.localAddrToConnMapSize, p.localIdToConnMapSize)
|
||||||
p.sendPacket = 0
|
p.sendPacket = 0
|
||||||
p.recvPacket = 0
|
p.recvPacket = 0
|
||||||
p.sendPacketSize = 0
|
p.sendPacketSize = 0
|
||||||
@ -640,3 +658,30 @@ func (p *Client) AcceptSock5Conn(conn *net.TCPConn) {
|
|||||||
|
|
||||||
p.AcceptTcpConn(conn, addr)
|
p.AcceptTcpConn(conn, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Client) addClientConn(uuid string, addr string, clientConn *ClientConn) {
|
||||||
|
|
||||||
|
p.localAddrToConnMap.Store(addr, clientConn)
|
||||||
|
p.localIdToConnMap.Store(uuid, clientConn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Client) getClientConnByAddr(addr string) *ClientConn {
|
||||||
|
ret, ok := p.localAddrToConnMap.Load(addr)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return ret.(*ClientConn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Client) getClientConnById(uuid string) *ClientConn {
|
||||||
|
ret, ok := p.localIdToConnMap.Load(uuid)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return ret.(*ClientConn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Client) deleteClientConn(uuid string, addr string) {
|
||||||
|
p.localIdToConnMap.Delete(uuid)
|
||||||
|
p.localAddrToConnMap.Delete(addr)
|
||||||
|
}
|
||||||
|
43
server.go
43
server.go
@ -25,7 +25,7 @@ type Server struct {
|
|||||||
|
|
||||||
conn *icmp.PacketConn
|
conn *icmp.PacketConn
|
||||||
|
|
||||||
localConnMap map[string]*ServerConn
|
localConnMap sync.Map
|
||||||
|
|
||||||
sendPacket uint64
|
sendPacket uint64
|
||||||
recvPacket uint64
|
recvPacket uint64
|
||||||
@ -60,8 +60,6 @@ func (p *Server) Run() error {
|
|||||||
}
|
}
|
||||||
p.conn = conn
|
p.conn = conn
|
||||||
|
|
||||||
p.localConnMap = make(map[string]*ServerConn)
|
|
||||||
|
|
||||||
recv := make(chan *Packet, 10000)
|
recv := make(chan *Packet, 10000)
|
||||||
go recvICMP(&p.workResultLock, &p.exit, *p.conn, recv)
|
go recvICMP(&p.workResultLock, &p.exit, *p.conn, recv)
|
||||||
|
|
||||||
@ -117,7 +115,7 @@ func (p *Server) processPacket(packet *Packet) {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
id := packet.my.Id
|
id := packet.my.Id
|
||||||
localConn := p.localConnMap[id]
|
localConn := p.getServerConnById(id)
|
||||||
if localConn == nil {
|
if localConn == nil {
|
||||||
|
|
||||||
if packet.my.Tcpmode > 0 {
|
if packet.my.Tcpmode > 0 {
|
||||||
@ -138,7 +136,7 @@ func (p *Server) processPacket(packet *Packet) {
|
|||||||
localConn = &ServerConn{timeout: (int)(packet.my.Timeout), tcpconn: targetConn, tcpaddrTarget: ipaddrTarget, id: id, activeRecvTime: now, activeSendTime: now, close: false,
|
localConn = &ServerConn{timeout: (int)(packet.my.Timeout), tcpconn: targetConn, tcpaddrTarget: ipaddrTarget, id: id, activeRecvTime: now, activeSendTime: now, close: false,
|
||||||
rproto: (int)(packet.my.Rproto), fm: fm, tcpmode: (int)(packet.my.Tcpmode)}
|
rproto: (int)(packet.my.Rproto), fm: fm, tcpmode: (int)(packet.my.Tcpmode)}
|
||||||
|
|
||||||
p.localConnMap[id] = localConn
|
p.addServerConn(id, localConn)
|
||||||
|
|
||||||
go p.RecvTCP(localConn, id, packet.src)
|
go p.RecvTCP(localConn, id, packet.src)
|
||||||
|
|
||||||
@ -157,7 +155,7 @@ func (p *Server) processPacket(packet *Packet) {
|
|||||||
localConn = &ServerConn{timeout: (int)(packet.my.Timeout), conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeRecvTime: now, activeSendTime: now, close: false,
|
localConn = &ServerConn{timeout: (int)(packet.my.Timeout), conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeRecvTime: now, activeSendTime: now, close: false,
|
||||||
rproto: (int)(packet.my.Rproto), tcpmode: (int)(packet.my.Tcpmode)}
|
rproto: (int)(packet.my.Rproto), tcpmode: (int)(packet.my.Tcpmode)}
|
||||||
|
|
||||||
p.localConnMap[id] = localConn
|
p.addServerConn(id, localConn)
|
||||||
|
|
||||||
go p.Recv(localConn, id, packet.src)
|
go p.Recv(localConn, id, packet.src)
|
||||||
}
|
}
|
||||||
@ -406,21 +404,29 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Server) close(conn *ServerConn) {
|
func (p *Server) close(conn *ServerConn) {
|
||||||
if p.localConnMap[conn.id] != nil {
|
if p.getServerConnById(conn.id) != nil {
|
||||||
if conn.conn != nil {
|
if conn.conn != nil {
|
||||||
conn.conn.Close()
|
conn.conn.Close()
|
||||||
}
|
}
|
||||||
if conn.tcpconn != nil {
|
if conn.tcpconn != nil {
|
||||||
conn.tcpconn.Close()
|
conn.tcpconn.Close()
|
||||||
}
|
}
|
||||||
delete(p.localConnMap, conn.id)
|
p.deleteServerConn(conn.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Server) checkTimeoutConn() {
|
func (p *Server) checkTimeoutConn() {
|
||||||
|
|
||||||
|
tmp := make(map[string]*ServerConn)
|
||||||
|
p.localConnMap.Range(func(key, value interface{}) bool {
|
||||||
|
id := key.(string)
|
||||||
|
serverConn := value.(*ServerConn)
|
||||||
|
tmp[id] = serverConn
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, conn := range p.localConnMap {
|
for _, conn := range tmp {
|
||||||
if conn.tcpmode > 0 {
|
if conn.tcpmode > 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -431,7 +437,7 @@ func (p *Server) checkTimeoutConn() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for id, conn := range p.localConnMap {
|
for id, conn := range tmp {
|
||||||
if conn.tcpmode > 0 {
|
if conn.tcpmode > 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -450,3 +456,20 @@ func (p *Server) showNet() {
|
|||||||
p.sendPacketSize = 0
|
p.sendPacketSize = 0
|
||||||
p.recvPacketSize = 0
|
p.recvPacketSize = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Server) addServerConn(uuid string, serverConn *ServerConn) {
|
||||||
|
|
||||||
|
p.localConnMap.Store(uuid, serverConn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Server) getServerConnById(uuid string) *ServerConn {
|
||||||
|
ret, ok := p.localConnMap.Load(uuid)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return ret.(*ServerConn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Server) deleteServerConn(uuid string) {
|
||||||
|
p.localConnMap.Delete(uuid)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user