Example #1
0
File: Room.go Project: cokeboL/ZIM
func handleClientLeaveRoom(client *NetCore.Client, msg *NetCore.NetMsg) bool {
	client.RemoveCloseCB("leaveRoom")

	msg.Buf = ErrConf.GetEmptyRspBuffer(ErrConf.NO_ERR)
	client.SendMsg(msg)

	if room, ok := idRoomMap[client.Id]; ok {
		Logger.Info(LogConf.WorkerClientJob, client.Idx, "client leave room id: %d RoomId: %d", client.Id, room.Id)

		notify := &LeaveRoomNotify{
			Id: NetCore.ClientIDType(client.Id),
		}
		msg.Cmd = MsgConf.ClientLeaveRoomNotify
		if buf, err := json.Marshal(notify); err == nil {
			msg.Buf = buf
		} else {
			return false
		}

		delete(room.Guests, client.Id)

		if len(room.Guests) > 0 {
			for _, v := range room.Guests {
				v.SendMsg(msg)
			}
		} else {
			delete(rooms, room.Id)
		}
	}

	Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientLeaveRoom client Idx: %d id: %d", client.Idx, client.Id)

	return true
}
Example #2
0
func handleClientRegist(client *NetCore.Client, msg *NetCore.NetMsg) bool {
	var (
		req ClientRegistReq
	)

	if err := json.Unmarshal([]byte(msg.Buf), &req); err != nil {
		return false
	}

	id := req.Id

	client.CancelWaitRegist()

	msg.Buf = ErrConf.GetEmptyRspBuffer(ErrConf.NO_ERR)
	client.SendMsg(msg)

	NetCore.AddClientToManager(client, NetCore.ClientIDType(id))

	Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientRegist client Idx: %d id: %d", client.Idx, id)

	return true
}
Example #3
0
File: Room.go Project: cokeboL/ZIM
//要把房间user列表回复给它
func handleClientEnterRoom(client *NetCore.Client, msg *NetCore.NetMsg) bool {
	var (
		req  EnterRoomReq
		rsp  = &EnterRoomRsp{Ret: 0}
		room *Room
		ok   bool
	)
	Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom 000")

	enterRoomRsp := func() {
		Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom aaa %d", rsp.Ret)
		if rsp.Ret == 0 {
			rsp.Guests = make([]NetCore.ClientIDType, len(room.Guests)+1)
			rsp.Guests[0] = NetCore.ClientIDType(client.Id)
			Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom bbb")

			n := 1
			for id, _ := range room.Guests {
				Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom ccc %d %d", id, n)
				rsp.Guests[n] = NetCore.ClientIDType(id)
				n = n + 1
				Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom ddd %d %d", id, len(rsp.Guests))
			}
		}
		Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom ddd")
		if buf, err := json.Marshal(rsp); err == nil {
			rspMsg := &NetCore.NetMsg{
				Cmd: msg.Cmd,
				Buf: buf,
			}
			client.SendMsg(rspMsg)

			Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom client Idx: %d id: %d RoomId: %d", client.Idx, client.Id, req.RoomId)
		} else {
			Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom xxxx")
		}
	}

	if err := json.Unmarshal([]byte(msg.Buf), &req); err != nil {
		rsp.Ret = ErrConf.INVALID_JSON
		enterRoomRsp()
		return false
	}

	if room, ok = rooms[req.RoomId]; ok {
		if _, ok2 := room.Guests[client.Id]; ok2 {
			rsp.Ret = ErrConf.ID_HAS_BEEN_IN_ROOM
			enterRoomRsp()
			Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom 111")
			return true
		}

		if req.Passwd != room.Passwd {
			rsp.Ret = ErrConf.ERROR_PASSWD_FOR_ROOM
			enterRoomRsp()
			Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom 111")
			return true
		}

		Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom 222")
		notify := &EnterRoomNotify{
			Id: NetCore.ClientIDType(client.Id),
		}
		notifyMsg := &NetCore.NetMsg{
			Cmd: MsgConf.ClientEnterRoomNotify,
		}
		if buf, err := json.Marshal(notify); err == nil {
			notifyMsg.Buf = buf
		} else {
			Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom 333")
			return false
		}

		for _, v := range room.Guests {
			v.SendMsg(notifyMsg)
		}

		enterRoomRsp()

		Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom 444")
		room.Guests[client.Id] = client
		idRoomMap[client.Id] = room

	} else {
		room = &Room{
			Id:     req.RoomId,
			Passwd: req.Passwd,
			Guests: make(map[NetCore.ClientIDType]*NetCore.Client),
		}

		enterRoomRsp()

		room.Guests[client.Id] = client
		idRoomMap[client.Id] = room

		rooms[req.RoomId] = room

		Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom 555")
	}

	client.AddCloseCB("leaveRoom", func(c *NetCore.Client) {
		//Logger.Info(LogConf.WorkerClientJob, client.Idx, "handleClientEnterRoom 666")
		var tmproom *Room
		if tmproom, ok = idRoomMap[c.Id]; ok {
			delete(tmproom.Guests, c.Id)

			leaveMsg := &NetCore.NetMsg{
				Cmd: MsgConf.ClientLeaveRoomNotify,
			}
			notify := &LeaveRoomNotify{
				Id: NetCore.ClientIDType(c.Id),
			}

			if buf, err := json.Marshal(notify); err == nil {
				leaveMsg.Buf = buf
			} else {
				return
			}

			if len(tmproom.Guests) > 0 {
				for _, v := range tmproom.Guests {
					v.SendMsg(leaveMsg)
				}
			} else {
				delete(rooms, tmproom.Id)
			}
			Logger.Info(LogConf.WorkerClientJob, client.Idx, "client leave room %d", client.Id)
		}
	})

	return true
}