func main() { flag.Parse() if err := InitConfig(); err != nil { panic(err) } log.LoadConfiguration(Conf.Log) runtime.GOMAXPROCS(runtime.NumCPU()) //comet if err := InitComet(Conf.Comets); err != nil { log.Warn("comet rpc current can't connect, retry") } //round round := NewRound(RoundOptions{ Timer: Conf.Timer, TimerSize: Conf.TimerSize, }) //room InitRoomBucket(round, RoomOptions{ BatchNum: Conf.RoomBatch, SignalTime: Conf.RoomSignal, }) //room info MergeRoomServers() go SyncRoomServers() InitPush() if err := InitKafka(); err != nil { panic(err) } // block until a signal is received. InitSignal() }
// auth for goim handshake with client, use rsa & aes. func (server *Server) authTCP(rr *bufio.Reader, wr *bufio.Writer, p *proto.Proto) (key string, rid int32, heartbeat time.Duration, err error) { if err = p.ReadTCP(rr); err != nil { return } if p.Operation != define.OP_AUTH { log.Warn("auth operation not valid: %d", p.Operation) err = ErrOperation return } if key, rid, heartbeat, err = server.operator.Connect(p); err != nil { return } p.Body = nil p.Operation = define.OP_AUTH_REPLY if err = p.WriteTCP(wr); err != nil { return } err = wr.Flush() return }
// expire removes the minimum element (according to Less) from the heap. // The complexity is O(log(n)) where n = max. // It is equivalent to Del(0). func (t *Timer) expire() { var ( fn func() td *TimerData d itime.Duration ) t.lock.Lock() for { if len(t.timers) == 0 { d = infiniteDuration if Debug { log.Debug("timer: no other instance") } break } td = t.timers[0] if d = td.Delay(); d > 0 { break } fn = td.fn // let caller put back t.del(td) t.lock.Unlock() if fn == nil { log.Warn("expire timer no fn") } else { if Debug { log.Debug("timer key: %s, expire: %s, index: %d expired, call fn", td.Key, td.ExpireString(), td.index) } fn() } t.lock.Lock() } t.signal.Reset(d) if Debug { log.Debug("timer: expier reset delay %d ms", int64(d)/int64(itime.Millisecond)) } t.lock.Unlock() return }
func main() { flag.Parse() if err := InitConfig(); err != nil { panic(err) } Debug = Conf.Debug runtime.GOMAXPROCS(Conf.MaxProc) log.LoadConfiguration(Conf.Log) defer log.Close() log.Info("comet[%s] start", Ver) perf.Init(Conf.PprofBind) // init slow log // TODO need to performance optimizition, so stop to use slow log /*if err := initSlowLog(Conf.SlowLog); err != nil { panic(err) }*/ // logic rpc if err := InitLogicRpc(Conf.LogicAddr); err != nil { log.Warn("logic rpc current can't connect, retry") } // new server buckets := make([]*Bucket, Conf.Bucket) for i := 0; i < Conf.Bucket; i++ { buckets[i] = NewBucket(BucketOptions{ ChannelSize: Conf.BucketChannel, RoomSize: Conf.BucketRoom, RoutineAmount: Conf.RoutineAmount, RoutineSize: Conf.RoutineSize, }, RoomOptions{ ChannelSize: Conf.RoomChannel, }) } round := NewRound(RoundOptions{ Reader: Conf.TCPReader, ReadBuf: Conf.TCPReadBuf, ReadBufSize: Conf.TCPReadBufSize, Writer: Conf.TCPWriter, WriteBuf: Conf.TCPWriteBuf, WriteBufSize: Conf.TCPWriteBufSize, Timer: Conf.Timer, TimerSize: Conf.TimerSize, }) operator := new(DefaultOperator) DefaultServer = NewServer(buckets, round, operator, ServerOptions{ CliProto: Conf.CliProto, SvrProto: Conf.SvrProto, HandshakeTimeout: Conf.HandshakeTimeout, TCPKeepalive: Conf.TCPKeepalive, TCPRcvbuf: Conf.TCPRcvbuf, TCPSndbuf: Conf.TCPSndbuf, }) // tcp comet if err := InitTCP(Conf.TCPBind, Conf.MaxProc); err != nil { panic(err) } // websocket comet if err := InitWebsocket(Conf.WebsocketBind); err != nil { panic(err) } // flash safe policy if Conf.FlashPolicyOpen { if err := InitFlashPolicy(); err != nil { panic(err) } } // wss comet if Conf.WebsocketTLSOpen { if err := InitWebsocketWithTLS(Conf.WebsocketTLSBind, Conf.WebsocketCertFile, Conf.WebsocketPrivateFile); err != nil { panic(err) } } // start rpc if err := InitRPCPush(Conf.RPCPushAddrs); err != nil { panic(err) } // block until a signal is received. InitSignal() }