Ejemplo n.º 1
0
func (ps *PlayerSession) Disconnect() {
	//	ps.mutex.Lock()
	//	defer ps.mutex.Unlock()
	ps.sync <- true
	defer func() { <-ps.sync }()
	ps.conn.Close()
	golog.Debug("PlayerSession", "Writer", "conn closed")
	close(ps.WriteCache)
	golog.Debug("PlayerSession", "Writer", "write cache closed")
}
Ejemplo n.º 2
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
		}
	}
}
Ejemplo n.º 3
0
func (servlet *AccountServlet) doLogin(ps *PlayerSession, pack *protocol.Packet) bool {
	packData := pack.Data.(*protocol.C2S_Login)

	golog.Debug("AccountLogin", "DoRequest", "Test login", "loginId", packData.GetLoginName(), "ts", packData.GetSource())

	ps.SendData([]byte("from account login"))
	return true
}
Ejemplo n.º 4
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()
}
Ejemplo n.º 5
0
func (ps *PlayerSession) Writer() {
	for b := range ps.WriteCache {
		if ps.close == true {
			golog.Debug("PlayerSession", "Writer", "session closed")
			break
		}
		if b == nil {
			golog.Debug("PlayerSession", "Writer", "get bytes nil")
			break
		}
		_, err := ps.conn.Write(b)
		if err != nil {
			golog.Debug("PlayerSession", "Writer", "write error", "err", err)
			break
		}
	}
	ps.Disconnect()
}
Ejemplo n.º 6
0
func (srv *SocketServer) Run() {
	var err error
	srv.listener, err = net.Listen("tcp", srv.address)
	if err != nil {
		golog.Fatal("ServerModule", "run", "Net listen err", "err", err)
	}
	if srv.listener == nil {
		golog.Fatal("ServerModule", "run", "Net listen nil")
	}

	golog.Debug("ServerModule", "run", "listen success", "servername", srv.name, "address", srv.address)

	for {
		conn, err := srv.listener.Accept()
		if err != nil {
			return
		}

		golog.Debug("ServerModule", "run", "client connected", "address", conn.RemoteAddr().String())

		go srv.GoSession(conn)
	}
}
Ejemplo n.º 7
0
func (servlet *AccountLogin) DoRequest(ps *PlayerSession, pack *protocol.Packet) bool {
	packData := pack.Data.(*protocol.C2SLogin)

	golog.Debug("AccountLogin", "DoRequest", "Test login", "loginId", packData.LoginId, "ts", packData.Ts)
	//fmt.Println("do account login ! loginId : ", packData.LoginId, ", ts :", packData.Ts)

	AccessPostgresql(driver.PGPool, packData.LoginId)
	//	golog.Info("AccountLogin", "DoRequest", "Access postgres over", "loginId", packData.LoginId, "ts", packData.Ts)

	AccessMongo(driver.Mgo, packData.LoginId)
	//golog.Info("AccountLogin", "DoRequest", "Access mongo over", "loginId", packData.LoginId, "ts", packData.Ts)

	AccessRedis(packData.LoginId, packData.Ts)
	//      golog.Info("AccountLogin", "DoRequest", "Access redis over", "loginId", packData.LoginId, "ts", packData.Ts, "cnt", rediscount)

	ps.SendData([]byte("from account login"))
	return true
}