func init() { var bytes []byte var err error var lines []string _, err = os.Stat(configFile) if !(err == nil || os.IsExist(err)) { goto Error } bytes, err = ioutil.ReadFile(configFile) if err != nil { goto Error } lines = strings.Split(string(bytes[:]), "\n") for i, v := range lines { strs := strings.Split(v, "= ") if len(strs) == 2 { strs[1] = strings.Replace(strs[1], "\n", "", -1) strs[1] = strings.Replace(strs[1], "\r", "", -1) } else { continue //Logger.Print("333", len(strs), v) //, strs[0], strs[1]) //goto Error } if strings.Contains(v, CourierAddr) { configs[CourierAddr] = strs[1] } else if strings.Contains(v, CourierPort) { configs[CourierPort] = strs[1] } else if strings.Contains(v, WorkerAddr) { configs[WorkerAddr] = strs[1] } else if strings.Contains(v, AdminPort) { configs[AdminPort] = strs[1] } else if strings.Contains(v, MysqlAddr) { configs[MysqlAddr] = strs[1] } else if strings.Contains(v, MongoAddr) { configs[MongoAddr] = strs[1] } Logger.Print("xxx", i, v) } for k, v := range configs { Logger.Print("configs", k, v) } return Error: Logger.Print("Couldn't find config file, file name: config.m") os.Exit(0) }
func NewAgent(addr string, stype uint32, timerName string) (*Client, uint32) { agentClient := newAgentClient(addr) if agentClient == nil { return nil, ErrConf.AGENT_ERR } timerMgr := Timer.NewTimerMgr(int64(time.Second)) timer := timerMgr.NewTimer(timerName, int64(NetConf.HEARTBEART_TIME), func() { agentClient.HeartBeat() }, true) agentClient.AddCloseCB("courierClose", func(client *Client) { Logger.Print("Courier ServerJob", LogConf.SERVER, "xxxxxxxxxx 111111 courierClose") timerMgr.DeleteTimer(timer) go NewAgent(addr, stype, timerName) }) go func() { time.Sleep(time.Second / 10) agentClient.Regist(stype) }() return agentClient, 0 }
func (client *Client) Clear() { Logger.Print("xxxxxxxxxxxxxxxxxxxxxx client clear") for _, cb := range client.cbMap { cb(client) } client.conn.Close() }
func StartTcpServer(addr string, chStop chan uint32) { chAppStop = chStop go func() { tcpAddr, err := net.ResolveTCPAddr("tcp4", addr) listener, err := net.ListenTCP("tcp", tcpAddr) if err != nil { Logger.Error(LogConf.NetCoreServer, LogConf.SERVER, "Listening error: %v", err) os.Exit(-1) } defer listener.Close() serverRunning = true Logger.Info(LogConf.NetCoreServer, LogConf.SERVER, "NetCore Start OK...") for { if !serverRunning { break } conn, err := listener.AcceptTCP() if err != nil { Logger.Print("Error accept:", err.Error()) } else { newClient(conn) } } chAppStop <- ErrConf.TCP_SERVER_ERR }() }
func GetConfig(key string) string { if !strings.Contains(configs[key], "=") { return configs[key] } else { Logger.Print("Couldn't find config key: " + key) } return "" }
func (client *Client) SendMsg(msg *NetMsg) { buf := make([]byte, PACK_HEAD_LEN+len(msg.Buf)) binary.LittleEndian.PutUint32(buf, uint32(len(msg.Buf))) binary.LittleEndian.PutUint32(buf[4:8], msg.Cmd) copy(buf[PACK_HEAD_LEN:], msg.Buf) client.sendQ <- buf Logger.Print("SendMsg %d %d %v", len(msg.Buf), msg.Cmd, msg.Buf) }
func main() { Logger.Print("xxxx config : ", Commen.GetConfig("xxx")) SigHandler.Start() Logger.Start(1, 1, 1) Monitor.Start(Commen.GetConfig(Commen.AdminMonitorAddr), 5, chAppStop) ServerJob.InitHandlers() ServerJob.InitAgent(Commen.GetConfig(Commen.CourierAddr), NetConf.Admin, chAppStop) port, err := strconv.Atoi(Commen.GetConfig(Commen.AdminPort)) if err != nil { Logger.Print("Server Stop, Get Config Port Error:", Commen.GetConfig(Commen.AdminPort), err) os.Exit(0) } HomePage.Start(port) WaitStop() }
func WaitStop() { exitCode := <-chAppStop Monitor.Stop() NetCore.StopTcpServer() Logger.Stop() Logger.Print("Server Stop, exitCode:", exitCode) os.Exit(0) }
func AddMsgHandler(cmd uint32, cb HandlerCB) { Logger.Print("== AddMsgHandler", cmd, cb) handlerMap[cmd] = cb }
func InitHandlers() { Logger.Print("Courier ServerJob", LogConf.SERVER, "ServerAgent init()") }
func InitHandlers() { Logger.Print("Courier ServerJob", LogConf.SERVER, "ServerAgent init()") //NetCore.AddMsgHandler(MsgConf.Courier, handleCourier) NetCore.AddMsgHandler(MsgConf.Regist, handleRegist) }
func InitHandlers() { Logger.Print(LogConf.CourierServerJob, LogConf.SERVER, "Server Handlers InitHandlers()") NetCore.AddMsgHandler(MsgConf.Courier, handleCourier) NetCore.AddMsgHandler(MsgConf.Regist, handleRegist) }