2 Star 19 Fork 4

不稳定丶大神/lucky

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
kcp_server.go 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
火星上的乞丐 提交于 2021-02-20 18:09 . re-named packages
package lucky
import (
"github.com/helloh2o/lucky/log"
"github.com/xtaci/kcp-go"
"net"
"runtime/debug"
"sync"
)
type kcpServer struct {
mu sync.Mutex
addr string
ln net.Listener
processor Processor
}
// NewKcpServer return a *kcpServer
func NewKcpServer(addr string, processor Processor) (s *kcpServer, err error) {
ts := new(kcpServer)
ts.addr = addr
ts.ln, err = kcp.ListenWithOptions(addr, nil, 10, 3)
if processor == nil {
panic("processor must be set.")
}
ts.processor = processor
if err != nil {
return nil, err
}
return ts, err
}
// Run kcp server
func (s *kcpServer) Run() error {
log.Release("Starting kcp server on %s", s.addr)
for {
conn, err := s.ln.Accept()
if err != nil {
return err
}
go s.Handle(conn)
}
}
// Handle goroutine handle connection
func (s *kcpServer) Handle(conn net.Conn) {
defer func() {
if r := recover(); r != nil {
log.Error("PANIC %v TCP handle, stack %s", r, string(debug.Stack()))
}
}()
var ic IConnection
// 可靠的UDP协议, like tcp
ic = NewKcpConn(conn, s.processor)
ic.ReadMsg()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/helloh2o/lucky.git
[email protected]:helloh2o/lucky.git
helloh2o
lucky
lucky
master

搜索帮助