Fetch the repository succeeded.
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)
// }
//}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。