goProject/trunk/game/common/server_tcp/start.go
2025-01-15 17:36:12 +08:00

56 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package server_tcp
import (
"fmt"
"framework/goroutineMgr"
"net"
"sync"
"goutil/debugUtil"
"common/clientMgr"
"goutil/logUtil"
)
// Start 启动服务器
func Start(wg *sync.WaitGroup, address string) {
defer func() {
wg.Done()
}()
// 处理goroutine数量
goroutineName := "server_tcp.Start"
goroutineMgr.Monitor(goroutineName)
defer goroutineMgr.ReleaseMonitor(goroutineName)
msg := fmt.Sprintf("server_tcp begins to listen on:%s...", address)
fmt.Println(msg)
logUtil.InfoLog(msg)
// 监听指定的端口
listener, err := net.Listen("tcp", address)
//debug模式下打印日志
if debugUtil.IsDebug() {
logUtil.DebugLog(fmt.Sprintf("收到客户的连接请求ip:%v", listener.Addr()))
}
if err != nil {
panic(fmt.Sprintf("server_tcp listen Error: %s", err))
}
for {
// 阻塞直至新连接到来
conn, err := listener.Accept()
if err != nil {
logUtil.ErrorLog("server_tcp accept error: %s", err)
continue
}
// 创建客户端对象
clientObj := newClient(conn)
clientObj.start()
clientMgr.RegisterClient(clientObj)
}
}