Exemplo n.º 1
0
func (ps *PlayerSession) Reader() {
	defer func() {
		msg := recover()
		if msg != nil {
			golog.Error("PlayerSession", "Reader", "throw error", "msg", msg)
			ps.close = true
		}
	}()
	for {
		//		golog.Debug("PlayerSession", "Reader", "Cycle reading", "cnt", cnt)
		if ps.close == true {
			golog.Info("PlayerSession", "Reader", "Session closed")
			break
		}

		data, err := ps.readPacket()
		if err != nil {
			if err == io.EOF {
				golog.Debug("PlayerSession", "Reader", "client disconnected",
					"err", err, "addr", ps.conn.RemoteAddr().String())
			} else {
				golog.Error("PlayerSession", "Reader", "Unable to read packet", "err", err)
			}
			ps.close = true
			return
		}
		//if encryption, do decryption
		decrData := ps.decrypt(data)

		if ok := ps.dispatch(decrData); !ok {
			ps.close = true
			return
		}
	}
}
Exemplo n.º 2
0
func (ps *PlayerSession) readPacket() ([]byte, error) {
	defer func() {
		msg := recover()
		if msg != nil {
			golog.Error("PlayerSession", "readerPacket", "throw error", "msg", msg)
			panic(msg)
		}
	}()

	header := []byte{0, 0, 0, 0}
	if _, err := io.ReadFull(ps.conn, header); err != nil {
		return nil, err
		//		return nil, errors.New("Packet error, header invalid")
	}

	//parse header
	length := int(uint32(header[3]) | uint32(header[2])<<8 | uint32(header[1])<<16 | uint32(header[0]<<32))
	if length < 1 {
		return nil, fmt.Errorf("Packet error, length invalid, length = %d", length)
	}

	if length > protocol.PACKET_MAX_SIZE {
		return nil, fmt.Errorf("Packet error, size = %d", length)
	}

	data := make([]byte, length)
	if _, err := io.ReadFull(ps.conn, data); err != nil {
		return nil, fmt.Errorf("Packet error, invalid data, size = %d", length)
	}
	return data, nil
}
Exemplo n.º 3
0
func (ps *PlayerSession) dispatch(data []byte) bool {
	defer func() {
		msg := recover()
		if msg != nil {
			golog.Error("PlayerSession", "dispatch", "throw error", "msg", msg)
			panic(msg)
		}
	}()

	pack := protocol.ReceiveData(data)
	if pack == nil {
		golog.Error("PlayerSession", "dispatch", "Receive no data")
		return false
	}

	return G_dispatcher.Dispatch(ps, pack)
}
Exemplo n.º 4
0
func (disp *Dispatcher) Dispatch(s *PlayerSession, pack *protocol.Packet) bool {
	defer func() {
		msg := recover()
		if msg != nil {
			golog.Error("Dispatcher", "Dispatch", "Dispatch panic", "msg", msg)
			panic(msg)
		}
	}()
	if servlet, ok := disp.register[pack.Cmd]; ok {
		return servlet.DoRequest(s, pack)
	}
	return false
}
Exemplo n.º 5
0
func (srv *SocketServer) GoSession(conn net.Conn) {
	session := NewPlayerSession(conn, srv.timeout)
	/*
		srv.mutex.Lock()
		srv.connPool[conn.RemoteAddr().String()] = connection
		srv.mutex.Unlock()
	*/
	defer func() {
		msg := recover()
		if msg != nil {
			golog.Error("PlayerSession", "readerPacket", "throw error", "msg", msg)
			panic(msg)
		}
	}()

	header := make([]byte, 4)
	_, err := io.ReadFull(conn, header[0:4])
	if err != nil {
		golog.Debug("ServerModule", "GoSession", "conn close! read header failed", "address", conn.RemoteAddr().String())
		conn.Close()
		return
	}
	var flag int32
	buf := bytes.NewBuffer(header)
	binary.Read(buf, binary.LittleEndian, &flag)

	golog.Debug("ServerModule", "GoSession", "connected!", "flag", flag)
	gateState := &protocol.S2C_GateState{
		State: proto.Int32(0),
		Key:   proto.Int64(0),
	}
	data, err := proto.Marshal(gateState)
	if err != nil {
		golog.Debug("ServerModule", "GoSession", "conn close! Marshal gate state failed", "address", conn.RemoteAddr().String())
		conn.Close()
		return
	}
	buffer := new(bytes.Buffer)
	binary.Write(buffer, binary.LittleEndian, len(data)+4)
	buffer.Write([]byte(data))
	conn.Write(buffer.Bytes())

	golog.Debug("ServerModule", "GoSession", "Create player session")
	go session.Writer()
	go session.Reader()
}
Exemplo n.º 6
0
func (ps *PlayerSession) SendData(b []byte) {
	//	session.mutex.Lock()
	//	defer session.mutex.Unlock()
	if b == nil || ps.close == true {
		golog.Error("PlayerSession", "SendData", "Data is nil or session closed")
		return
	}
	/*
		if len(session.WriteCache) != cap(session.WriteCache) {
			golog.Error("PlayerSession", "SendData", "Channel full, close conn")
			session.Disconnect()
			return
		}
	*/

	ps.WriteCache <- b
}
Exemplo n.º 7
0
func AccessPostgresql(db *sql.DB, loginId string) {
	defer func() {
		msg := recover()
		if msg != nil {
			golog.Error("1", "1", "1", "msg", msg)
			panic(msg)
		}
	}()
	/*
		rows, err := db.Query("select loginname,accname from account limit 3") //"SELECT name FROM users WHERE age = $1", age)
		//	var cnt int
		//	err := db.QueryRow("select count(*) from account").Scan(&cnt)
		if err != nil {
			golog.Error("AccessPostgresql", "AccessPostgresql", "Access postgresql query", "err", err)
		}
		//	golog.Info("AccPostgres", "", "", "cnt", cnt, "loginId", loginId)

		defer rows.Close()
		var accname, loginname string
		for rows.Next() {
			err = rows.Scan(&loginname, &accname)
			if err != nil {
				golog.Error("AccessPostgresql", "AccessPostgresql", "Access postgresql scan", "err", err)
			}
			golog.Info("AccessPostgresql", "AccessPostgresql", "Access postgresql over", "loginname", loginname, "accname", accname)
		}
	*/

	var stmt *sql.Stmt
	var err error

	stmt, err = db.Prepare("select loginname,accname from account limit 3")
	if err != nil {
		golog.Error("AccessPostgresql", "AccessPostgresql", "Access postgresql prepare", "err", err)
		return
	}

	var rows *sql.Rows

	rows, err = stmt.Query()
	if err != nil {
		golog.Error("AccessPostgresql", "AccessPostgresql", "Access postgresql query", "err", err)
		return
	}

	defer stmt.Close()

	for rows.Next() {
		var loginname, accname string
		err = rows.Scan(&loginname, &accname)
		if err != nil {
			golog.Error("AccessPostgresql", "AccessPostgresql", "Access postgresql scan", "err", err)
			return
		}

		//			golog.Info("AccessPostgresql", "AccessPostgresql", "Access postgresql over", "loginname", loginname, "accname", accname)
	}
	count++
	if count%1000 == 0 {
		golog.Info("AccessPostgresql", "AccessPostgresql", "Access postgresql over", "count", count)
	}
}
Exemplo n.º 8
0
func AccessRedis(loginId string, ts int32) {
	defer func() {
		msg := recover()
		if msg != nil {
			panic(msg)
		}
	}()
	conn := driver.RedisPool.Get()
	defer conn.Close()

	_, err := conn.Do("MULTI")
	if err != nil {
		golog.Error("AccessRedis", "AccessRedis", "DO MULTI", "msg", err)
		panic(err)
	}

	_, err = conn.Do("hget", "rank_arena_hash", "AI5653f448aff8bc6d00002351")
	if err != nil {
		golog.Error("AccessRedis", "AccessRedis", "DO hgetall", "msg", err)
		panic(err)
	}
	/*
		_, err = conn.Do("lrange", "rank_arena_list", 0, -1)
		if err != nil {
			golog.Error("AccessRedis", "AccessRedis", "Do lrange", "msg", err)
			panic(err)
		}
	*/
	_, err = conn.Do("EXEC")
	if err != nil {
		golog.Error("AccessRedis", "AccessRedis", "Do EXEC", "msg", err)
		panic(err)
	}
	rediscount++
	if rediscount%1000 == 0 {
		golog.Info("AccessRedis", "AccessRedis", "Access redis over", "count", rediscount)
	}

	/*
		results, err := redis.MultiBulk(reply, err)
		if err != nil {
			golog.Error("AccessRedis", "AccessRedis", "multibulk err", "msg", err)
			panic(err)
		}
		if len(results) < 2 {
			golog.Error("AccessRedis", "AccessRedis", "results len error", "len", len(results), "loginid", loginId, "index", ts)
			panic("results array len error")
		}
	*/
	/*
		{
			_, err := redis.IntMap(results[0], err)
				for k, v := range intMap {
					//fmt.Println(k, v)
					//golog.Debug("AccessRedis", "AccessRedis", "hgetall rank_arena_hash", "k", k, "v", v)
				}
			_, err = redis.Strings(results[1], err)
				for _, _ = range strs {
					//fmt.Println("lrange", val)
					//golog.Debug("AccessRedis", "AccessRedis", "lrange rank_arena_list", "val", val)
				}
		}
	*/
}