Beispiel #1
0
//注册用户
void Connection::HandleRegReq(RegUserReq* req)
{
    RegUserRes res;
    res.set_rescode(FAILED);

    //判断用户名是否存在,如果不存在,操作数据库,注册,如果存在,返回错误码
    CDatabase *pdb = CDBPool<CDatabase>::Instance()->Acquire();
    if(pdb != NULL)
    {
        char sql[MAX_SQL_LEN];
        memset(sql,0,sizeof(sql));
        snprintf(sql,sizeof(sql),"select * from tab_user where username='******'",req->name().c_str());
        if(pdb->SelectBySql(sql))
        {
            if(pdb->GetRowNum())
            {
                log::log(Info,"username have been Register");
                //查找到了
            }
            else
            {
                //注册用户
                memset(sql,0,sizeof(sql));
                snprintf(sql,sizeof(sql),"insert into tab_user(username,password) values('%s','%s')",req->name().c_str(),req->password().c_str());
                if(pdb->SelectBySql(sql))
                {
                    //注册成功
                    log::log(Info,"Register success!");
                    res.set_rescode(SUCCESS);
                }
                else
                {
                    log::log(Info,"database error");
                    res.set_rescode(INNER_ERR);
                }
            }
            //释放结果集
            pdb->FreeRecord();
        }
        CDBPool<CDatabase>::Instance()->Release(pdb);
    }
    SendRequest(&res,REGUSER_RES);
}
Beispiel #2
0
//登陆请求,同一个用户不能登陆两次
void Connection::HandleLoginReq(LoginReq* req)
{
    //返回userid把
    LoginRes res;
    res.set_rescode(FAILED);

    CDatabase *pdb = CDBPool<CDatabase>::Instance()->Acquire();
    if(pdb != NULL)
    {
        char sql[1024];
        memset(sql,0,sizeof(sql));
        snprintf(sql,sizeof(sql),"select * from tab_user where username='******'",req->name().c_str());
        if(pdb->SelectBySql(sql))
        {
            if(pdb->GetRecord())
            {
                if(strncmp(req->password().c_str(),pdb->row[2],strlen(pdb->row[2]))==0)
                {
                    log::log(Info,"login SUCCESS!!!");
                    log::log(Info,"userid:",pdb->row[0]);
                    m_userid = atoi(pdb->row[0]);
                    res.set_rescode(SUCCESS);
                    //查找到了
                    res.set_userid(atoi(pdb->row[0]));
                }
            }
            //释放结果集
            pdb->FreeRecord();
        }
        CDBPool<CDatabase>::Instance()->Release(pdb);
    }
    SendRequest(&res,LOGIN_RES);
    Online msg;
    msg.set_id(m_userid);
    msg.set_name(req->name());
    m_server->BroadcastMsg(&msg,ONLINE_MSG);
}