Ejemplo n.º 1
0
func StartServer() {
	iniConf := common.Config()
	ip := iniConf.DefaultString("tcp_ip", "10.0.2.15")
	port := iniConf.DefaultString("tcp_port", "8888")
	g_IP = util.IPToInt(ip)
	g_Port = uint32(util.Atoi(port))

	listener, err := common.TCPServer(ip + ":" + port)
	if err != nil {
		panic(err)
	}
	/*
		udp_addr1 := iniConf.DefaultString("udp_address1", "127.0.0.1:9100")
		udp_addr2 := iniConf.DefaultString("udp_address2", "127.0.0.1:9101")

		udpSocket1, err := common.UDPServer(udp_addr1)
		if err != nil {
			panic(err)
		}
		udpSocket2, err := common.UDPServer(udp_addr2)
		if err != nil {
			panic(err)
		}
	*/
	service := &Service{
		Listener:  listener,
		ClientCh:  make(chan *Client, 1000),
		WaitGroup: sync_.NewWaitGroup(),
		ClientMap: make(map[uint32]*Client),
		RWMutex:   &sync.RWMutex{},
	}
	for i := 0; i < CLIENT_GROUP_NUM; i++ {
		NewClientGroup(service)
	}
	go service.Serve()
	//go service.P2pService(udpSocket1)
	//go service.P2pService(udpSocket2)

	topic := fmt.Sprintf("conn_%v_%v", ip, port)
	if _, err := common.NsqConsumerGO(topic, "conn-channel", 3, service); err != nil {
		panic(err)
	}
	// Handle SIGINT and SIGTERM.
	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
	syslog.Info("recv signal and exit:", <-ch)
	service.Stop()
	return
}
Ejemplo n.º 2
0
func NewSocket() *Socket {
	socket := &Socket{
		svrAddr:        nil,
		socket:         nil,
		sendQueue:      make(chan []byte, 1000),
		connectChan:    make(chan struct{}, 1),
		recvHandle:     safemap.NewMap(),
		callbackHandle: safemap.NewMap(),
		waitGroup:      sync.NewWaitGroup(),
	}
	go socket.send()
	go socket.recv()
	go socket.heartbeat()
	return socket
}
Ejemplo n.º 3
0
func QiniuInit() error {
	c := common.MongoCollection(FILE_DB, FILE_QINIU_FILE_TABLE)

	index := mgo.Index{
		Key:      []string{"filename"},
		Unique:   true,
		DropDups: true,
	}

	g_file_mgr = &FileMgr{
		fileCh:       make(chan *File, 100),
		expiredTimer: time.Tick(time.Hour),
		waitGroup:    sync.NewWaitGroup(),
		rsClient:     rs.New(&digest.Mac{AccessKey: g_access_key, SecretKey: []byte(g_secret_key)}),
	}

	go g_file_mgr.run()

	return errors.As(c.EnsureIndex(index))
}