mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 19:17:38 +08:00
typo: rename TCPListener to TCPClient
This commit is contained in:
parent
d2fe00e2bc
commit
c0be2d9698
@ -50,7 +50,7 @@ type QQClient struct {
|
|||||||
// protocol public field
|
// protocol public field
|
||||||
SequenceId atomic.Int32
|
SequenceId atomic.Int32
|
||||||
SessionId []byte
|
SessionId []byte
|
||||||
TCP *network.TCPListener // todo: combine other protocol state into one struct
|
TCP *network.TCPClient // todo: combine other protocol state into one struct
|
||||||
ConnectTime time.Time
|
ConnectTime time.Time
|
||||||
|
|
||||||
transport *network.Transport
|
transport *network.Transport
|
||||||
@ -190,7 +190,7 @@ func NewClientMd5(uin int64, passwordMd5 [16]byte) *QQClient {
|
|||||||
Uin: uin,
|
Uin: uin,
|
||||||
PasswordMd5: passwordMd5,
|
PasswordMd5: passwordMd5,
|
||||||
AllowSlider: true,
|
AllowSlider: true,
|
||||||
TCP: &network.TCPListener{},
|
TCP: &network.TCPClient{},
|
||||||
sig: &auth.SigInfo{
|
sig: &auth.SigInfo{
|
||||||
OutPacketSessionID: []byte{0x02, 0xB0, 0x5B, 0x8B},
|
OutPacketSessionID: []byte{0x02, 0xB0, 0x5B, 0x8B},
|
||||||
},
|
},
|
||||||
|
@ -9,32 +9,32 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TCPListener struct {
|
type TCPClient struct {
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
connected bool
|
connected bool
|
||||||
plannedDisconnect func(*TCPListener)
|
plannedDisconnect func(*TCPClient)
|
||||||
unexpectedDisconnect func(*TCPListener, error)
|
unexpectedDisconnect func(*TCPClient, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrConnectionClosed = errors.New("connection closed")
|
var ErrConnectionClosed = errors.New("connection closed")
|
||||||
|
|
||||||
// PlannedDisconnect 预料中的断开连接
|
// PlannedDisconnect 预料中的断开连接
|
||||||
// 如调用 Close() Connect()
|
// 如调用 Close() Connect()
|
||||||
func (t *TCPListener) PlannedDisconnect(f func(*TCPListener)) {
|
func (t *TCPClient) PlannedDisconnect(f func(*TCPClient)) {
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
defer t.lock.Unlock()
|
defer t.lock.Unlock()
|
||||||
t.plannedDisconnect = f
|
t.plannedDisconnect = f
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnexpectedDisconnect 未预料的断开连接
|
// UnexpectedDisconnect 未预料的断开连接
|
||||||
func (t *TCPListener) UnexpectedDisconnect(f func(*TCPListener, error)) {
|
func (t *TCPClient) UnexpectedDisconnect(f func(*TCPClient, error)) {
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
defer t.lock.Unlock()
|
defer t.lock.Unlock()
|
||||||
t.unexpectedDisconnect = f
|
t.unexpectedDisconnect = f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) Connect(addr string) error {
|
func (t *TCPClient) Connect(addr string) error {
|
||||||
t.Close()
|
t.Close()
|
||||||
conn, err := net.Dial("tcp", addr)
|
conn, err := net.Dial("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -47,7 +47,7 @@ func (t *TCPListener) Connect(addr string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) Write(buf []byte) error {
|
func (t *TCPClient) Write(buf []byte) error {
|
||||||
if conn := t.getConn(); conn != nil {
|
if conn := t.getConn(); conn != nil {
|
||||||
_, err := conn.Write(buf)
|
_, err := conn.Write(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -60,7 +60,7 @@ func (t *TCPListener) Write(buf []byte) error {
|
|||||||
return ErrConnectionClosed
|
return ErrConnectionClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) ReadBytes(len int) ([]byte, error) {
|
func (t *TCPClient) ReadBytes(len int) ([]byte, error) {
|
||||||
buf := make([]byte, len)
|
buf := make([]byte, len)
|
||||||
if conn := t.getConn(); conn != nil {
|
if conn := t.getConn(); conn != nil {
|
||||||
_, err := io.ReadFull(conn, buf)
|
_, err := io.ReadFull(conn, buf)
|
||||||
@ -75,7 +75,7 @@ func (t *TCPListener) ReadBytes(len int) ([]byte, error) {
|
|||||||
return nil, ErrConnectionClosed
|
return nil, ErrConnectionClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) ReadInt32() (int32, error) {
|
func (t *TCPClient) ReadInt32() (int32, error) {
|
||||||
b, err := t.ReadBytes(4)
|
b, err := t.ReadBytes(4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -83,17 +83,17 @@ func (t *TCPListener) ReadInt32() (int32, error) {
|
|||||||
return int32(binary.BigEndian.Uint32(b)), nil
|
return int32(binary.BigEndian.Uint32(b)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) Close() {
|
func (t *TCPClient) Close() {
|
||||||
t.close()
|
t.close()
|
||||||
t.invokePlannedDisconnect()
|
t.invokePlannedDisconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) unexpectedClose(err error) {
|
func (t *TCPClient) unexpectedClose(err error) {
|
||||||
t.close()
|
t.close()
|
||||||
t.invokeUnexpectedDisconnect(err)
|
t.invokeUnexpectedDisconnect(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) close() {
|
func (t *TCPClient) close() {
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
defer t.lock.Unlock()
|
defer t.lock.Unlock()
|
||||||
if t.conn != nil {
|
if t.conn != nil {
|
||||||
@ -102,7 +102,7 @@ func (t *TCPListener) close() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) invokePlannedDisconnect() {
|
func (t *TCPClient) invokePlannedDisconnect() {
|
||||||
t.lock.RLock()
|
t.lock.RLock()
|
||||||
defer t.lock.RUnlock()
|
defer t.lock.RUnlock()
|
||||||
if t.plannedDisconnect != nil && t.connected {
|
if t.plannedDisconnect != nil && t.connected {
|
||||||
@ -111,7 +111,7 @@ func (t *TCPListener) invokePlannedDisconnect() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) invokeUnexpectedDisconnect(err error) {
|
func (t *TCPClient) invokeUnexpectedDisconnect(err error) {
|
||||||
t.lock.RLock()
|
t.lock.RLock()
|
||||||
defer t.lock.RUnlock()
|
defer t.lock.RUnlock()
|
||||||
if t.unexpectedDisconnect != nil && t.connected {
|
if t.unexpectedDisconnect != nil && t.connected {
|
||||||
@ -120,7 +120,7 @@ func (t *TCPListener) invokeUnexpectedDisconnect(err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPListener) getConn() net.Conn {
|
func (t *TCPClient) getConn() net.Conn {
|
||||||
t.lock.RLock()
|
t.lock.RLock()
|
||||||
defer t.lock.RUnlock()
|
defer t.lock.RUnlock()
|
||||||
return t.conn
|
return t.conn
|
||||||
|
@ -16,7 +16,7 @@ type Transport struct {
|
|||||||
Device *auth.Device
|
Device *auth.Device
|
||||||
|
|
||||||
// connection
|
// connection
|
||||||
// conn *TCPListener
|
// conn *TCPClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Transport) packBody(req *Request, w *binary.Writer) {
|
func (t *Transport) packBody(req *Request, w *binary.Writer) {
|
||||||
|
@ -253,14 +253,14 @@ func (c *QQClient) sendAndWaitDynamic(seq uint16, pkt []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// plannedDisconnect 计划中断线事件
|
// plannedDisconnect 计划中断线事件
|
||||||
func (c *QQClient) plannedDisconnect(_ *network.TCPListener) {
|
func (c *QQClient) plannedDisconnect(_ *network.TCPClient) {
|
||||||
c.debug("planned disconnect.")
|
c.debug("planned disconnect.")
|
||||||
c.stat.DisconnectTimes.Add(1)
|
c.stat.DisconnectTimes.Add(1)
|
||||||
c.Online.Store(false)
|
c.Online.Store(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// unexpectedDisconnect 非预期断线事件
|
// unexpectedDisconnect 非预期断线事件
|
||||||
func (c *QQClient) unexpectedDisconnect(_ *network.TCPListener, e error) {
|
func (c *QQClient) unexpectedDisconnect(_ *network.TCPClient, e error) {
|
||||||
c.error("unexpected disconnect: %v", e)
|
c.error("unexpected disconnect: %v", e)
|
||||||
c.stat.DisconnectTimes.Add(1)
|
c.stat.DisconnectTimes.Add(1)
|
||||||
c.Online.Store(false)
|
c.Online.Store(false)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user