135 lines
3.3 KiB
Go
135 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/aceld/zinx/zutils/commandline/args"
|
|
|
|
"omc/conf"
|
|
|
|
api2 "omc/api"
|
|
"omc/core"
|
|
"omc/db"
|
|
"omc/decoder"
|
|
"omc/dpack"
|
|
"omc/omc"
|
|
"os"
|
|
"os/signal"
|
|
"time"
|
|
|
|
"github.com/aceld/zinx/zconf"
|
|
"github.com/aceld/zinx/ziface"
|
|
"github.com/aceld/zinx/zlog"
|
|
"github.com/aceld/zinx/znet"
|
|
)
|
|
|
|
// OnConnectionAdd 当客户端建立连接的时候的hook函数
|
|
func OnConnectionAdd(conn ziface.IConnection) {
|
|
//创建一个user
|
|
|
|
user := core.NewUser(conn, conn.RemoteAddrString())
|
|
//将当前新上线玩家添加到ChannelManager中
|
|
m := core.GetManager(conn.GetName())
|
|
if m == nil {
|
|
zlog.Ins().ErrorF("server internal error in GetManager")
|
|
conn.Stop()
|
|
return
|
|
}
|
|
m.AddUser(user)
|
|
|
|
//将该连接绑定属性PID
|
|
conn.SetProperty("UID", user.UID)
|
|
|
|
zlog.Ins().InfoF("====> User uID = %s", user.UID, " arrived ====", "")
|
|
}
|
|
|
|
// OnConnectionLost 当客户端断开连接的时候的hook函数
|
|
func OnConnectionLost(conn ziface.IConnection) {
|
|
//获取当前连接的PID属性
|
|
uID, _ := conn.GetProperty("UID")
|
|
var userID string
|
|
if uID != nil {
|
|
userID = uID.(string)
|
|
}
|
|
|
|
//根据pID获取对应usr
|
|
m := core.GetManager(conn.GetName())
|
|
if m == nil {
|
|
zlog.Ins().ErrorF("server internal error in GetManager")
|
|
return
|
|
}
|
|
user := m.GetUserByPID(userID)
|
|
|
|
//触发玩家下线业务
|
|
if user != nil {
|
|
user.LostConnection(m)
|
|
}
|
|
|
|
zlog.Ins().InfoF("====> User %s-%s", user.UID, user.UserName, " left =====")
|
|
|
|
}
|
|
|
|
func main() {
|
|
//配置初始化
|
|
conf.Init(args.Args.ConfigFile)
|
|
db.Init()
|
|
//创建服务器句柄
|
|
for _, cg := range conf.OmcConf.Channel {
|
|
serverName := fmt.Sprintf("omc-tcp-Server-port:%d", cg.TCPPort)
|
|
//注册用户管理模块
|
|
m := core.NewManager(serverName, cg.BindFlag, cg.Province, cg.DeviceCode)
|
|
|
|
//new 一个TCP服务
|
|
s := znet.NewUserConfServer(&zconf.Config{
|
|
TCPPort: cg.TCPPort,
|
|
Name: serverName,
|
|
Host: "0.0.0.0",
|
|
MaxConn: 3000,
|
|
WorkerPoolSize: 10,
|
|
HeartbeatMax: conf.OmcConf.HeartbeatMax,
|
|
LogDir: conf.OmcConf.LogDir,
|
|
LogFile: conf.OmcConf.LogFile,
|
|
})
|
|
|
|
//注册客户端连接建立和丢失函数
|
|
s.SetOnConnStart(OnConnectionAdd)
|
|
s.SetOnConnStop(OnConnectionLost)
|
|
|
|
//注册路由
|
|
s.AddRouter(omc.ReqLoginAlarm, &api2.LoginApi{})
|
|
s.AddRouter(omc.ReqHeartBeat, &api2.HeartBeatApi{})
|
|
s.AddRouter(omc.CloseConnAlarm, &api2.CloseApi{})
|
|
s.AddRouter(omc.ReqSyncAlarmMsg, &api2.SyncAlarmApi{})
|
|
s.AddRouter(omc.ReqSyncAlarmFile, &api2.SyncAlarmFileApi{})
|
|
s.AddRouter(omc.ReqCMCALoginSeq, &api2.SyncAlarmFileApi{})
|
|
s.AddRouter(omc.ReqCMCALoginAlarm, &api2.SyncAlarmFileApi{})
|
|
|
|
//添加LTV数据格式Decoder
|
|
s.SetDecoder(decoder.NewOmcDecoder())
|
|
//添加LTV数据格式的Pack封包Encoder
|
|
s.SetPacket(dpack.NewDataPack())
|
|
|
|
// (启动心跳检测)
|
|
s.StartHeartBeatWithOption(60*time.Second, &ziface.HeartBeatOption{
|
|
MakeMsg: core.MyHeartBeatMsg,
|
|
OnRemoteNotAlive: core.MyOnRemoteNotAlive,
|
|
Router: &api2.HeartBeatApi{},
|
|
HeadBeatMsgID: uint32(0xFF),
|
|
})
|
|
// 设置默认的心跳发送函数
|
|
heart := s.GetHeartBeat()
|
|
heart.SetHeartbeatFunc(core.MyHeartBeat)
|
|
|
|
//启动服务
|
|
go s.Serve()
|
|
|
|
//启动实时告警
|
|
go m.RealTimeAlarm()
|
|
}
|
|
|
|
// close
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt, os.Kill)
|
|
sig := <-c
|
|
zlog.Ins().InfoF("===exit=== %s", sig)
|
|
}
|