Ejemplo n.º 1
0
void SendMsg::Execute(Session *s) {
    InStream jis(s->GetRequestPack()->buf, s->GetRequestPack()->head.len);
	uint16 cmd = s->GetCmd();
    
	// 登录id
    string from;
	string to;
    string text;
    string uuid;
    jis>>from;
	jis>>to;
    jis>>text;
    jis>>uuid;
	
    int16 error_code = kErrCode00;
	char error_msg[ERR_MSG_LENGTH+1] = {0};
	DataService dao;
    IMMsg msg;
	int ret = kErrCode00;
    int msgId;
    ret = dao.SendMsg(from, to, text, msgId);
	if (ret == kErrCode00) {
		LOG(INFO)<<"发送记录插入 userId: "<<from;
	} else  {
		error_code = ret;
        assert(error_code < kErrCodeMax);
		strcpy(error_msg, gErrMsg[error_code].c_str());
		LOG(ERROR)<<"发送记录插入出错: "<<error_msg<<" userId: "<<from;
	}
    
	OutStream jos;
	// 包头命令
	jos<<cmd;
	size_t lengthPos = jos.Length();
	jos.Skip(2);///留出2个字节空间用来后面存储包体长度+包尾(8)的长度
	// 包头cnt、seq、error_code、error_msg
	uint16 cnt = 0;
	uint16 seq = 0;
	jos<<cnt<<seq<<error_code;
	jos.WriteBytes(error_msg, ERR_MSG_LENGTH);
    
	// 包体
    if (ret == kErrCode00) {
        jos<<msgId;
        jos<<uuid;
    }
    
    FillOutPackage(jos, lengthPos, cmd);
	s->Send(jos.Data(), jos.Length());
    
    if (ret == kErrCode00) {
        PushService push;
        push.PushSendMsg(from, to, dao);
    }
}
Ejemplo n.º 2
0
void RequestAddBuddy::Execute(Session *s) {
    InStream jis(s->GetRequestPack()->buf, s->GetRequestPack()->head.len);
	uint16 cmd = s->GetCmd();
    
	// 好友请求
	string fromName;
    string toName;
    jis>>fromName;
	jis>>toName;
	
	int16 error_code = kErrCode00;
	char error_msg[ERR_MSG_LENGTH+1] = {0};
    
    
	// 实际的添加操作
	DataService dao;
	int ret;
    string toId;
    string reqId;
	ret = dao.RequestAddBuddy(fromName, toName, toId, reqId);
	if (ret == kErrCode00) {
        strcpy(error_msg, "请求发送成功");
		LOG(INFO)<<"添加好友请求成功 from: "<<fromName<<" to: "<<toName;
	} else {
		error_code = ret;
        assert(error_code < kErrCodeMax);
		strcpy(error_msg, gErrMsg[error_code].c_str());
		LOG(INFO)<<"添加好友请求失败 from: "<<fromName<<" to: "<<toName<<" "<<error_msg;
	}
    
	OutStream jos;
	// 包头命令
	jos<<cmd;
	size_t lengthPos = jos.Length();
	jos.Skip(2);///留出2个字节空间用来后面存储包体长度+包尾(8)的长度
	// 包头cnt、seq、error_code、error_msg
	uint16 cnt = 0;
	uint16 seq = 0;
	jos<<cnt<<seq<<error_code;
	jos.WriteBytes(error_msg, ERR_MSG_LENGTH);
    
	// 空包体
    
    FillOutPackage(jos, lengthPos, cmd);
    
	s->Send(jos.Data(), jos.Length());
    if (ret == kErrCode00) {
        
        PushService push;
        //通知在线请求方同意结果
        push.PushRequestAddBuddy(s->GetIMUser(), toId, reqId, dao);
    }
}
Ejemplo n.º 3
0
void SearchBuddy::Execute(Session *s) {
    InStream jis(s->GetRequestPack()->buf, s->GetRequestPack()->head.len);
	uint16 cmd = s->GetCmd();
    
	// 搜索名
	string name;
	jis>>name;
	
	int16 error_code = kErrCode00;
	char error_msg[ERR_MSG_LENGTH+1] = {0};
    
    
	// 实际的搜索操作
	DataService dao;
    IMUser user;
    user.username = name;
	int ret;
	ret = dao.UserSearch(name, s, user);
	if (ret == kErrCode00) {
		LOG(INFO)<<"查询用户成功 查询名: "<<name;
	} else {
		error_code = ret;
        assert(error_code < kErrCodeMax);
		strcpy(error_msg, gErrMsg[error_code].c_str());
		LOG(INFO)<<"查询用户失败 查询名: "<<name<<" "<<error_msg;
	}
    
	OutStream jos;
	// 包头命令
	jos<<cmd;
	size_t lengthPos = jos.Length();
	jos.Skip(2);///留出2个字节空间用来后面存储包体长度+包尾(8)的长度
	// 包头cnt、seq、error_code、error_msg
	uint16 cnt = 0;
	uint16 seq = 0;
	jos<<cnt<<seq<<error_code;
	jos.WriteBytes(error_msg, ERR_MSG_LENGTH);
    
	// 包体
    if (ret == kErrCode00) {
        jos<<user.userId;
        jos<<user.username;
        jos<<user.regDate;
        jos<<user.signature;
        jos<<user.gender;
        jos<<user.relation;
        jos<<user.status;
        jos<<user.statusName;
    }
    FillOutPackage(jos, lengthPos, cmd);
	s->Send(jos.Data(), jos.Length());
}
Ejemplo n.º 4
0
void RejectAddBuddy::Execute(Session *s) {
    InStream jis(s->GetRequestPack()->buf, s->GetRequestPack()->head.len);
	uint16 cmd = s->GetCmd();
    
	string reqId, fromId;
	jis>>reqId;
    jis>>fromId;
	
	int16 error_code = kErrCode00;
	char error_msg[ERR_MSG_LENGTH+1] = {0};
    
    
	// 拒绝
	DataService dao;
    int userId = s->GetIMUser().userId;
	int ret = dao.RejectAddBuddy(fromId, Convert::IntToString(userId), reqId);

	if (ret == kErrCode00) {
		LOG(INFO)<<"拒绝用户 " << "fromUserId: "<<fromId<< " toUserId: "<<userId<< " 成功";
	} else {
		error_code = ret;
        assert(error_code < kErrCodeMax);
		strcpy(error_msg, gErrMsg[error_code].c_str());
        LOG(INFO)<<"拒绝用户 " << "fromUserId: "<<fromId<< " toUserId: "<<userId<< " 失败: "<<error_msg;
	}
    
	OutStream jos;
	// 包头命令
	jos<<cmd;
	size_t lengthPos = jos.Length();
	jos.Skip(2);///留出2个字节空间用来后面存储包体长度+包尾(8)的长度
	// 包头cnt、seq、error_code、error_msg
	uint16 cnt = 0;
	uint16 seq = 0;
	jos<<cnt<<seq<<error_code;
	jos.WriteBytes(error_msg, ERR_MSG_LENGTH);
    
    ///空包体
    
    FillOutPackage(jos, lengthPos, cmd);
    
	s->Send(jos.Data(), jos.Length());
    
    if (ret == kErrCode00) {
        //通知在线请求方被拒绝结果
        PushService push;
        push.PushBuddyRequestResult(s->GetIMUser(), Convert::StringToInt(fromId), false, false, reqId, dao);

    }
}
Ejemplo n.º 5
0
void HeartBeat::Execute(Session *s) {
    InStream jis(s->GetRequestPack()->buf, s->GetRequestPack()->head.len);
	uint16 cmd = s->GetCmd();
    
	// 登录id
    string userId;
    jis>>userId;

    int16 error_code = kErrCode00;
	char error_msg[ERR_MSG_LENGTH+1] = {0};

	int ret = kErrCode00;

	if (ret == kErrCode00) {
		LOG(INFO)<<"用户心跳包 userId: "<<userId;
         timeval now;
         gettimeofday(&now, NULL);
         s->SetLastTime(now);
	} else  {
		error_code = ret;
        assert(error_code < kErrCodeMax);
		strcpy(error_msg, gErrMsg[error_code].c_str());
		//LOG_ERROR<<"发送记录插入出错: "<<error_msg<<" userId: "<<from;
	}
    
	OutStream jos;
	// 包头命令
	jos<<cmd;
	size_t lengthPos = jos.Length();
	jos.Skip(2);///留出2个字节空间用来后面存储包体长度+包尾(8)的长度
	// 包头cnt、seq、error_code、error_msg
	uint16 cnt = 0;
	uint16 seq = 0;
	jos<<cnt<<seq<<error_code;
	jos.WriteBytes(error_msg, ERR_MSG_LENGTH);
    
	// 空包体
    
    FillOutPackage(jos, lengthPos, cmd);
	s->Send(jos.Data(), jos.Length());
}
Ejemplo n.º 6
0
void AcceptAddBuddy::Execute(Session *s) {
    InStream jis(s->GetRequestPack()->buf, s->GetRequestPack()->head.len);
	uint16 cmd = s->GetCmd();
    
	string reqId, fromId, addPeer;
	jis>>reqId;
    jis>>fromId;
    jis>>addPeer;
	
	int16 error_code = kErrCode00;
	char error_msg[ERR_MSG_LENGTH+1] = {0};
    
    
	// 添加
	DataService dao;
	int ret;
    bool peer =  static_cast<bool>(Convert::StringToInt(addPeer));
    int userId = s->GetIMUser().userId;
    IMUser fromUser;    // 用于返回请求方最新资料(主要是否在线)
    ret = dao.AcceptAddBuddy(fromId, Convert::IntToString(userId), reqId, peer, fromUser);
	if (ret == kErrCode00) {
		LOG(INFO)<<"接受用户 " <<(Convert::StringToInt(addPeer) ? "并且添加对方为好友" : "") <<
                "fromUserId: "<<fromId<< " toUserId: "<<userId<< " 成功";
	} else {
		error_code = ret;
        assert(error_code < kErrCodeMax);
		strcpy(error_msg, gErrMsg[error_code].c_str());
        LOG(INFO)<<"接受用户 " <<(Convert::StringToInt(addPeer) ? "并且添加对方为好友" : "") <<
        "fromUserId: "<<fromId<< " toUserId: "<<userId<< " 失败: "<<error_msg;
	}
    
	OutStream jos;
	// 包头命令
	jos<<cmd;
	size_t lengthPos = jos.Length();
	jos.Skip(2);///留出2个字节空间用来后面存储包体长度+包尾(8)的长度
	// 包头cnt、seq、error_code、error_msg
	uint16 cnt = 0;
	uint16 seq = 0;
	jos<<cnt<<seq<<error_code;
	jos.WriteBytes(error_msg, ERR_MSG_LENGTH);
    
	// 包体
    if (ret == kErrCode00) {
        jos<<fromUser.userId;
        jos<<fromUser.username;
        jos<<fromUser.regDate;
        jos<<fromUser.signature;
        jos<<fromUser.gender;
        jos<<fromUser.relation;
        jos<<fromUser.status;
        jos<<fromUser.statusName;
        jos<<addPeer;
    }
 
    FillOutPackage(jos, lengthPos, cmd);
	s->Send(jos.Data(), jos.Length());
    
    if (ret == kErrCode00) {
        
        PushService push;
        //通知在线请求方同意结果
        push.PushBuddyRequestResult(s->GetIMUser(), Convert::StringToInt(fromId), true, peer, reqId, dao);
    }
}
Ejemplo n.º 7
0
void RetrieveUnreadMsg::Execute(Session *s) {
    InStream jis(s->GetRequestPack()->buf, s->GetRequestPack()->head.len);
	uint16 cmd = s->GetCmd();
    if (cmd == CMD_UNREAD) {
        cmd = CMD_RETRIEVE_UNREAD_MSG;
    }
    
	// 登录id
	string userId;
	jis>>userId;
	
    int16 error_code = kErrCode00;
	char error_msg[ERR_MSG_LENGTH+1] = {0};
	DataService dao;
    std::list<IMMsg> msgs;
	int ret = kErrCode00;
    ret = dao.RetrieveUnreadMsg(userId, msgs);
	if (ret == kErrCode00) {
		LOG(INFO)<<"查询未读消息成功 userId: "<<userId <<" 未读消息条数: "<<msgs.size();
        uint16 cnt = static_cast<uint16>(msgs.size());    //总共多少
		uint16 seq = 0;                                     //序列号
        list<IMMsg>::const_iterator it;
        for (it = msgs.begin(); it != msgs.end(); ++it) {
            OutStream jos;
            // 包头命令
            jos<<cmd;
            size_t lengthPos = jos.Length();
            jos.Skip(2);///留出2个字节空间用来后面存储包体长度+包尾(8)的长度
            // 包头cnt、seq、error_code、error_msg
            
            jos<<cnt<<seq<<error_code;
            seq++;
            jos.WriteBytes(error_msg, ERR_MSG_LENGTH);
            
            // 包体
            jos<<it->msgId;
            jos<<it->fromId;
            jos<<it->fromName;
            jos<<it->toId;
            jos<<it->toName;
            jos<<it->text;
            jos<<it->sent;
            jos<<it->requestTime;
            jos<<it->sendTime;
            
            FillOutPackage(jos, lengthPos, cmd);
            
            s->Send(jos.Data(), jos.Length());
        }
        //TODO 发送后更新消息状态

	} else  {
		error_code = ret;
        assert(error_code < kErrCodeMax);
		strcpy(error_msg, gErrMsg[error_code].c_str());
		LOG(INFO)<<"查询未读消息失败: "<<error_msg <<" userId: "<<userId;
        
        OutStream jos;
        // 包头命令
        jos<<cmd;
        size_t lengthPos = jos.Length();
        jos.Skip(2);///留出2个字节空间用来后面存储包体长度+包尾(8)的长度
        // 包头cnt、seq、error_code、error_msg
        uint16 cnt = 0;
        uint16 seq = 0;
        jos<<cnt<<seq<<error_code;
        jos.WriteBytes(error_msg, ERR_MSG_LENGTH);
        
        // 空包体
        
        FillOutPackage(jos, lengthPos, cmd);
        
        s->Send(jos.Data(), jos.Length());
	}
}