Compare commits
2 Commits
3964cd05b0
...
f51d2c7137
Author | SHA1 | Date | |
---|---|---|---|
f51d2c7137 | |||
bd262df8ea |
19
client.go
19
client.go
@ -42,6 +42,7 @@ type ClientCommon struct {
|
|||||||
heartbeatPeriod time.Duration
|
heartbeatPeriod time.Duration
|
||||||
wg stario.WaitGroup
|
wg stario.WaitGroup
|
||||||
netType NetType
|
netType NetType
|
||||||
|
showError bool
|
||||||
skipKeyExchange bool
|
skipKeyExchange bool
|
||||||
useHeartBeat bool
|
useHeartBeat bool
|
||||||
sequenceDe func([]byte) (interface{}, error)
|
sequenceDe func([]byte) (interface{}, error)
|
||||||
@ -201,6 +202,12 @@ func (c *ClientCommon) Heartbeat() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ClientCommon) ShowError(std bool) {
|
||||||
|
c.mu.Lock()
|
||||||
|
c.showError = std
|
||||||
|
c.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func (c *ClientCommon) readMessage() {
|
func (c *ClientCommon) readMessage() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -221,7 +228,9 @@ func (c *ClientCommon) readMessage() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("client read error", err)
|
if c.showError {
|
||||||
|
fmt.Println("client read error", err)
|
||||||
|
}
|
||||||
c.alive.Store(false)
|
c.alive.Store(false)
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
c.status = Status{
|
c.status = Status{
|
||||||
@ -269,7 +278,9 @@ func (c *ClientCommon) loadMessage() {
|
|||||||
//transfer to Msg
|
//transfer to Msg
|
||||||
msg, err := c.sequenceDe(c.msgDe(c.SecretKey, data.Msg))
|
msg, err := c.sequenceDe(c.msgDe(c.SecretKey, data.Msg))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("client decode data error", err)
|
if c.showError {
|
||||||
|
fmt.Println("client decode data error", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
message := Message{
|
message := Message{
|
||||||
@ -399,6 +410,8 @@ func (c *ClientCommon) sendWait(msg TransferMsg, timeout time.Duration) (Message
|
|||||||
close(data.Reply)
|
close(data.Reply)
|
||||||
c.noFinSyncMsgPool.Delete(data.TransferMsg.ID)
|
c.noFinSyncMsgPool.Delete(data.TransferMsg.ID)
|
||||||
return Message{}, os.ErrDeadlineExceeded
|
return Message{}, os.ErrDeadlineExceeded
|
||||||
|
case <-c.stopCtx.Done():
|
||||||
|
return Message{}, errors.New("Service shutdown")
|
||||||
case msg, ok := <-data.Reply:
|
case msg, ok := <-data.Reply:
|
||||||
if !ok {
|
if !ok {
|
||||||
return msg, os.ErrInvalid
|
return msg, os.ErrInvalid
|
||||||
@ -420,6 +433,8 @@ func (c *ClientCommon) sendCtx(msg TransferMsg, ctx context.Context) (Message, e
|
|||||||
close(data.Reply)
|
close(data.Reply)
|
||||||
c.noFinSyncMsgPool.Delete(data.TransferMsg.ID)
|
c.noFinSyncMsgPool.Delete(data.TransferMsg.ID)
|
||||||
return Message{}, os.ErrDeadlineExceeded
|
return Message{}, os.ErrDeadlineExceeded
|
||||||
|
case <-c.stopCtx.Done():
|
||||||
|
return Message{}, errors.New("Service shutdown")
|
||||||
case msg, ok := <-data.Reply:
|
case msg, ok := <-data.Reply:
|
||||||
if !ok {
|
if !ok {
|
||||||
return msg, os.ErrInvalid
|
return msg, os.ErrInvalid
|
||||||
|
@ -38,6 +38,7 @@ type Client interface {
|
|||||||
Stop() error
|
Stop() error
|
||||||
StopMonitorChan() <-chan struct{}
|
StopMonitorChan() <-chan struct{}
|
||||||
Status() Status
|
Status() Status
|
||||||
|
ShowError(bool)
|
||||||
|
|
||||||
GetSequenceEn() func(interface{}) ([]byte, error)
|
GetSequenceEn() func(interface{}) ([]byte, error)
|
||||||
SetSequenceEn(func(interface{}) ([]byte, error))
|
SetSequenceEn(func(interface{}) ([]byte, error))
|
||||||
|
20
server.go
20
server.go
@ -41,6 +41,7 @@ type ServerCommon struct {
|
|||||||
maxHeartbeatLostSeconds int64
|
maxHeartbeatLostSeconds int64
|
||||||
sequenceDe func([]byte) (interface{}, error)
|
sequenceDe func([]byte) (interface{}, error)
|
||||||
sequenceEn func(interface{}) ([]byte, error)
|
sequenceEn func(interface{}) ([]byte, error)
|
||||||
|
showError bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() Server {
|
func NewServer() Server {
|
||||||
@ -64,6 +65,11 @@ func NewServer() Server {
|
|||||||
}
|
}
|
||||||
return &server
|
return &server
|
||||||
}
|
}
|
||||||
|
func (s *ServerCommon) ShowError(std bool) {
|
||||||
|
s.mu.Lock()
|
||||||
|
s.showError = std
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func (s *ServerCommon) Stop() error {
|
func (s *ServerCommon) Stop() error {
|
||||||
if !s.alive.Load().(bool) {
|
if !s.alive.Load().(bool) {
|
||||||
@ -184,7 +190,9 @@ func (s *ServerCommon) acceptTU() {
|
|||||||
}
|
}
|
||||||
conn, err := s.listener.Accept()
|
conn, err := s.listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error accept:", err)
|
if s.showError {
|
||||||
|
fmt.Println("error accept:", err)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var id string
|
var id string
|
||||||
@ -274,7 +282,9 @@ func (s *ServerCommon) loadMessage() {
|
|||||||
//fmt.Println("received:", float64(time.Now().UnixNano()-nowd)/1000000)
|
//fmt.Println("received:", float64(time.Now().UnixNano()-nowd)/1000000)
|
||||||
msg, err := s.sequenceDe(cc.msgDe(cc.SecretKey, data.Msg))
|
msg, err := s.sequenceDe(cc.msgDe(cc.SecretKey, data.Msg))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("server decode data error", err)
|
if s.showError {
|
||||||
|
fmt.Println("server decode data error", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//fmt.Println("decoded:", float64(time.Now().UnixNano()-nowd)/1000000)
|
//fmt.Println("decoded:", float64(time.Now().UnixNano()-nowd)/1000000)
|
||||||
@ -294,7 +304,7 @@ func (s *ServerCommon) loadMessage() {
|
|||||||
func (s *ServerCommon) sysMsg(message Message) {
|
func (s *ServerCommon) sysMsg(message Message) {
|
||||||
switch message.Key {
|
switch message.Key {
|
||||||
case "bye":
|
case "bye":
|
||||||
fmt.Println("recv stop signal from client", message.ClientConn.ClientID)
|
//fmt.Println("recv stop signal from client", message.ClientConn.ClientID)
|
||||||
if message.TransferMsg.Type == MSG_SYS_WAIT {
|
if message.TransferMsg.Type == MSG_SYS_WAIT {
|
||||||
message.Reply(nil)
|
message.Reply(nil)
|
||||||
}
|
}
|
||||||
@ -405,6 +415,8 @@ func (s *ServerCommon) sendWait(c *ClientConn, msg TransferMsg, timeout time.Dur
|
|||||||
close(data.Reply)
|
close(data.Reply)
|
||||||
s.noFinSyncMsgPool.Delete(data.TransferMsg.ID)
|
s.noFinSyncMsgPool.Delete(data.TransferMsg.ID)
|
||||||
return Message{}, os.ErrDeadlineExceeded
|
return Message{}, os.ErrDeadlineExceeded
|
||||||
|
case <-s.stopCtx.Done():
|
||||||
|
return Message{}, errors.New("Service shutdown")
|
||||||
case msg, ok := <-data.Reply:
|
case msg, ok := <-data.Reply:
|
||||||
if !ok {
|
if !ok {
|
||||||
return msg, os.ErrInvalid
|
return msg, os.ErrInvalid
|
||||||
@ -434,6 +446,8 @@ func (s *ServerCommon) sendCtx(c *ClientConn, msg TransferMsg, ctx context.Conte
|
|||||||
close(data.Reply)
|
close(data.Reply)
|
||||||
s.noFinSyncMsgPool.Delete(data.TransferMsg.ID)
|
s.noFinSyncMsgPool.Delete(data.TransferMsg.ID)
|
||||||
return Message{}, os.ErrClosed
|
return Message{}, os.ErrClosed
|
||||||
|
case <-s.stopCtx.Done():
|
||||||
|
return Message{}, errors.New("Service shutdown")
|
||||||
case msg, ok := <-data.Reply:
|
case msg, ok := <-data.Reply:
|
||||||
if !ok {
|
if !ok {
|
||||||
return msg, os.ErrInvalid
|
return msg, os.ErrInvalid
|
||||||
|
@ -40,6 +40,7 @@ type Server interface {
|
|||||||
SetSequenceEn(func(interface{}) ([]byte, error))
|
SetSequenceEn(func(interface{}) ([]byte, error))
|
||||||
GetSequenceDe() func([]byte) (interface{}, error)
|
GetSequenceDe() func([]byte) (interface{}, error)
|
||||||
SetSequenceDe(func([]byte) (interface{}, error))
|
SetSequenceDe(func([]byte) (interface{}, error))
|
||||||
|
ShowError(bool)
|
||||||
|
|
||||||
HeartbeatTimeoutSec() int64
|
HeartbeatTimeoutSec() int64
|
||||||
SetHeartbeatTimeoutSec(int64)
|
SetHeartbeatTimeoutSec(int64)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user