add
This commit is contained in:
parent
05b7ff19aa
commit
a72544515e
52
client.go
52
client.go
@ -15,8 +15,8 @@ const (
|
|||||||
RECV_PROTO int = 0
|
RECV_PROTO int = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewClient(addr string, server string, target string, timeout int, catch int, key int,
|
func NewClient(addr string, server string, target string, timeout int, key int,
|
||||||
tcpmode int) (*Client, error) {
|
tcpmode int, tcpmode_buffersize int, tcpmode_maxwin int, tcpmode_resend_timems int) (*Client, error) {
|
||||||
|
|
||||||
var ipaddr *net.UDPAddr
|
var ipaddr *net.UDPAddr
|
||||||
var tcpaddr *net.TCPAddr
|
var tcpaddr *net.TCPAddr
|
||||||
@ -49,9 +49,11 @@ func NewClient(addr string, server string, target string, timeout int, catch int
|
|||||||
addrServer: server,
|
addrServer: server,
|
||||||
targetAddr: target,
|
targetAddr: target,
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
catch: catch,
|
|
||||||
key: key,
|
key: key,
|
||||||
tcpmode: tcpmode,
|
tcpmode: tcpmode,
|
||||||
|
tcpmode_buffersize: tcpmode_buffersize,
|
||||||
|
tcpmode_maxwin: tcpmode_maxwin,
|
||||||
|
tcpmode_resend_timems: tcpmode_resend_timems,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +64,6 @@ type Client struct {
|
|||||||
timeout int
|
timeout int
|
||||||
sproto int
|
sproto int
|
||||||
rproto int
|
rproto int
|
||||||
catch int
|
|
||||||
key int
|
key int
|
||||||
tcpmode int
|
tcpmode int
|
||||||
tcpmode_buffersize int
|
tcpmode_buffersize int
|
||||||
@ -89,9 +90,6 @@ type Client struct {
|
|||||||
recvPacket uint64
|
recvPacket uint64
|
||||||
sendPacketSize uint64
|
sendPacketSize uint64
|
||||||
recvPacketSize uint64
|
recvPacketSize uint64
|
||||||
|
|
||||||
sendCatchPacket uint64
|
|
||||||
recvCatchPacket uint64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientConn struct {
|
type ClientConn struct {
|
||||||
@ -167,24 +165,12 @@ func (p *Client) Run() {
|
|||||||
interval := time.NewTicker(time.Second)
|
interval := time.NewTicker(time.Second)
|
||||||
defer interval.Stop()
|
defer interval.Stop()
|
||||||
|
|
||||||
inter := 1000
|
|
||||||
if p.catch > 0 {
|
|
||||||
inter = 1000 / p.catch
|
|
||||||
if inter <= 0 {
|
|
||||||
inter = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
intervalCatch := time.NewTicker(time.Millisecond * time.Duration(inter))
|
|
||||||
defer intervalCatch.Stop()
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-interval.C:
|
case <-interval.C:
|
||||||
p.checkTimeoutConn()
|
p.checkTimeoutConn()
|
||||||
p.ping()
|
p.ping()
|
||||||
p.showNet()
|
p.showNet()
|
||||||
case <-intervalCatch.C:
|
|
||||||
p.sendCatch()
|
|
||||||
case r := <-recv:
|
case r := <-recv:
|
||||||
p.processPacket(r)
|
p.processPacket(r)
|
||||||
}
|
}
|
||||||
@ -274,7 +260,7 @@ func (p *Client) AcceptTcpConn(conn *net.TCPConn) {
|
|||||||
p.sendPacketSize += (uint64)(len(mb))
|
p.sendPacketSize += (uint64)(len(mb))
|
||||||
|
|
||||||
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(MyMsg_DATA), mb,
|
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(MyMsg_DATA), mb,
|
||||||
SEND_PROTO, RECV_PROTO, p.catch, p.key,
|
SEND_PROTO, RECV_PROTO, p.key,
|
||||||
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems)
|
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -317,7 +303,7 @@ func (p *Client) Accept() error {
|
|||||||
|
|
||||||
clientConn.activeTime = now
|
clientConn.activeTime = now
|
||||||
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(MyMsg_DATA), bytes[:n],
|
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(MyMsg_DATA), bytes[:n],
|
||||||
SEND_PROTO, RECV_PROTO, p.catch, p.key,
|
SEND_PROTO, RECV_PROTO, p.key,
|
||||||
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems)
|
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems)
|
||||||
|
|
||||||
p.sequence++
|
p.sequence++
|
||||||
@ -362,10 +348,6 @@ func (p *Client) processPacket(packet *Packet) {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
clientConn.activeTime = now
|
clientConn.activeTime = now
|
||||||
|
|
||||||
if packet.my.Type == (int32)(MyMsg_CATCH) {
|
|
||||||
p.recvCatchPacket++
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := p.listenConn.WriteToUDP(packet.my.Data, addr)
|
_, err := p.listenConn.WriteToUDP(packet.my.Data, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
loggo.Error("WriteToUDP Error read udp %s", err)
|
loggo.Error("WriteToUDP Error read udp %s", err)
|
||||||
@ -411,7 +393,7 @@ func (p *Client) ping() {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
b, _ := now.MarshalBinary()
|
b, _ := now.MarshalBinary()
|
||||||
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(MyMsg_PING), b,
|
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(MyMsg_PING), b,
|
||||||
SEND_PROTO, RECV_PROTO, p.catch, p.key,
|
SEND_PROTO, RECV_PROTO, p.key,
|
||||||
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems)
|
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems)
|
||||||
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++
|
||||||
@ -419,24 +401,10 @@ func (p *Client) ping() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Client) showNet() {
|
func (p *Client) showNet() {
|
||||||
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s sendCatch %d/s recvCatch %d/s",
|
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s",
|
||||||
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.sendCatchPacket, p.recvCatchPacket)
|
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024)
|
||||||
p.sendPacket = 0
|
p.sendPacket = 0
|
||||||
p.recvPacket = 0
|
p.recvPacket = 0
|
||||||
p.sendPacketSize = 0
|
p.sendPacketSize = 0
|
||||||
p.recvPacketSize = 0
|
p.recvPacketSize = 0
|
||||||
p.sendCatchPacket = 0
|
|
||||||
p.recvCatchPacket = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Client) sendCatch() {
|
|
||||||
if p.catch > 0 {
|
|
||||||
for _, conn := range p.localIdToConnMap {
|
|
||||||
sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, conn.id, (uint32)(MyMsg_CATCH), make([]byte, 0),
|
|
||||||
SEND_PROTO, RECV_PROTO, p.catch, p.key,
|
|
||||||
p.tcpmode, p.tcpmode_buffersize, p.tcpmode_maxwin, p.tcpmode_resend_timems)
|
|
||||||
p.sequence++
|
|
||||||
p.sendCatchPacket++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package pingtunnel
|
|||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
"github.com/esrrhs/go-engine/src/rbuffergo"
|
"github.com/esrrhs/go-engine/src/rbuffergo"
|
||||||
"math/rand"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -27,7 +26,7 @@ func NewFrameMgr(buffersize int, windowsize int, resend_timems int) *FrameMgr {
|
|||||||
|
|
||||||
fm := &FrameMgr{sendb: sendb, recvb: recvb,
|
fm := &FrameMgr{sendb: sendb, recvb: recvb,
|
||||||
sendlock: &sync.Mutex{}, recvlock: &sync.Mutex{},
|
sendlock: &sync.Mutex{}, recvlock: &sync.Mutex{},
|
||||||
windowsize: windowsize, win: list.New(), sendid: rand.Int() % (FRAME_MAX_ID + 1),
|
windowsize: windowsize, win: list.New(), sendid: 0,
|
||||||
resend_timems: resend_timems, sendlist: list.New()}
|
resend_timems: resend_timems, sendlist: list.New()}
|
||||||
|
|
||||||
return fm
|
return fm
|
||||||
|
85
msg.pb.go
85
msg.pb.go
@ -25,19 +25,16 @@ type MyMsg_TYPE int32
|
|||||||
const (
|
const (
|
||||||
MyMsg_DATA MyMsg_TYPE = 0
|
MyMsg_DATA MyMsg_TYPE = 0
|
||||||
MyMsg_PING MyMsg_TYPE = 1
|
MyMsg_PING MyMsg_TYPE = 1
|
||||||
MyMsg_CATCH MyMsg_TYPE = 2
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var MyMsg_TYPE_name = map[int32]string{
|
var MyMsg_TYPE_name = map[int32]string{
|
||||||
0: "DATA",
|
0: "DATA",
|
||||||
1: "PING",
|
1: "PING",
|
||||||
2: "CATCH",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var MyMsg_TYPE_value = map[string]int32{
|
var MyMsg_TYPE_value = map[string]int32{
|
||||||
"DATA": 0,
|
"DATA": 0,
|
||||||
"PING": 1,
|
"PING": 1,
|
||||||
"CATCH": 2,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x MyMsg_TYPE) String() string {
|
func (x MyMsg_TYPE) String() string {
|
||||||
@ -48,13 +45,40 @@ func (MyMsg_TYPE) EnumDescriptor() ([]byte, []int) {
|
|||||||
return fileDescriptor_c06e4cca6c2cc899, []int{0, 0}
|
return fileDescriptor_c06e4cca6c2cc899, []int{0, 0}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Frame_TYPE int32
|
||||||
|
|
||||||
|
const (
|
||||||
|
Frame_DATA Frame_TYPE = 0
|
||||||
|
Frame_REQ Frame_TYPE = 1
|
||||||
|
Frame_ACK Frame_TYPE = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
var Frame_TYPE_name = map[int32]string{
|
||||||
|
0: "DATA",
|
||||||
|
1: "REQ",
|
||||||
|
2: "ACK",
|
||||||
|
}
|
||||||
|
|
||||||
|
var Frame_TYPE_value = map[string]int32{
|
||||||
|
"DATA": 0,
|
||||||
|
"REQ": 1,
|
||||||
|
"ACK": 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x Frame_TYPE) String() string {
|
||||||
|
return proto.EnumName(Frame_TYPE_name, int32(x))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (Frame_TYPE) EnumDescriptor() ([]byte, []int) {
|
||||||
|
return fileDescriptor_c06e4cca6c2cc899, []int{1, 0}
|
||||||
|
}
|
||||||
|
|
||||||
type MyMsg struct {
|
type MyMsg struct {
|
||||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||||
Type int32 `protobuf:"varint,2,opt,name=type,proto3" json:"type,omitempty"`
|
Type int32 `protobuf:"varint,2,opt,name=type,proto3" json:"type,omitempty"`
|
||||||
Target string `protobuf:"bytes,3,opt,name=target,proto3" json:"target,omitempty"`
|
Target string `protobuf:"bytes,3,opt,name=target,proto3" json:"target,omitempty"`
|
||||||
Data []byte `protobuf:"bytes,4,opt,name=data,proto3" json:"data,omitempty"`
|
Data []byte `protobuf:"bytes,4,opt,name=data,proto3" json:"data,omitempty"`
|
||||||
Rproto int32 `protobuf:"varint,5,opt,name=rproto,proto3" json:"rproto,omitempty"`
|
Rproto int32 `protobuf:"varint,5,opt,name=rproto,proto3" json:"rproto,omitempty"`
|
||||||
Catch int32 `protobuf:"varint,6,opt,name=catch,proto3" json:"catch,omitempty"`
|
|
||||||
Key int32 `protobuf:"varint,7,opt,name=key,proto3" json:"key,omitempty"`
|
Key int32 `protobuf:"varint,7,opt,name=key,proto3" json:"key,omitempty"`
|
||||||
Tcpmode int32 `protobuf:"varint,8,opt,name=tcpmode,proto3" json:"tcpmode,omitempty"`
|
Tcpmode int32 `protobuf:"varint,8,opt,name=tcpmode,proto3" json:"tcpmode,omitempty"`
|
||||||
TcpmodeBuffersize int32 `protobuf:"varint,9,opt,name=tcpmode_buffersize,json=tcpmodeBuffersize,proto3" json:"tcpmode_buffersize,omitempty"`
|
TcpmodeBuffersize int32 `protobuf:"varint,9,opt,name=tcpmode_buffersize,json=tcpmodeBuffersize,proto3" json:"tcpmode_buffersize,omitempty"`
|
||||||
@ -125,13 +149,6 @@ func (m *MyMsg) GetRproto() int32 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MyMsg) GetCatch() int32 {
|
|
||||||
if m != nil {
|
|
||||||
return m.Catch
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MyMsg) GetKey() int32 {
|
func (m *MyMsg) GetKey() int32 {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
return m.Key
|
return m.Key
|
||||||
@ -248,6 +265,7 @@ func (m *Frame) GetDataid() []int32 {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
proto.RegisterEnum("MyMsg_TYPE", MyMsg_TYPE_name, MyMsg_TYPE_value)
|
proto.RegisterEnum("MyMsg_TYPE", MyMsg_TYPE_name, MyMsg_TYPE_value)
|
||||||
|
proto.RegisterEnum("Frame_TYPE", Frame_TYPE_name, Frame_TYPE_value)
|
||||||
proto.RegisterType((*MyMsg)(nil), "MyMsg")
|
proto.RegisterType((*MyMsg)(nil), "MyMsg")
|
||||||
proto.RegisterType((*Frame)(nil), "Frame")
|
proto.RegisterType((*Frame)(nil), "Frame")
|
||||||
}
|
}
|
||||||
@ -255,27 +273,26 @@ func init() {
|
|||||||
func init() { proto.RegisterFile("msg.proto", fileDescriptor_c06e4cca6c2cc899) }
|
func init() { proto.RegisterFile("msg.proto", fileDescriptor_c06e4cca6c2cc899) }
|
||||||
|
|
||||||
var fileDescriptor_c06e4cca6c2cc899 = []byte{
|
var fileDescriptor_c06e4cca6c2cc899 = []byte{
|
||||||
// 339 bytes of a gzipped FileDescriptorProto
|
// 332 bytes of a gzipped FileDescriptorProto
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x51, 0xcb, 0x6e, 0xe2, 0x30,
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x64, 0x51, 0xcd, 0x4a, 0xc3, 0x40,
|
||||||
0x14, 0x1d, 0x27, 0x71, 0x48, 0xee, 0x30, 0x28, 0x73, 0x67, 0x5a, 0x59, 0x5d, 0x45, 0x48, 0x48,
|
0x10, 0x76, 0xf3, 0xd3, 0xa6, 0x63, 0x2d, 0x71, 0x45, 0x19, 0x7a, 0x0a, 0x01, 0xa1, 0x17, 0x3d,
|
||||||
0x6c, 0xda, 0x45, 0xfb, 0x05, 0x40, 0x9f, 0x0b, 0x2a, 0x14, 0x65, 0xd3, 0x6e, 0x50, 0x20, 0x26,
|
0xe8, 0x13, 0xb4, 0x5a, 0x45, 0xa4, 0x52, 0x97, 0x5e, 0xf4, 0x52, 0x52, 0xb3, 0x0d, 0x8b, 0x26,
|
||||||
0xb5, 0xda, 0x84, 0x28, 0x31, 0x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xe9, 0x75, 0x51, 0xc8, 0x8f, 0x45, 0x8b, 0x0e, 0x73, 0xfd, 0x17,
|
||||||
0x5b, 0xf8, 0xb4, 0xca, 0xd5, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xb6, 0x1b, 0xbb, 0x28, 0x14,
|
0x00, 0x00, 0xff, 0xff, 0x6c, 0xfe, 0x5f, 0xc5, 0x17, 0x02, 0x00, 0x00,
|
||||||
0x02, 0x00, 0x00,
|
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ message MyMsg {
|
|||||||
enum TYPE {
|
enum TYPE {
|
||||||
DATA = 0;
|
DATA = 0;
|
||||||
PING = 1;
|
PING = 1;
|
||||||
CATCH = 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string id = 1;
|
string id = 1;
|
||||||
@ -12,7 +11,6 @@ message MyMsg {
|
|||||||
string target = 3;
|
string target = 3;
|
||||||
bytes data = 4;
|
bytes data = 4;
|
||||||
int32 rproto = 5;
|
int32 rproto = 5;
|
||||||
int32 catch = 6;
|
|
||||||
int32 key = 7;
|
int32 key = 7;
|
||||||
int32 tcpmode = 8;
|
int32 tcpmode = 8;
|
||||||
int32 tcpmode_buffersize = 9;
|
int32 tcpmode_buffersize = 9;
|
||||||
@ -21,6 +19,12 @@ message MyMsg {
|
|||||||
}
|
}
|
||||||
|
|
||||||
message Frame {
|
message Frame {
|
||||||
|
enum TYPE {
|
||||||
|
DATA = 0;
|
||||||
|
REQ = 1;
|
||||||
|
ACK = 2;
|
||||||
|
}
|
||||||
|
|
||||||
int32 type = 1;
|
int32 type = 1;
|
||||||
bool resend = 2;
|
bool resend = 2;
|
||||||
int64 sendtime = 3;
|
int64 sendtime = 3;
|
||||||
|
@ -17,7 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, target string,
|
func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, target string,
|
||||||
connId string, msgType uint32, data []byte, sproto int, rproto int, catch int, key int,
|
connId string, msgType uint32, data []byte, sproto int, rproto int, key int,
|
||||||
tcpmode int, tcpmode_buffer_size int, tcpmode_maxwin int, tcpmode_resend_time int) {
|
tcpmode int, tcpmode_buffer_size int, tcpmode_maxwin int, tcpmode_resend_time int) {
|
||||||
|
|
||||||
m := &MyMsg{
|
m := &MyMsg{
|
||||||
@ -26,7 +26,6 @@ func sendICMP(id int, sequence int, conn icmp.PacketConn, server *net.IPAddr, ta
|
|||||||
Target: target,
|
Target: target,
|
||||||
Data: data,
|
Data: data,
|
||||||
Rproto: (int32)(rproto),
|
Rproto: (int32)(rproto),
|
||||||
Catch: (int32)(catch),
|
|
||||||
Key: (int32)(key),
|
Key: (int32)(key),
|
||||||
Tcpmode: (int32)(tcpmode),
|
Tcpmode: (int32)(tcpmode),
|
||||||
TcpmodeBuffersize: (int32)(tcpmode_buffer_size),
|
TcpmodeBuffersize: (int32)(tcpmode_buffer_size),
|
||||||
@ -135,20 +134,7 @@ func GetMd5String(s string) string {
|
|||||||
return hex.EncodeToString(h.Sum(nil))
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
type CatchMsg struct {
|
|
||||||
conn *ServerConn
|
|
||||||
id string
|
|
||||||
src *net.IPAddr
|
|
||||||
data []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FRAME_MAX_SIZE int = 888
|
FRAME_MAX_SIZE int = 888
|
||||||
FRAME_MAX_ID int = 999
|
FRAME_MAX_ID int = 999
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
FRAME_TYPE_DATA int = 0x0101
|
|
||||||
FRAME_TYPE_REQ int = 0x0202
|
|
||||||
FRAME_TYPE_ACK int = 0x0303
|
|
||||||
)
|
|
||||||
|
53
server.go
53
server.go
@ -27,9 +27,6 @@ type Server struct {
|
|||||||
sendPacketSize uint64
|
sendPacketSize uint64
|
||||||
recvPacketSize uint64
|
recvPacketSize uint64
|
||||||
|
|
||||||
sendCatchPacket uint64
|
|
||||||
recvCatchPacket uint64
|
|
||||||
|
|
||||||
echoId int
|
echoId int
|
||||||
echoSeq int
|
echoSeq int
|
||||||
}
|
}
|
||||||
@ -43,8 +40,6 @@ type ServerConn struct {
|
|||||||
activeTime time.Time
|
activeTime time.Time
|
||||||
close bool
|
close bool
|
||||||
rproto int
|
rproto int
|
||||||
catch int
|
|
||||||
catchQueue chan *CatchMsg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Server) Run() {
|
func (p *Server) Run() {
|
||||||
@ -89,7 +84,7 @@ func (p *Server) processPacket(packet *Packet) {
|
|||||||
t.UnmarshalBinary(packet.my.Data)
|
t.UnmarshalBinary(packet.my.Data)
|
||||||
loggo.Info("ping from %s %s %d %d %d", packet.src.String(), t.String(), packet.my.Rproto, packet.echoId, packet.echoSeq)
|
loggo.Info("ping from %s %s %d %d %d", packet.src.String(), t.String(), packet.my.Rproto, packet.echoId, packet.echoSeq)
|
||||||
sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(MyMsg_PING), packet.my.Data,
|
sendICMP(packet.echoId, packet.echoSeq, *p.conn, packet.src, "", "", (uint32)(MyMsg_PING), packet.my.Data,
|
||||||
(int)(packet.my.Rproto), -1, 0, p.key,
|
(int)(packet.my.Rproto), -1, p.key,
|
||||||
0, 0, 0, 0)
|
0, 0, 0, 0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -117,10 +112,8 @@ func (p *Server) processPacket(packet *Packet) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
catchQueue := make(chan *CatchMsg, packet.my.Catch)
|
|
||||||
|
|
||||||
localConn = &ServerConn{tcpconn: targetConn, tcpaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
|
localConn = &ServerConn{tcpconn: targetConn, tcpaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
|
||||||
rproto: (int)(packet.my.Rproto), catchQueue: catchQueue}
|
rproto: (int)(packet.my.Rproto)}
|
||||||
|
|
||||||
p.localConnMap[id] = localConn
|
p.localConnMap[id] = localConn
|
||||||
|
|
||||||
@ -141,10 +134,8 @@ func (p *Server) processPacket(packet *Packet) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
catchQueue := make(chan *CatchMsg, packet.my.Catch)
|
|
||||||
|
|
||||||
localConn = &ServerConn{conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
|
localConn = &ServerConn{conn: targetConn, ipaddrTarget: ipaddrTarget, id: id, activeTime: now, close: false,
|
||||||
rproto: (int)(packet.my.Rproto), catchQueue: catchQueue}
|
rproto: (int)(packet.my.Rproto)}
|
||||||
|
|
||||||
p.localConnMap[id] = localConn
|
p.localConnMap[id] = localConn
|
||||||
|
|
||||||
@ -153,20 +144,6 @@ func (p *Server) processPacket(packet *Packet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
localConn.activeTime = now
|
localConn.activeTime = now
|
||||||
localConn.catch = (int)(packet.my.Catch)
|
|
||||||
|
|
||||||
if packet.my.Type == (int32)(MyMsg_CATCH) {
|
|
||||||
select {
|
|
||||||
case re := <-localConn.catchQueue:
|
|
||||||
sendICMP(packet.echoId, packet.echoSeq, *p.conn, re.src, "", re.id, (uint32)(MyMsg_CATCH), re.data,
|
|
||||||
re.conn.rproto, -1, 0, p.key,
|
|
||||||
0, 0, 0, 0)
|
|
||||||
p.sendCatchPacket++
|
|
||||||
case <-time.After(time.Duration(1) * time.Millisecond):
|
|
||||||
}
|
|
||||||
p.recvCatchPacket++
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if packet.my.Type == (int32)(MyMsg_DATA) {
|
if packet.my.Type == (int32)(MyMsg_DATA) {
|
||||||
|
|
||||||
@ -207,16 +184,9 @@ func (p *Server) RecvTCP(conn *ServerConn, id string, src *net.IPAddr) {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
conn.activeTime = now
|
conn.activeTime = now
|
||||||
|
|
||||||
if conn.catch > 0 {
|
|
||||||
select {
|
|
||||||
case conn.catchQueue <- &CatchMsg{conn: conn, id: id, src: src, data: bytes[:n]}:
|
|
||||||
case <-time.After(time.Duration(10) * time.Millisecond):
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(MyMsg_DATA), bytes[:n],
|
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(MyMsg_DATA), bytes[:n],
|
||||||
conn.rproto, -1, 0, p.key,
|
conn.rproto, -1, p.key,
|
||||||
0, 0, 0, 0)
|
0, 0, 0, 0)
|
||||||
}
|
|
||||||
|
|
||||||
p.sendPacket++
|
p.sendPacket++
|
||||||
p.sendPacketSize += (uint64)(n)
|
p.sendPacketSize += (uint64)(n)
|
||||||
@ -248,16 +218,9 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
conn.activeTime = now
|
conn.activeTime = now
|
||||||
|
|
||||||
if conn.catch > 0 {
|
|
||||||
select {
|
|
||||||
case conn.catchQueue <- &CatchMsg{conn: conn, id: id, src: src, data: bytes[:n]}:
|
|
||||||
case <-time.After(time.Duration(10) * time.Millisecond):
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(MyMsg_DATA), bytes[:n],
|
sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(MyMsg_DATA), bytes[:n],
|
||||||
conn.rproto, -1, 0, p.key,
|
conn.rproto, -1, p.key,
|
||||||
0, 0, 0, 0)
|
0, 0, 0, 0)
|
||||||
}
|
|
||||||
|
|
||||||
p.sendPacket++
|
p.sendPacket++
|
||||||
p.sendPacketSize += (uint64)(n)
|
p.sendPacketSize += (uint64)(n)
|
||||||
@ -290,12 +253,10 @@ func (p *Server) checkTimeoutConn() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Server) showNet() {
|
func (p *Server) showNet() {
|
||||||
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s sendCatch %d/s recvCatch %d/s",
|
loggo.Info("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s",
|
||||||
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.sendCatchPacket, p.recvCatchPacket)
|
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024)
|
||||||
p.sendPacket = 0
|
p.sendPacket = 0
|
||||||
p.recvPacket = 0
|
p.recvPacket = 0
|
||||||
p.sendPacketSize = 0
|
p.sendPacketSize = 0
|
||||||
p.recvPacketSize = 0
|
p.recvPacketSize = 0
|
||||||
p.sendCatchPacket = 0
|
|
||||||
p.recvCatchPacket = 0
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user