Exemple #1
0
func main() {
	flag.Parse()
	if err := InitConfig(); err != nil {
		panic(err)
	}
	runtime.GOMAXPROCS(Conf.MaxProc)
	log.LoadConfiguration(Conf.Log)
	defer log.Close()
	log.Info("logic[%s] start", Ver)
	perf.Init(Conf.PprofAddrs)
	// router rpc
	if err := InitRouter(); err != nil {
		log.Warn("router rpc current can't connect, retry")
	}
	MergeRoomCount()
	go SyncRoomCount()
	// logic rpc
	if err := InitRPC(NewDefaultAuther()); err != nil {
		panic(err)
	}
	if err := InitKafka(Conf.KafkaAddrs); err != nil {
		panic(err)
	}
	if err := InitHTTP(); err != nil {
		panic(err)
	}
	// block until a signal is received.
	InitSignal()
}
Exemple #2
0
func main() {
	flag.Parse()
	if err := InitConfig(); err != nil {
		panic(err)
	}
	runtime.GOMAXPROCS(Conf.MaxProc)
	log.LoadConfiguration(Conf.Log)
	defer log.Close()
	perf.Init(Conf.PprofBind)
	if Conf.Type == ProtoTCP {
		initTCP()
	} else if Conf.Type == ProtoWebsocket {
		initWebsocket()
	}
	time.Sleep(10 * time.Second)
}
Exemple #3
0
func main() {
	flag.Parse()
	if err := InitConfig(); err != nil {
		panic(err)
	}
	runtime.GOMAXPROCS(Conf.MaxProc)
	log.LoadConfiguration(Conf.Log)
	defer log.Close()
	log.Info("comet[%s] start", Ver)
	perf.Init(Conf.PprofBind)
	// 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(Conf.Channel, Conf.Room, Conf.RoomChannel, Conf.CliProto, Conf.SvrProto)
	}
	round := NewRound(Conf.ReadBuf, Conf.WriteBuf, Conf.Timer, Conf.TimerSize)
	operator := new(DefaultOperator)
	DefaultServer = NewServer(buckets, round, operator)
	if err := InitTCP(); err != nil {
		panic(err)
	}
	if err := InitWebsocket(); err != nil {
		panic(err)
	}
	if Conf.WebsocketTLSOpen {
		if err := InitWebsocketWithTLS(); err != nil {
			panic(err)
		}
	}
	if err := InitHTTP(); err != nil {
		panic(err)
	}
	// start rpc
	if err := InitRPCPush(); err != nil {
		panic(err)
	}
	// block until a signal is received.
	InitSignal()
}
Exemple #4
0
func main() {
	flag.Parse()
	if err := InitConfig(); err != nil {
		panic(err)
	}
	runtime.GOMAXPROCS(Conf.MaxProc)
	log.LoadConfiguration(Conf.Log)
	defer log.Close()
	log.Info("comet[%s] start", Ver)
	perf.Init(Conf.PprofBind)
	if err := InitRSA(); err != nil {
		panic(err)
	}
	// new server
	buckets := make([]*Bucket, Conf.Bucket)
	for i := 0; i < Conf.Bucket; i++ {
		buckets[i] = NewBucket(Conf.Channel, Conf.CliProto, Conf.SvrProto)
	}
	round := NewRound(Conf.ReadBuf, Conf.WriteBuf, Conf.Timer, Conf.TimerSize, Conf.Session, Conf.SessionSize)
	codec := new(BinaryServerCodec)
	operator := new(DefaultOperator)
	cryptor := NewDefaultCryptor()
	DefaultServer = NewServer(buckets, round, codec, operator, cryptor)
	if err := InitTCP(); err != nil {
		panic(err)
	}
	if err := InitWebsocket(); err != nil {
		panic(err)
	}
	if err := InitHttpPush(); err != nil {
		panic(err)
	}
	// start rpc
	if err := InitRPCPush(); err != nil {
		panic(err)
	}
	// block until a signal is received.
	InitSignal()
}
Exemple #5
0
func main() {
	flag.Parse()
	if err := InitConfig(); err != nil {
		panic(err)
	}
	runtime.GOMAXPROCS(Conf.MaxProc)
	log.LoadConfiguration(Conf.Log)
	defer log.Close()
	log.Info("router[%s] start", VERSION)
	// start prof
	perf.Init(Conf.PprofBind)
	// start rpc
	buckets := make([]*Bucket, Conf.Bucket)
	for i := 0; i < Conf.Bucket; i++ {
		buckets[i] = NewBucket(Conf.Session, Conf.Server, Conf.Cleaner)
	}
	if err := InitRPC(buckets); err != nil {
		panic(err)
	}
	// block until a signal is received.
	InitSignal()
}
Exemple #6
0
func main() {
	flag.Parse()
	if err := InitConfig(); err != nil {
		panic(err)
	}
	runtime.GOMAXPROCS(Conf.MaxProc)
	log.LoadConfiguration(Conf.Log)
	defer log.Close()
	log.Info("logic[%s] start", Ver)
	perf.Init(Conf.PprofBind)
	if err := InitRouterRpc(Conf.RouterPPCAddrs, Conf.RouterRPCRetry); err != nil {
		panic(err)
	}
	// init http
	if err := InitHTTP(); err != nil {
		panic(err)
	}
	// start rpc
	if err := InitRPC(NewDefaultAuther()); err != nil {
		panic(err)
	}
	// block until a signal is received.
	InitSignal()
}
Exemple #7
0
func main() {
	flag.Parse()
	if err := InitConfig(); err != nil {
		panic(err)
	}
	runtime.GOMAXPROCS(Conf.MaxProc)
	log.LoadConfiguration(Conf.Log)
	defer log.Close()
	perf.Init(Conf.PprofBind)
	if err := InitRSA(); err != nil {
		panic(err)
	}
	conn, err := net.Dial("tcp", Conf.TCPAddr)
	if err != nil {
		log.Error("net.Dial(\"%s\") error(%v)", Conf.TCPAddr, err)
		return
	}
	seqId := int32(0)
	wr := bufio.NewWriter(conn)
	rd := bufio.NewReader(conn)
	proto := new(Proto)
	proto.Ver = 1
	key := make([]byte, 32)
	// handshake
	if _, err = rand.Read(key); err != nil {
		panic(err)
	}
	proto.Operation = OP_HANDSHARE
	proto.SeqId = seqId
	proto.Body = key
	// aes
	var (
		block cipher.Block
	)
	if block, err = aes.NewCipher(key[:16]); err != nil {
		log.Error("aes.NewCipher() error(%v)", err)
		return
	}
	log.Debug("aes key: %x, iv: %x", key[:16], key[16:])
	ebm := cipher.NewCBCEncrypter(block, key[16:])
	dbm := cipher.NewCBCDecrypter(block, key[16:])
	// rsa
	if proto.Body, err = rsa.EncryptOAEP(sha256.New(), rand.Reader, RSAPub, proto.Body, nil); err != nil {
		return
	}
	if err = WriteProto(wr, proto); err != nil {
		log.Error("WriteProto() error(%v)", err)
		return
	}
	if err = ReadProto(rd, proto); err != nil {
		log.Error("ReadProto() error(%v)", err)
		return
	}
	log.Debug("handshake ok, proto: %v", proto)
	dbm.CryptBlocks(proto.Body, proto.Body)
	if proto.Body, err = padding.PKCS7.Unpadding(proto.Body, dbm.BlockSize()); err != nil {
		log.Error("Unpadding() error(%v)", err)
		return
	}
	log.Debug("handshake sessionid : %s", string(proto.Body))
	seqId++
	// auth
	// test handshake timeout
	// time.Sleep(time.Second * 31)
	proto.Operation = OP_AUTH
	proto.SeqId = seqId
	proto.Body = padding.PKCS7.Padding([]byte(Conf.SubKey), ebm.BlockSize())
	// user aes encrypt sub key
	ebm.CryptBlocks(proto.Body, proto.Body)
	if err = WriteProto(wr, proto); err != nil {
		log.Error("WriteProto() error(%v)", err)
		return
	}
	if err = ReadProto(rd, proto); err != nil {
		log.Error("ReadProto() error(%v)", err)
		return
	}
	log.Debug("auth ok, proto: %v", proto)
	seqId++
	// writer
	go func() {
		proto1 := new(Proto)
		for {
			// heartbeat
			proto1.Operation = OP_HEARTBEAT
			proto1.SeqId = seqId
			proto1.Body = nil
			if err = WriteProto(wr, proto1); err != nil {
				log.Error("WriteProto() error(%v)", err)
				return
			}
			// test heartbeat
			//time.Sleep(time.Second * 31)
			seqId++
			// op_test
			proto1.Operation = OP_TEST
			proto1.SeqId = seqId
			// use aes
			proto1.Body = padding.PKCS7.Padding([]byte("hello test"), block.BlockSize())
			ebm.CryptBlocks(proto1.Body, proto1.Body)
			if err = WriteProto(wr, proto1); err != nil {
				log.Error("WriteProto() error(%v)", err)
				return
			}
			seqId++
			time.Sleep(10000 * time.Millisecond)
		}
	}()
	// reader
	for {
		if err = ReadProto(rd, proto); err != nil {
			log.Error("ReadProto() error(%v)", err)
			return
		}
		if proto.Body != nil {
			dbm.CryptBlocks(proto.Body, proto.Body)
		}
		if proto.Operation == OP_HEARTBEAT_REPLY {
			if err = conn.SetReadDeadline(time.Now().Add(25 * time.Second)); err != nil {
				log.Error("conn.SetReadDeadline() error(%v)", err)
				return
			}
			log.Debug("receive heartbeat")
		} else if proto.Operation == OP_TEST_REPLY {
			log.Debug("receive test")
			bodyStr, err := padding.PKCS7.Unpadding(proto.Body, block.BlockSize())
			if err != nil {
				log.Error("pkcs7.Unpadding() error(%v)", err)
				return
			}
			log.Debug("body: %s", bodyStr)
		} else if proto.Operation == OP_SEND_SMS_REPLY {
			log.Debug("receive message")
			bodyStr, err := padding.PKCS7.Unpadding(proto.Body, block.BlockSize())
			if err != nil {
				log.Error("pkcs7.Unpadding() error(%v)", err)
				return
			}
			log.Debug("body: %s", bodyStr)
		}
	}
}
Exemple #8
0
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)
	// 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,
		}, RoomOptions{
			ChannelSize: Conf.RoomChannel,
			BatchNum:    Conf.RoomBatch,
			SignalTime:  Conf.RoomSignal,
		})
	}
	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{
		Proto:            Conf.Proto,
		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)
	}
	// wss comet
	if Conf.WebsocketTLSOpen {
		if err := InitWebsocketWithTLS(Conf.WebsocketTLSBind, Conf.WebsocketCertFile, Conf.WebsocketPrivateFile); err != nil {
			panic(err)
		}
	}
	// http comet
	if err := InitHTTP(Conf.HTTPBind); err != nil {
		panic(err)
	}
	// start rpc
	if err := InitRPCPush(Conf.RPCPushAddrs); err != nil {
		panic(err)
	}
	// block until a signal is received.
	InitSignal()
}