Exemple #1
0
func CreateHandler(user *User, data []byte) {

	// request body unmarshaling
	req := new(gs_protocol.ReqCreate)
	err := proto.Unmarshal(data, req)
	gs.CheckError(err)

	if user.userID != req.GetUserID() {
		if DEBUG {
			gs.Log("Fail room create, user id missmatch")
		}
		return
	}

	// room create
	roomID := GetRandomRoomID()
	r := NewRoom(roomID)
	r.users.Set(user.userID, user) // insert user
	user.room = r                  // set room
	rooms.Set(roomID, r)           // set room into global shared map
	if DEBUG {
		gs.Log("Get rand room id : ", gs.Itoa64(roomID))
	}
	// response body marshaling
	res := new(gs_protocol.ResCreate)
	res.RoomID = proto.Int64(roomID)
	res.UserID = proto.Int64(user.userID)

	if DEBUG {
		gs.Log("Room create, room id : ", gs.Itoa64(roomID))
	}
	msg, err := proto.Marshal(res)
	gs.CheckError(err)
	user.Push(NewMessage(user.userID, gs_protocol.Type_Create, msg))
}
Exemple #2
0
func Action1Handler(user *User, data []byte) {

	// request body unmarshaling
	req := new(gs_protocol.ReqAction1)
	err := proto.Unmarshal(data, req)
	gs.CheckError(err)

	// TODO create business logic for Action1 Type
	if DEBUG {
		gs.Log("Action1 userID : ", gs.Itoa64(req.GetUserID()))
	}

	// broadcast message
	notifyMsg := new(gs_protocol.NotifyAction1Msg)
	notifyMsg.UserID = proto.Int64(user.userID)
	msg, err := proto.Marshal(notifyMsg)
	gs.CheckError(err)

	user.SendToAll(NewMessage(user.userID, gs_protocol.Type_NotifyAction1, msg))

	// response body marshaling
	res := new(gs_protocol.ResAction1)
	res.UserID = proto.Int64(user.userID)
	res.Result = proto.Int32(1) // is success?
	msg, err = proto.Marshal(res)
	gs.CheckError(err)
	user.Push(NewMessage(user.userID, gs_protocol.Type_DefinedAction1, msg))
}
Exemple #3
0
func RoomListHandler(user *User, data []byte) {
	// request body unmarshaling
	req := new(gs_protocol.ReqRoomList)
	err := proto.Unmarshal(data, req)
	gs.CheckError(err)

	res := new(gs_protocol.ResRoomList)
	res.RoomIDs = rooms.GetKeys()
	msg, err := proto.Marshal(res)
	gs.CheckError(err)
	user.Push(NewMessage(user.userID, gs_protocol.Type_RoomList, msg))
}
Exemple #4
0
func NotifyJoinHandler(rawData []byte) {
	res := new(gs_protocol.NotifyJoinMsg)
	err := proto.Unmarshal(rawData, res)
	gs.CheckError(err)
	fmt.Println("server notify return : user id : ", res.GetUserID())
	fmt.Println("server notify return : room id : ", res.GetRoomID())
}
Exemple #5
0
func QuitHandler(user *User, data []byte) {

	// request body unmarshaling
	req := new(gs_protocol.ReqQuit)
	err := proto.Unmarshal(data, req)
	gs.CheckError(err)

	res := new(gs_protocol.ResQuit)
	res.IsSuccess = proto.Int32(1) // is success?
	msg, err := proto.Marshal(res)
	gs.CheckError(err)
	user.Push(NewMessage(user.userID, gs_protocol.Type_Quit, msg))

	// same act user.Leave()
	user.exit <- struct{}{}
}
Exemple #6
0
func ResRoomList(rawData []byte) {

	res := new(gs_protocol.ResRoomList)
	err := proto.Unmarshal(rawData, res)
	gs.CheckError(err)

	fmt.Printf("server return : room list : %v", res.GetRoomIDs())
}
Exemple #7
0
func ResLogin(rawData []byte) {

	res := new(gs_protocol.ResLogin)
	err := proto.Unmarshal(rawData, res)
	gs.CheckError(err)
	fmt.Println("server return : user id", res.GetUserID())
	fmt.Println("server return : result", res.GetResult())
}
Exemple #8
0
func ResCreate(rawData []byte) {

	res := new(gs_protocol.ResCreate)
	err := proto.Unmarshal(rawData, res)
	gs.CheckError(err)

	fmt.Println("server return : user id", res.GetUserID())
	fmt.Println("server return : room id", res.GetRoomID())
}
Exemple #9
0
func ResJoin(rawData []byte) {

	res := new(gs_protocol.ResJoin)
	err := proto.Unmarshal(rawData, res)
	gs.CheckError(err)

	fmt.Println("server return : user id", res.GetUserID())
	fmt.Println("server return : room id", res.GetRoomID())
	fmt.Printf("server return : members %v", res.GetMembers())
}
Exemple #10
0
func LoginHandler(user *User, data []byte) {

	// request body unmarshaling
	req := new(gs_protocol.ReqLogin)
	err := proto.Unmarshal(data, req)
	gs.CheckError(err)
	user.userID = req.GetUserID()

	// TODO validation logic here

	// response body marshaling
	res := new(gs_protocol.ResLogin)
	res.Result = proto.Int32(1)
	res.UserID = proto.Int64(user.userID)

	msg, err := proto.Marshal(res)
	gs.CheckError(err)
	user.recv <- NewMessage(user.userID, gs_protocol.Type_Login, msg)
}
Exemple #11
0
func JoinHandler(user *User, data []byte) {

	// request body unmarshaling
	req := new(gs_protocol.ReqJoin)
	err := proto.Unmarshal(data, req)
	gs.CheckError(err)

	roomID := req.GetRoomID()

	value, ok := rooms.Get(roomID)

	if !ok {
		if DEBUG {
			gs.Log("Fail room join, room does not exist, room id : ", gs.Itoa64(roomID))
		}
		return
	}

	r := value.(*Room)
	r.users.Set(user.userID, user)
	user.room = r

	// broadcast message
	notifyMsg := new(gs_protocol.NotifyJoinMsg)
	notifyMsg.UserID = proto.Int64(user.userID)
	notifyMsg.RoomID = proto.Int64(roomID)
	msg, err := proto.Marshal(notifyMsg)
	gs.CheckError(err)

	user.SendToAll(NewMessage(user.userID, gs_protocol.Type_NotifyJoin, msg))

	// response body marshaling
	res := new(gs_protocol.ResJoin)
	res.UserID = proto.Int64(user.userID)
	res.RoomID = proto.Int64(roomID)
	res.Members = r.getRoomUsers()

	msg, err = proto.Marshal(res)
	gs.CheckError(err)
	user.Push(NewMessage(user.userID, gs_protocol.Type_Join, msg))
}
Exemple #12
0
func ReqQuit(c net.Conn, userUID int64, data []byte) {
	req := new(gs_protocol.ReqQuit)
	req.UserID = proto.Int64(userUID)
	msgTypeBytes := gs.WriteMsgType(gs_protocol.Type_Quit)
	msg, err := proto.Marshal(req)
	gs.CheckError(err)
	data = append(msgTypeBytes, msg...)

	_, err = c.Write(data)
	if err != nil {
		fmt.Println(err)
		return
	}

	gs.Logf("client send : %v\n", req)
}
Exemple #13
0
func ResQuit(rawData []byte) {
	res := new(gs_protocol.ResQuit)
	err := proto.Unmarshal(rawData, res)
	gs.CheckError(err)
	fmt.Println("server return : user id : ", res.GetIsSuccess())
}