Changeset 14 in chevmsgr for trunk/msgsrv.cpp


Ignore:
Timestamp:
08/30/15 21:31:39 (9 years ago)
Author:
cheese
Message:

테스트 코드 추가
UI 모양 작업
채팅되는 상태까지 확인

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/msgsrv.cpp

    r13 r14  
    1616// --------------------------------------------------------------
    1717
    18 typedef struct
     18typedef struct LoginSession
    1919{
    2020    cf::network::tcp * sock;
     
    2222} LoginSession;
    2323std::map<std::string, LoginSession> gOnlineUsers;
    24 
    25 
    26 //================================================================
     24std::map<std::string, std::vector<std::string> > gSessionMap;
     25
     26// --------------------------------------------------------------
    2727
    2828int cb_getFriendList(void * userArg, int argc, char ** argv, char ** colName)
     
    7575        : db(NULL)
    7676    {
     77        Init();
    7778    }
    7879   
     
    139140        throw(cf::exception)
    140141    {
    141         int result;
    142         char * errMsg;
     142        int result = 0;
     143        char * errMsg = NULL;
    143144
    144145        result = sqlite3_exec(db, query.c_str(), cb, userArg, &errMsg);
    145146
    146147        if (result != SQLITE_OK)
    147             THROW_EXCEPTION (errMsg);
     148            THROW_EXCEPTION("[DBERROR][" << result << "] " << errMsg);
    148149    }
    149150
     
    163164        {
    164165            FORWARD_EXCEPTION(e);
    165 
    166             return false;
    167166        }
    168167    }
     
    175174            bool isExist = false;
    176175            std::string existQuery = "select * from T_ACCOUNT where id = '" + id + "'";
    177             std::string insertQuery = "insert into T_ACCOUNT values('" + id + "', '" + pw + "', '" + sms + "', '" + ip + "')";
     176            std::string insertQuery = "insert into T_ACCOUNT(id, pw) values('" + id + "', '" + pw + "')";
    178177
    179178            this->exec(existQuery, cb_join, &isExist);
     
    282281{
    283282    gOnlineUsers.erase(id);
     283    LOG(STR(id << " was logged out"));
     284}
     285
     286static unsigned int generateSeed()
     287{
     288    unsigned int ret = 0;
     289    int t = (int)time(NULL);
     290
     291    cf::bin b;
     292    b.resize(sizeof(int));
     293
     294    b.set((cf::uint8_t*)&t, sizeof(int));
     295    cf::bin s = crypto().sha256(b);
     296
     297    memcpy(&ret, s.buffer(), sizeof(unsigned int));
     298
     299    return ret;
    284300}
    285301
     
    287303{
    288304    char random[8] = {0x00,};
    289     sprintf(random, "%06d", rand() % 1000000);
     305    sprintf(random, "%06d", generateSeed() % 1000000);
    290306
    291307    return random;
     
    294310static std::string httpSMS(const Protocol::Message & parser)
    295311{
    296     std::string phone = parser.get<std::string>("phone");
     312    std::string phone = parser.get<std::string>(ProtocolType::PHONE);
    297313
    298314#define CRLF    "\r\n"
     
    306322        CRLF;
    307323
    308     Protocol::Response response;
    309324    cf::network::tcp smsSock;
    310325    cf::network::host smsServer(url, 80);
     
    312327    smsSock.connect(smsServer);
    313328    smsSock.send(http);
     329    smsSock.receive();
    314330    smsSock.close();
    315331
     
    329345}
    330346
    331 static bool login(const Protocol::Message & parser)
     347static bool login(const Protocol::Message & parser, cf::network::tcp & sock, cf::bin & key)
    332348    throw (cf::exception)
    333349{
     
    335351    std::string pw = parser.get<std::string>(ProtocolType::PW);
    336352
    337     return dbmgr.login(id, pw);
    338 }
    339 
    340 static bool chat(const Protocol::Message & parser,
    341                  const std::string & message)
     353    bool result = dbmgr.login(id, pw);
     354    if (result)
     355    {
     356        LoginSession loginSess;
     357        loginSess.sock = &sock;
     358        loginSess.key = key;
     359        gOnlineUsers[id] = loginSess;
     360    }
     361
     362    return result;
     363}
     364
     365static bool chat(const Protocol::Message & message)
    342366{
    343367    bool result = false;
    344     std::string to = parser.get<std::string>(ProtocolType::TO);
    345 
    346     if (isOnline(to))
    347     {
    348         gOnlineUsers[to].sock->send(message);
     368    Protocol::Message parser = message;
     369    std::string sessid = parser.get<std::string>(ProtocolType::SESSION_ID);
     370    std::vector<std::string> idList = gSessionMap[sessid];
     371    std::string sender = parser.get<std::string>(ProtocolType::ID);
     372    parser.mObject[ProtocolType::TYPE] = ProtocolType::LISTEN;
     373    std::string serialized = parser.serialize();
     374
     375    for (size_t iter = 0; iter < idList.size(); iter++)
     376    {
     377        std::string id = idList[iter];
     378
     379        if (sender != id && isOnline(id))
     380            gOnlineUsers[id].sock->send(serialized);
     381
    349382        result = true;
    350383    }
     
    362395}
    363396
    364 static bool opensession(const Protocol::Message & parser)
    365 {
     397static bool openSession(const Protocol::Message & message)
     398{
     399    Protocol::Message parser = message;
    366400    bool result = false;
    367401    std::string sessid;
    368402    std::vector<std::string> idList = parser.getList<std::string>(ProtocolType::ID_LIST);
    369     std::string concat = idList[0];
    370 
    371     for (size_t iter = 1; iter < concat.size(); iter++)
    372         concat += idList[iter];
     403    std::string concat = joinStrings(idList);
    373404
    374405    sessid = createSessionID(concat);
    375406
     407    parser.mObject[ProtocolType::SESSION_ID] = sessid;
     408    std::string serialized = parser.serialize();
     409
    376410    for (size_t iter = 0; iter < idList.size(); iter++)
    377411    {
    378         if (isOnline(idList[iter]))
    379             gOnlineUsers[idList[iter]].sock->send(sessid);
     412        std::string id = idList[iter];
     413        if (isOnline(id))
     414            gOnlineUsers[id].sock->send(serialized);
    380415
    381416        result = true;
    382417    }
    383418
     419    gSessionMap[sessid] = idList;
     420
    384421    return result;
    385422}
     
    396433static std::string workerInitiator(cf::network::tcp & sock)
    397434{
     435    Protocol::Message parser;
     436
    398437    try
    399438    {
    400439        std::string sms;
     440        std::string id;
     441        bool loggedIn = false;
    401442
    402443        while (true)
    403444        {
    404             Protocol::Message parser;
    405445            parser.parse(sock.receive().toString());
    406446
    407             if (parser.type() == "sms")
     447            if (parser.type() == ProtocolType::SMS)
    408448            {
    409449                sms = httpSMS(parser);
    410450            }
    411             else if (parser.type() == "join")
     451            else if (parser.type() == ProtocolType::JOIN)
    412452            {
    413                 if (join(parser, sms, sock.peer().address()))
     453                if (!join(parser, sms, sock.peer().address()))
    414454                    THROW_EXCEPTION("user(" << parser.get<std::string>(ProtocolType::ID) << ") cannot join");
    415455            }
    416             else if (parser.type() == "login")
     456            else if (parser.type() == ProtocolType::LOGIN)
    417457            {
    418                 if (login(parser))
    419                     parser.get<std::string>(ProtocolType::ID);
    420             }           
     458                std::string ip = sock.peer().address();
     459                cf::bin seed = sms + DELIMITER + ip;
     460                cf::bin key = crypto().sha256(seed);
     461
     462                if (login(parser, sock, key))
     463                    id = parser.get<std::string>(ProtocolType::ID);
     464
     465                loggedIn = true;
     466            }
     467
     468            // success
     469            sock.send(Protocol::Response().result(parser.type(), true));
     470            if (loggedIn)
     471                return id;
    421472        }
    422473    }
    423474    catch (cf::exception & e)
    424475    {
     476        sock.send(Protocol::Response().result(parser.type(), false));
    425477        FORWARD_EXCEPTION(e);
    426478    }
     
    436488    try
    437489    {
    438         workerInitiator(*sock);
     490        id = workerInitiator(*sock);
    439491
    440492        Protocol::Response response;
     
    449501            LOG(message);
    450502
    451             if (parser.type() == "chat")
    452                 result = chat(parser, message);
    453             else if (parser.type() == "opensession")
    454                 result = opensession(parser);
    455 //          else if (parser.type() == "getFriendList")
    456 //              result =
    457 
    458             sock->send(response.result(parser.type(), result));
     503            if (parser.type() == ProtocolType::TELL)
     504            {
     505                result = chat(parser);
     506                sock->send(response.result(parser.type(), result));
     507            }
     508            else if (parser.type() == ProtocolType::OPEN_SESSION)
     509            {
     510                result = openSession(parser);
     511            }
    459512        }
    460513    }
    461514    catch (cf::exception & e)
    462515    {
    463         LOG(e.what());
     516        LOG(e.stackTrace());
    464517    }
    465518
     
    472525{
    473526    cf::network::tcp sock;
    474 
    475     srand((unsigned int)time(NULL));
    476527
    477528    try
Note: See TracChangeset for help on using the changeset viewer.