Changeset 14 in chevmsgr for trunk/msgsrv.cpp
- Timestamp:
- 08/30/15 21:31:39 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/msgsrv.cpp
r13 r14 16 16 // -------------------------------------------------------------- 17 17 18 typedef struct 18 typedef struct LoginSession 19 19 { 20 20 cf::network::tcp * sock; … … 22 22 } LoginSession; 23 23 std::map<std::string, LoginSession> gOnlineUsers; 24 25 26 // ================================================================24 std::map<std::string, std::vector<std::string> > gSessionMap; 25 26 // -------------------------------------------------------------- 27 27 28 28 int cb_getFriendList(void * userArg, int argc, char ** argv, char ** colName) … … 75 75 : db(NULL) 76 76 { 77 Init(); 77 78 } 78 79 … … 139 140 throw(cf::exception) 140 141 { 141 int result ;142 char * errMsg ;142 int result = 0; 143 char * errMsg = NULL; 143 144 144 145 result = sqlite3_exec(db, query.c_str(), cb, userArg, &errMsg); 145 146 146 147 if (result != SQLITE_OK) 147 THROW_EXCEPTION (errMsg);148 THROW_EXCEPTION("[DBERROR][" << result << "] " << errMsg); 148 149 } 149 150 … … 163 164 { 164 165 FORWARD_EXCEPTION(e); 165 166 return false;167 166 } 168 167 } … … 175 174 bool isExist = false; 176 175 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 + "')"; 178 177 179 178 this->exec(existQuery, cb_join, &isExist); … … 282 281 { 283 282 gOnlineUsers.erase(id); 283 LOG(STR(id << " was logged out")); 284 } 285 286 static 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; 284 300 } 285 301 … … 287 303 { 288 304 char random[8] = {0x00,}; 289 sprintf(random, "%06d", rand() % 1000000);305 sprintf(random, "%06d", generateSeed() % 1000000); 290 306 291 307 return random; … … 294 310 static std::string httpSMS(const Protocol::Message & parser) 295 311 { 296 std::string phone = parser.get<std::string>( "phone");312 std::string phone = parser.get<std::string>(ProtocolType::PHONE); 297 313 298 314 #define CRLF "\r\n" … … 306 322 CRLF; 307 323 308 Protocol::Response response;309 324 cf::network::tcp smsSock; 310 325 cf::network::host smsServer(url, 80); … … 312 327 smsSock.connect(smsServer); 313 328 smsSock.send(http); 329 smsSock.receive(); 314 330 smsSock.close(); 315 331 … … 329 345 } 330 346 331 static bool login(const Protocol::Message & parser )347 static bool login(const Protocol::Message & parser, cf::network::tcp & sock, cf::bin & key) 332 348 throw (cf::exception) 333 349 { … … 335 351 std::string pw = parser.get<std::string>(ProtocolType::PW); 336 352 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 365 static bool chat(const Protocol::Message & message) 342 366 { 343 367 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 349 382 result = true; 350 383 } … … 362 395 } 363 396 364 static bool opensession(const Protocol::Message & parser) 365 { 397 static bool openSession(const Protocol::Message & message) 398 { 399 Protocol::Message parser = message; 366 400 bool result = false; 367 401 std::string sessid; 368 402 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); 373 404 374 405 sessid = createSessionID(concat); 375 406 407 parser.mObject[ProtocolType::SESSION_ID] = sessid; 408 std::string serialized = parser.serialize(); 409 376 410 for (size_t iter = 0; iter < idList.size(); iter++) 377 411 { 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); 380 415 381 416 result = true; 382 417 } 383 418 419 gSessionMap[sessid] = idList; 420 384 421 return result; 385 422 } … … 396 433 static std::string workerInitiator(cf::network::tcp & sock) 397 434 { 435 Protocol::Message parser; 436 398 437 try 399 438 { 400 439 std::string sms; 440 std::string id; 441 bool loggedIn = false; 401 442 402 443 while (true) 403 444 { 404 Protocol::Message parser;405 445 parser.parse(sock.receive().toString()); 406 446 407 if (parser.type() == "sms")447 if (parser.type() == ProtocolType::SMS) 408 448 { 409 449 sms = httpSMS(parser); 410 450 } 411 else if (parser.type() == "join")451 else if (parser.type() == ProtocolType::JOIN) 412 452 { 413 if ( join(parser, sms, sock.peer().address()))453 if (!join(parser, sms, sock.peer().address())) 414 454 THROW_EXCEPTION("user(" << parser.get<std::string>(ProtocolType::ID) << ") cannot join"); 415 455 } 416 else if (parser.type() == "login")456 else if (parser.type() == ProtocolType::LOGIN) 417 457 { 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; 421 472 } 422 473 } 423 474 catch (cf::exception & e) 424 475 { 476 sock.send(Protocol::Response().result(parser.type(), false)); 425 477 FORWARD_EXCEPTION(e); 426 478 } … … 436 488 try 437 489 { 438 workerInitiator(*sock);490 id = workerInitiator(*sock); 439 491 440 492 Protocol::Response response; … … 449 501 LOG(message); 450 502 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 } 459 512 } 460 513 } 461 514 catch (cf::exception & e) 462 515 { 463 LOG(e. what());516 LOG(e.stackTrace()); 464 517 } 465 518 … … 472 525 { 473 526 cf::network::tcp sock; 474 475 srand((unsigned int)time(NULL));476 527 477 528 try
Note:
See TracChangeset
for help on using the changeset viewer.