4 Star 5 Fork 4

Plato/Service-Box-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
connection.go 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
CloudGuan 提交于 2022-11-15 13:56 . update: 新增导出函数
package net
import (
"gitee.com/dennis-kk/service-box-go/util/errors"
"github.com/panjf2000/gnet"
"net"
"sync/atomic"
)
const (
BoxConnClose = iota
BoxConnConnecting
)
//connection wrapper of gnet connection
type gnetConn struct {
state int32
conn gnet.Conn
}
func (c *gnetConn) LocalAddr() (addr net.Addr) {
if c.IsClose() {
return nil
}
return c.conn.LocalAddr()
}
func (c *gnetConn) RemoteAddr() (addr net.Addr) {
if c.IsClose() {
return nil
}
return c.conn.RemoteAddr()
}
func (c *gnetConn) Read(b []byte) (n int, err error) {
if c == nil {
return 0, errors.ChannelHasClosed
}
if c.IsClose() {
return 0, errors.ChannelHasClosed
}
length, buffer := c.conn.ReadN(len(b))
copy(b, buffer)
c.conn.ShiftN(length)
return length, nil
}
func (c *gnetConn) Write(buf []byte) error {
if c == nil || c.conn == nil {
return errors.ChannelHasClosed
}
if c.IsClose() {
return errors.ChannelHasClosed
}
return c.conn.AsyncWrite(buf)
}
func (c *gnetConn) Close() error {
return c.conn.Close()
}
func (c *gnetConn) IsClose() bool {
return atomic.LoadInt32(&c.state) == BoxConnClose
}
func (c *gnetConn) OnClose() {
atomic.StoreInt32(&c.state, BoxConnClose)
}
func makeGnetConn(v interface{}) IBoxConn {
if c, ok := v.(gnet.Conn); ok {
return &gnetConn{
state: BoxConnConnecting,
conn: c,
}
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dennis-kk/service-box-go.git
git@gitee.com:dennis-kk/service-box-go.git
dennis-kk
service-box-go
Service-Box-go
v0.4.14

搜索帮助