1 Star 0 Fork 0

wuzpdev/goworld

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TCPServer.go 2.93 KB
一键复制 编辑 原始数据 按行查看 历史
seis 提交于 2017-10-12 14:12 . fix using Temporary
package netutil
import (
"net"
"time"
"os"
"github.com/xiaonanln/goworld/engine/consts"
"github.com/xiaonanln/goworld/engine/gwioutil"
"github.com/xiaonanln/goworld/engine/gwlog"
)
const (
_RESTART_TCP_SERVER_INTERVAL = 3 * time.Second
_RESTART_UDP_SERVER_INTERVAL = 3 * time.Second
)
// TCPServerDelegate is the implementations that a TCP server should provide
type TCPServerDelegate interface {
ServeTCPConnection(net.Conn)
}
// ServeTCPForever serves on specified address as TCP server, for ever ...
func ServeTCPForever(listenAddr string, delegate TCPServerDelegate) {
for {
err := serveTCPForeverOnce(listenAddr, delegate)
gwlog.Errorf("server@%s failed with error: %v, will restart after %s", listenAddr, err, _RESTART_TCP_SERVER_INTERVAL)
if consts.DEBUG_MODE {
os.Exit(2)
}
time.Sleep(_RESTART_TCP_SERVER_INTERVAL)
}
}
func serveTCPForeverOnce(listenAddr string, delegate TCPServerDelegate) error {
defer func() {
if err := recover(); err != nil {
gwlog.TraceError("serveTCPImpl: paniced with error %s", err)
}
}()
return ServeTCP(listenAddr, delegate)
}
// ServeTCP serves on specified address as TCP server
func ServeTCP(listenAddr string, delegate TCPServerDelegate) error {
ln, err := net.Listen("tcp", listenAddr)
gwlog.Infof("Listening on TCP: %s ...", listenAddr)
if err != nil {
return err
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
if gwioutil.IsTimeoutError(err) {
continue
} else {
return err
}
}
gwlog.Infof("Connection from: %s", conn.RemoteAddr())
go delegate.ServeTCPConnection(conn)
}
}
//// TCPServerDelegate is the implementations that a TCP server should provide
//type UDPServerDelegate interface {
// ServeUDPConnection(conn net.Conn)
//}
//func ServeUDPForever(listenAddr *net.UDPAddr, delegate UDPServerDelegate) error {
// for {
// err := serveUDPForeverOnce(listenAddr, delegate)
// gwlog.Errorf("server@%s failed with error: %v, will restart after %s", listenAddr, err, _RESTART_UDP_SERVER_INTERVAL)
// if consts.DEBUG_MODE {
// os.Exit(2)
// }
// time.Sleep(_RESTART_UDP_SERVER_INTERVAL)
// }
//
//}
//
//func serveUDPForeverOnce(listenAddr *net.UDPAddr, delegate UDPServerDelegate) error {
// defer func() {
// if err := recover(); err != nil {
// gwlog.TraceError("serveUDPImpl: paniced with error %s", err)
// }
// }()
//
// return ServeUDP(listenAddr, delegate)
//}
//
//// ServeUDP serves on specified address as UDP server
//func ServeUDP(listenAddr *net.UDPAddr, delegate UDPServerDelegate) error {
// ln, err := net.ListenUDP("udp", listenAddr)
// gwlog.Infof("Listening on UDP: %s ...", listenAddr.String())
//
// if err != nil {
// return err
// }
//
// defer ln.Close()
//
// for {
// conn, err := ln.()
// if err != nil {
// if IsTemporaryNetError(err) {
// continue
// } else {
// return err
// }
// }
//
// gwlog.Infof("Connection from: %s", conn.RemoteAddr())
// go delegate.ServeUDPConnection(conn)
// }
//}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wuzpdev/goworld.git
git@gitee.com:wuzpdev/goworld.git
wuzpdev
goworld
goworld
v0.1.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385