1 Star 0 Fork 0

evan / newtbig

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
connect.go 3.53 KB
一键复制 编辑 原始数据 按行查看 历史
evan 提交于 2024-05-09 01:57 . v0.2.0
// Copyright 2020 newtbig Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tcp
import (
"encoding/binary"
"fmt"
"io"
"net"
"time"
log "gitee.com/lakertt/newtbig/logging"
"gitee.com/lakertt/newtbig/module"
pb "gitee.com/lakertt/newtbig/msg/framepb"
)
func NewConnect(conn net.Conn, opts module.Options) (module.Connect, error) {
c := new(Connect)
err := c.OnInit(conn, opts)
if err != nil {
return nil, err
}
return c, nil
}
type Connect struct {
conn net.Conn
IP net.IP
Port string
header []byte
cache []byte
readDeadline time.Duration
writeDeadline time.Duration
maxPacketSize int64
byteOrder binary.ByteOrder
headSize uint16
isClose bool
}
func (c *Connect) IsClosed() bool {
return c.isClose
}
func (c *Connect) OnClose() {
if !c.isClose {
c.isClose = true
if c.conn != nil {
c.conn.Close()
}
}
}
func (c *Connect) OnInit(_conn net.Conn, opts module.Options) error {
log.Logger.Debugf("Connect OnInit readDeadLine :%d writeDeadLine :%d", opts.ReadDeadline, opts.WriteDeadline)
c.readDeadline = time.Duration(opts.ReadDeadline) * time.Second
c.writeDeadline = time.Duration(opts.WriteDeadline) * time.Second
c.maxPacketSize = opts.MaxPacketSize
c.cache = make([]byte, c.maxPacketSize)
c.headSize = opts.NetHeadSize
c.header = make([]byte, c.headSize)
c.isClose = false
if opts.ByteOrder {
c.byteOrder = binary.BigEndian
} else {
c.byteOrder = binary.LittleEndian
}
c.conn = _conn
host, port, err := net.SplitHostPort(_conn.RemoteAddr().String())
if err != nil {
return err
}
c.IP = net.ParseIP(host)
c.Port = port
return nil
}
func (c *Connect) OnSend(d []byte) error {
if c.isClose {
return fmt.Errorf("connect :%s is close", c.OnString())
}
c.conn.SetWriteDeadline(time.Now().Add(c.writeDeadline))
sz := len(d)
if c.headSize == 2 {
c.byteOrder.PutUint16(c.cache, uint16(sz))
} else {
c.byteOrder.PutUint32(c.cache, uint32(sz))
}
copy(c.cache[c.headSize:], d)
_, err := c.conn.Write(c.cache[:sz+int(c.headSize)])
return err
}
func (c *Connect) OnReceive() ([]byte, error) {
if c.isClose {
return nil, fmt.Errorf("connect :%s is close", c.OnString())
}
c.conn.SetReadDeadline(time.Now().Add(c.readDeadline))
_, err := io.ReadFull(c.conn, c.header)
if err != nil {
log.Logger.Debugf("Connect :%s OnReceive Read head err : %s", c.OnString(), err.Error())
return nil, err
}
var size int
if c.headSize == 2 {
size = int(c.byteOrder.Uint16(c.header))
} else {
size = int(c.byteOrder.Uint32(c.header))
}
payload := make([]byte, size)
_, err = io.ReadFull(c.conn, payload)
if err != nil && err != io.EOF { //errors.Is(err, io.ErrUnexpectedEOF) {
log.Logger.Debugf("Connect %s OnReceive Read data err :%s", c.OnString(), err.Error())
return nil, err
}
return payload, nil
}
func (c *Connect) OnString() string {
return fmt.Sprintf("%s_%s:%s", "tcp", c.IP, c.Port)
}
func (c *Connect) OnSendMsg(d *pb.Msg) error {
return nil
}
func (c *Connect) OnReceiveMsg() (*pb.Msg, error) {
return nil, nil
}
Go
1
https://gitee.com/lakertt/newtbig.git
git@gitee.com:lakertt/newtbig.git
lakertt
newtbig
newtbig
v0.2.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891