func NewServer(port int) *Server { se := Server{ server: *rpc.NewServer(), port: port, } if err := se.server.Register(&se.classs); err != nil { base.ERR(err) return nil } if err := se.server.Register(NewShutdown(se.Stop)); err != nil { base.ERR(err) return nil } return &se }
func DefaultClientCode(addr string, hand func(code string, v interface{}) error) func(code string, v interface{}) error { ro := route.NewRoute(cfg.Whole.Apps) cod := ro.Code() recod := cod.MakeReCodeMap() base.INFO(recod) c := DefaulClient(addr, func(m1 byte, m2 byte, m3 byte, b []byte) error { c1, c2, c3, err := cod.Map(m1, m2, m3) var code string if err != nil { code = fmt.Sprintf("None.%d.%d.%d", m1, m2, m3) } else { code = c1 + "." + c2 + "." + c3 } es := base.NewEncodeBytes(b) var r interface{} es.DeJson(&r) return hand(code, r) }) return func(code string, v interface{}) error { m1, m2, m3, err := recod.Map(code) if err != nil { base.ERR(err) return err } es := base.EnJson(v) return c(m1, m2, m3, es.Bytes()) } }
func NewConn(addr string) Conn { conn, err := net.Dial("tcp", addr) if err != nil { base.ERR(err) return nil } return NewConnNet(conn) }
func (se *Listener) Start() error { var err error base.NOTICE("Listen start from port", se.port) se.listener, err = net.Listen("tcp", fmt.Sprintf(":%d", se.port)) if err != nil { base.ERR(err) return err } for { if conn, err := se.listener.Accept(); err == nil { go se.Listen(NewConnNet(conn)) } else { base.ERR(err) return err } } base.NOTICE("Listen stop") return nil }
func (se *Server) Start() (err error) { base.NOTICE("Server start from port", se.port) se.listen, err = net.Listen("tcp", fmt.Sprintf(":%d", se.port)) if err != nil { base.ERR(err) return } for { if conn, err := se.listen.Accept(); err == nil { go se.server.ServeConn(conn) //go h.Server.ServeCodec(jsonrpc.NewServerCodec(conn)) } else { base.ERR(err) return err } } base.NOTICE("Server stop") return nil }
func NewServerConfig(path string) *ServerConfig { b, err := ioutil.ReadFile(path) if err != nil { base.ERR(err) return nil } en := base.NewEncodeBytes(b) var conf ServerConfig en.DeJson(&conf) conf.makeId() return &conf }
func NewClient(addr string) *Client { if conn := buf[addr]; conn != nil { return conn } c, err := rpc.Dial("tcp", addr) if err != nil { base.ERR(err) return nil } conn := &Client{ Client: *c, } buf[addr] = conn return conn }
func (se *ServerConfig) Start() { b := fmt.Sprintf("./%s", strings.ToLower(se.Type)) args := []string{} if se.Bin != "" { b = fmt.Sprintf("./%s", se.Bin) } args = append(args, "-id", fmt.Sprintf("%d", se.Id)) cmd := exec.Command(b, args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr base.NOTICE("Start", se.Id, se.Type, b) err := cmd.Start() if err != nil { base.ERR(err) } }
func NewWholeConfig(path string) *WholeConfig { b, err := ioutil.ReadFile(path) if err != nil { base.ERR(err) return nil } en := base.NewEncodeBytes(b) var conf WholeConfig en.DeJson(&conf) for k, _ := range conf.Apps { conf.Apps[k].makeId() } for k, _ := range conf.Agents { conf.Agents[k].makeId() } return &conf }