|
|
@ -5,7 +5,6 @@ import (
|
|
|
|
"git.noahlan.cn/noahlan/nnet/conn"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/conn"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/packet"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/packet"
|
|
|
|
"git.noahlan.cn/noahlan/ntool/nlog"
|
|
|
|
"git.noahlan.cn/noahlan/ntool/nlog"
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var ErrEventTypeIllegal = errors.New("EventType illegal")
|
|
|
|
var ErrEventTypeIllegal = errors.New("EventType illegal")
|
|
|
@ -65,8 +64,6 @@ type (
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
eventManager struct {
|
|
|
|
eventManager struct {
|
|
|
|
pool *ants.Pool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onConnected []OnConnectedFn
|
|
|
|
onConnected []OnConnectedFn
|
|
|
|
onConnectError []OnConnectErrorFn
|
|
|
|
onConnectError []OnConnectErrorFn
|
|
|
|
onDisconnected []OnDisconnectedFn
|
|
|
|
onDisconnected []OnDisconnectedFn
|
|
|
@ -82,9 +79,8 @@ type (
|
|
|
|
///////////////// type-align
|
|
|
|
///////////////// type-align
|
|
|
|
var _ Event = (*eventManager)(nil)
|
|
|
|
var _ Event = (*eventManager)(nil)
|
|
|
|
|
|
|
|
|
|
|
|
func NewEventManager(pool *ants.Pool) EventManager {
|
|
|
|
func NewEventManager() EventManager {
|
|
|
|
return &eventManager{
|
|
|
|
return &eventManager{
|
|
|
|
pool: pool,
|
|
|
|
|
|
|
|
onConnected: make([]OnConnectedFn, 0),
|
|
|
|
onConnected: make([]OnConnectedFn, 0),
|
|
|
|
onConnectError: make([]OnConnectErrorFn, 0),
|
|
|
|
onConnectError: make([]OnConnectErrorFn, 0),
|
|
|
|
onDisconnected: make([]OnDisconnectedFn, 0),
|
|
|
|
onDisconnected: make([]OnDisconnectedFn, 0),
|
|
|
@ -228,9 +224,7 @@ func (m *eventManager) OnConnected(nc *conn.Connection) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fn := range m.onConnected {
|
|
|
|
for _, fn := range m.onConnected {
|
|
|
|
_ = m.pool.Submit(func() {
|
|
|
|
|
|
|
|
fn(nc)
|
|
|
|
fn(nc)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -239,9 +233,7 @@ func (m *eventManager) OnConnectError(err error) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fn := range m.onConnectError {
|
|
|
|
for _, fn := range m.onConnectError {
|
|
|
|
_ = m.pool.Submit(func() {
|
|
|
|
|
|
|
|
fn(err)
|
|
|
|
fn(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -250,9 +242,7 @@ func (m *eventManager) OnDisconnected(nc *conn.Connection, err error) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fn := range m.onDisconnected {
|
|
|
|
for _, fn := range m.onDisconnected {
|
|
|
|
_ = m.pool.Submit(func() {
|
|
|
|
|
|
|
|
fn(nc, err)
|
|
|
|
fn(nc, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -261,9 +251,7 @@ func (m *eventManager) OnClose(nc *conn.Connection) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fn := range m.onClose {
|
|
|
|
for _, fn := range m.onClose {
|
|
|
|
_ = m.pool.Submit(func() {
|
|
|
|
|
|
|
|
fn(nc)
|
|
|
|
fn(nc)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -272,9 +260,7 @@ func (m *eventManager) OnSend(nc *conn.Connection, v any) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fn := range m.onSend {
|
|
|
|
for _, fn := range m.onSend {
|
|
|
|
_ = m.pool.Submit(func() {
|
|
|
|
|
|
|
|
fn(nc, v)
|
|
|
|
fn(nc, v)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -283,9 +269,7 @@ func (m *eventManager) OnSendError(nc *conn.Connection, v any, err error) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fn := range m.onSendError {
|
|
|
|
for _, fn := range m.onSendError {
|
|
|
|
_ = m.pool.Submit(func() {
|
|
|
|
|
|
|
|
fn(nc, v, err)
|
|
|
|
fn(nc, v, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -294,9 +278,7 @@ func (m *eventManager) OnReceive(nc *conn.Connection, p packet.IPacket) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fn := range m.onReceive {
|
|
|
|
for _, fn := range m.onReceive {
|
|
|
|
_ = m.pool.Submit(func() {
|
|
|
|
|
|
|
|
fn(nc, p)
|
|
|
|
fn(nc, p)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -305,8 +287,6 @@ func (m *eventManager) OnReceiveError(nc *conn.Connection, err error) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, fn := range m.onReceiveError {
|
|
|
|
for _, fn := range m.onReceiveError {
|
|
|
|
_ = m.pool.Submit(func() {
|
|
|
|
|
|
|
|
fn(nc, err)
|
|
|
|
fn(nc, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|