[4] | 1 |
|
---|
| 2 | #include "cf/network.h"
|
---|
| 3 | #include "cf/task.h"
|
---|
| 4 | #include "cf/codec.h"
|
---|
| 5 | #include "cf/file.h"
|
---|
[13] | 6 | #include "crypto.h"
|
---|
[4] | 7 |
|
---|
| 8 | #include "msg.hpp"
|
---|
| 9 |
|
---|
| 10 | #include <map>
|
---|
| 11 | #include <stdlib.h>
|
---|
| 12 | #include <time.h>
|
---|
| 13 |
|
---|
| 14 | #include "sqlite3.h"
|
---|
| 15 |
|
---|
| 16 | // --------------------------------------------------------------
|
---|
| 17 |
|
---|
[14] | 18 | typedef struct LoginSession
|
---|
[4] | 19 | {
|
---|
| 20 | cf::network::tcp * sock;
|
---|
| 21 | cf::bin key;
|
---|
| 22 | } LoginSession;
|
---|
| 23 | std::map<std::string, LoginSession> gOnlineUsers;
|
---|
[14] | 24 | std::map<std::string, std::vector<std::string> > gSessionMap;
|
---|
[4] | 25 |
|
---|
[14] | 26 | // --------------------------------------------------------------
|
---|
[4] | 27 |
|
---|
| 28 | int cb_getFriendList(void * userArg, int argc, char ** argv, char ** colName)
|
---|
| 29 | {
|
---|
| 30 | std::string * friendList = (std::string *) userArg;
|
---|
| 31 |
|
---|
| 32 | *friendList = argv[1];
|
---|
| 33 |
|
---|
| 34 | return 0;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | int cb_getSMS(void * userArg, int argc, char ** argv, char ** colName)
|
---|
| 38 | {
|
---|
| 39 | std::string * SMS = (std::string *) userArg;
|
---|
| 40 |
|
---|
| 41 | *SMS = argv[1];
|
---|
| 42 |
|
---|
| 43 | return 0;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | int cb_login(void * userArg, int argc, char ** argv, char ** colName)
|
---|
| 47 | {
|
---|
| 48 | bool * result = (bool *)userArg;
|
---|
| 49 |
|
---|
| 50 | *result = true;
|
---|
| 51 |
|
---|
| 52 | return 0;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | int cb_join(void * userArg, int argc, char ** argv, char ** colName)
|
---|
| 56 | {
|
---|
| 57 | bool * isExist = (bool *)userArg;
|
---|
| 58 |
|
---|
| 59 | *isExist = true;
|
---|
| 60 |
|
---|
| 61 | return 0;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | //===============================================================
|
---|
| 65 |
|
---|
| 66 | // --------------------------------------------------------------
|
---|
| 67 | class DBManager
|
---|
| 68 | {
|
---|
| 69 | private:
|
---|
| 70 | sqlite3 * db;
|
---|
| 71 |
|
---|
| 72 | public:
|
---|
| 73 | DBManager()
|
---|
| 74 | throw (cf::exception)
|
---|
| 75 | : db(NULL)
|
---|
| 76 | {
|
---|
[14] | 77 | Init();
|
---|
[4] | 78 | }
|
---|
| 79 |
|
---|
| 80 | void Init()
|
---|
| 81 | throw (cf::exception)
|
---|
| 82 | {
|
---|
| 83 | int result = 0;
|
---|
| 84 | bool isExist = false;
|
---|
| 85 |
|
---|
| 86 | isExist = cf::file("account.db").exists();
|
---|
| 87 |
|
---|
| 88 | result = sqlite3_open("account.db", &db);
|
---|
| 89 | if (result)
|
---|
| 90 | fprintf(stderr, "sqlite_open failed\n");
|
---|
| 91 |
|
---|
| 92 | if (!isExist)
|
---|
| 93 | setup();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | void setup()
|
---|
| 97 | throw (cf::exception)
|
---|
| 98 | {
|
---|
| 99 | std::string sqlCreateAccountTable;
|
---|
| 100 | std::string sqlCreateAuthTable;
|
---|
| 101 | std::string sqlCreateFriendsTable;
|
---|
| 102 | // declaration
|
---|
| 103 |
|
---|
| 104 | // query
|
---|
| 105 | sqlCreateAccountTable =
|
---|
| 106 | "CREATE TABLE T_ACCOUNT"
|
---|
| 107 | "("
|
---|
[15] | 108 | " num INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
|
---|
[4] | 109 | " id TEXT,"
|
---|
| 110 | " pw TEXT"
|
---|
| 111 | ")";
|
---|
| 112 | sqlCreateAuthTable =
|
---|
| 113 | "CREATE TABLE T_AUTH"
|
---|
| 114 | "("
|
---|
| 115 | " id TEXT PRIMARY KEY NOT NULL,"
|
---|
| 116 | " sms1 TEXT,"
|
---|
| 117 | " ip1 TEXT,"
|
---|
| 118 | " sms2 TEXT,"
|
---|
| 119 | " ip2 TEXT,"
|
---|
| 120 | " sms3 TEXT,"
|
---|
| 121 | " ip3 TEXT,"
|
---|
| 122 | " sms4 TEXT,"
|
---|
| 123 | " ip4 TEXT,"
|
---|
| 124 | " sms5 TEXT,"
|
---|
| 125 | " ip5 TEXT"
|
---|
| 126 | ")";
|
---|
| 127 | sqlCreateFriendsTable =
|
---|
| 128 | "CREATE TABLE T_FRIENDS"
|
---|
| 129 | "("
|
---|
| 130 | " id TEXT PRIMARY KEY NOT NULL,"
|
---|
| 131 | " friend TEXT"
|
---|
| 132 | ")";
|
---|
| 133 |
|
---|
| 134 | exec(sqlCreateAccountTable, NULL, NULL);
|
---|
| 135 | exec(sqlCreateAuthTable, NULL, NULL);
|
---|
| 136 | exec(sqlCreateFriendsTable, NULL, NULL);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | void exec(std::string & query, sqlite3_callback cb, void * userArg)
|
---|
| 140 | throw(cf::exception)
|
---|
| 141 | {
|
---|
[14] | 142 | int result = 0;
|
---|
| 143 | char * errMsg = NULL;
|
---|
[4] | 144 |
|
---|
| 145 | result = sqlite3_exec(db, query.c_str(), cb, userArg, &errMsg);
|
---|
| 146 |
|
---|
| 147 | if (result != SQLITE_OK)
|
---|
[14] | 148 | THROW_EXCEPTION("[DBERROR][" << result << "] " << errMsg);
|
---|
[4] | 149 | }
|
---|
| 150 |
|
---|
| 151 | bool login(const std::string & id, const std::string & pw)
|
---|
| 152 | throw (cf::exception)
|
---|
| 153 | {
|
---|
| 154 | try
|
---|
| 155 | {
|
---|
| 156 | bool result = false;
|
---|
| 157 | std::string query = "select * from T_ACCOUNT where id ='" + id + "' and pw = '" + pw + "'";
|
---|
| 158 |
|
---|
| 159 | this->exec(query, cb_login, &result);
|
---|
| 160 |
|
---|
| 161 | return result;
|
---|
| 162 | }
|
---|
| 163 | catch (cf::exception & e)
|
---|
| 164 | {
|
---|
| 165 | FORWARD_EXCEPTION(e);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | bool join(const std::string & id, const std::string & pw, const std::string & sms, const std::string & ip)
|
---|
| 170 | throw (cf::exception)
|
---|
| 171 | {
|
---|
| 172 | try
|
---|
| 173 | {
|
---|
| 174 | bool isExist = false;
|
---|
| 175 | std::string existQuery = "select * from T_ACCOUNT where id = '" + id + "'";
|
---|
[14] | 176 | std::string insertQuery = "insert into T_ACCOUNT(id, pw) values('" + id + "', '" + pw + "')";
|
---|
[4] | 177 |
|
---|
| 178 | this->exec(existQuery, cb_join, &isExist);
|
---|
| 179 |
|
---|
| 180 | if (!isExist)
|
---|
| 181 | this->exec(insertQuery, NULL, NULL);
|
---|
| 182 | }
|
---|
| 183 | catch (cf::exception & e)
|
---|
| 184 | {
|
---|
| 185 | FORWARD_EXCEPTION(e);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | return true;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | std::string getFriendList(std::string & id)
|
---|
| 192 | throw(cf::exception)
|
---|
| 193 | {
|
---|
| 194 | int result = 0;
|
---|
| 195 | std::string friendList;
|
---|
| 196 |
|
---|
| 197 | try
|
---|
| 198 | {
|
---|
| 199 | std::string query = "select * from T_FRIENDS where id = '" + id + "'";
|
---|
| 200 |
|
---|
| 201 | this->exec(query, cb_getFriendList, &friendList);
|
---|
| 202 | }
|
---|
| 203 | catch (cf::exception & e)
|
---|
| 204 | {
|
---|
| 205 | FORWARD_EXCEPTION(e);
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | int getSMS(std::string & id)
|
---|
| 210 | {
|
---|
| 211 | int result = 0;
|
---|
| 212 | std::string smsMsg;
|
---|
| 213 |
|
---|
| 214 | try
|
---|
| 215 | {
|
---|
| 216 | std::string query = "select * from T_AUTH where id = '" + id + "'";
|
---|
| 217 |
|
---|
| 218 | this->exec(query, cb_getSMS, &smsMsg);
|
---|
| 219 | }
|
---|
| 220 | catch (cf::exception & e)
|
---|
| 221 | {
|
---|
| 222 | FORWARD_EXCEPTION(e);
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | int addFriend(std::string & id)
|
---|
| 227 | {
|
---|
| 228 | try
|
---|
| 229 | {
|
---|
| 230 | std::string query = "insert into T_FRIENDS values('" + id + "')";
|
---|
| 231 |
|
---|
| 232 | this->exec(query, NULL, NULL);
|
---|
| 233 | }
|
---|
| 234 | catch (cf::exception & e)
|
---|
| 235 | {
|
---|
| 236 | FORWARD_EXCEPTION(e);
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | };
|
---|
| 240 | DBManager dbmgr;
|
---|
| 241 |
|
---|
| 242 | class Runner
|
---|
| 243 | {
|
---|
| 244 | public:
|
---|
| 245 | cf::network::tcp * mSock;
|
---|
| 246 | cf::task::thread * mThread;
|
---|
| 247 |
|
---|
| 248 | Runner(cf::network::tcp & sock, int (*worker)(void *))
|
---|
| 249 | : mSock(NULL)
|
---|
| 250 | , mThread(NULL)
|
---|
| 251 | {
|
---|
| 252 | mSock = new(std::nothrow) cf::network::tcp(sock.detach());
|
---|
| 253 | mThread = new(std::nothrow) cf::task::thread(worker);
|
---|
| 254 | if (!mSock || !mThread)
|
---|
| 255 | {
|
---|
| 256 | dispose();
|
---|
| 257 | THROW_EXCEPTION("cannot allocate thread arguments");
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | mThread->start(this);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | ~Runner()
|
---|
| 264 | {
|
---|
| 265 | dispose();
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | void dispose()
|
---|
| 269 | {
|
---|
| 270 | if (mSock) delete mSock;
|
---|
| 271 | if (mThread) delete mThread;
|
---|
| 272 | }
|
---|
| 273 | };
|
---|
| 274 |
|
---|
| 275 | static bool isOnline(const std::string & id)
|
---|
| 276 | {
|
---|
| 277 | return gOnlineUsers.find(id) != gOnlineUsers.end();
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | static void logout(const std::string & id)
|
---|
| 281 | {
|
---|
| 282 | gOnlineUsers.erase(id);
|
---|
[14] | 283 | LOG(STR(id << " was logged out"));
|
---|
[4] | 284 | }
|
---|
| 285 |
|
---|
[14] | 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;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[4] | 302 | static std::string getRandomCode()
|
---|
| 303 | {
|
---|
| 304 | char random[8] = {0x00,};
|
---|
[14] | 305 | sprintf(random, "%06d", generateSeed() % 1000000);
|
---|
[4] | 306 |
|
---|
| 307 | return random;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[6] | 310 | static std::string httpSMS(const Protocol::Message & parser)
|
---|
[4] | 311 | {
|
---|
[14] | 312 | std::string phone = parser.get<std::string>(ProtocolType::PHONE);
|
---|
[4] | 313 |
|
---|
| 314 | #define CRLF "\r\n"
|
---|
| 315 | std::string code = getRandomCode();
|
---|
| 316 | std::string url = "unsigned.kr";
|
---|
| 317 | std::string uri = "/~cheese/sms/req.php?to=" + phone + "&code=" + code;
|
---|
| 318 | std::string http =
|
---|
| 319 | "GET " + uri + " HTTP/1.1" CRLF
|
---|
| 320 | "Host: " + url + CRLF
|
---|
| 321 | "Accept: text/plain" CRLF
|
---|
| 322 | CRLF;
|
---|
| 323 |
|
---|
| 324 | cf::network::tcp smsSock;
|
---|
| 325 | cf::network::host smsServer(url, 80);
|
---|
| 326 |
|
---|
| 327 | smsSock.connect(smsServer);
|
---|
| 328 | smsSock.send(http);
|
---|
[14] | 329 | smsSock.receive();
|
---|
[4] | 330 | smsSock.close();
|
---|
| 331 |
|
---|
| 332 | return code;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[6] | 335 | static bool join(const Protocol::Message & parser, const std::string & sms, const std::string & address)
|
---|
[4] | 336 | throw (cf::exception)
|
---|
| 337 | {
|
---|
[13] | 338 | if (sms != parser.get<std::string>(ProtocolType::SMS))
|
---|
[4] | 339 | THROW_EXCEPTION("SMS is not same");
|
---|
| 340 |
|
---|
[13] | 341 | std::string id = parser.get<std::string>(ProtocolType::ID);
|
---|
| 342 | std::string pw = parser.get<std::string>(ProtocolType::PW);
|
---|
[4] | 343 |
|
---|
| 344 | return dbmgr.join(id, pw, sms, address);
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[14] | 347 | static bool login(const Protocol::Message & parser, cf::network::tcp & sock, cf::bin & key)
|
---|
[4] | 348 | throw (cf::exception)
|
---|
| 349 | {
|
---|
[13] | 350 | std::string id = parser.get<std::string>(ProtocolType::ID);
|
---|
| 351 | std::string pw = parser.get<std::string>(ProtocolType::PW);
|
---|
[4] | 352 |
|
---|
[14] | 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;
|
---|
[4] | 363 | }
|
---|
| 364 |
|
---|
[14] | 365 | static bool chat(const Protocol::Message & message)
|
---|
[4] | 366 | {
|
---|
| 367 | bool result = false;
|
---|
[14] | 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();
|
---|
[4] | 374 |
|
---|
[14] | 375 | for (size_t iter = 0; iter < idList.size(); iter++)
|
---|
[4] | 376 | {
|
---|
[14] | 377 | std::string id = idList[iter];
|
---|
| 378 |
|
---|
| 379 | if (sender != id && isOnline(id))
|
---|
| 380 | gOnlineUsers[id].sock->send(serialized);
|
---|
| 381 |
|
---|
[4] | 382 | result = true;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | return result;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[13] | 388 | static std::string createSessionID(std::string & idList)
|
---|
| 389 | {
|
---|
| 390 | cf::bin sessid;
|
---|
| 391 |
|
---|
| 392 | sessid = crypto().sha256(cf::bin(idList));
|
---|
| 393 |
|
---|
| 394 | return cf::codec::hex::getInstance()->encode(sessid);
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[14] | 397 | static bool openSession(const Protocol::Message & message)
|
---|
[13] | 398 | {
|
---|
[14] | 399 | Protocol::Message parser = message;
|
---|
[13] | 400 | bool result = false;
|
---|
| 401 | std::string sessid;
|
---|
| 402 | std::vector<std::string> idList = parser.getList<std::string>(ProtocolType::ID_LIST);
|
---|
[14] | 403 | std::string concat = joinStrings(idList);
|
---|
[13] | 404 |
|
---|
| 405 | sessid = createSessionID(concat);
|
---|
| 406 |
|
---|
[14] | 407 | parser.mObject[ProtocolType::SESSION_ID] = sessid;
|
---|
| 408 | std::string serialized = parser.serialize();
|
---|
| 409 |
|
---|
[13] | 410 | for (size_t iter = 0; iter < idList.size(); iter++)
|
---|
| 411 | {
|
---|
[14] | 412 | std::string id = idList[iter];
|
---|
| 413 | if (isOnline(id))
|
---|
| 414 | gOnlineUsers[id].sock->send(serialized);
|
---|
[13] | 415 |
|
---|
| 416 | result = true;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
[14] | 419 | gSessionMap[sessid] = idList;
|
---|
| 420 |
|
---|
[13] | 421 | return result;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[4] | 424 | static std::string keyExchange(const std::string sms, const std::string address)
|
---|
| 425 | {
|
---|
[13] | 426 | cf::bin sessKey;
|
---|
[4] | 427 |
|
---|
[13] | 428 | sessKey = crypto().sha256(cf::bin(sms + address));
|
---|
[4] | 429 |
|
---|
[13] | 430 | return cf::codec::hex::getInstance()->encode(sessKey);
|
---|
[4] | 431 | }
|
---|
| 432 |
|
---|
| 433 | static std::string workerInitiator(cf::network::tcp & sock)
|
---|
| 434 | {
|
---|
[14] | 435 | Protocol::Message parser;
|
---|
| 436 |
|
---|
[4] | 437 | try
|
---|
| 438 | {
|
---|
| 439 | std::string sms;
|
---|
[14] | 440 | std::string id;
|
---|
| 441 | bool loggedIn = false;
|
---|
[4] | 442 |
|
---|
| 443 | while (true)
|
---|
| 444 | {
|
---|
| 445 | parser.parse(sock.receive().toString());
|
---|
| 446 |
|
---|
[14] | 447 | if (parser.type() == ProtocolType::SMS)
|
---|
[4] | 448 | {
|
---|
| 449 | sms = httpSMS(parser);
|
---|
| 450 | }
|
---|
[14] | 451 | else if (parser.type() == ProtocolType::JOIN)
|
---|
[4] | 452 | {
|
---|
[14] | 453 | if (!join(parser, sms, sock.peer().address()))
|
---|
[13] | 454 | THROW_EXCEPTION("user(" << parser.get<std::string>(ProtocolType::ID) << ") cannot join");
|
---|
[4] | 455 | }
|
---|
[14] | 456 | else if (parser.type() == ProtocolType::LOGIN)
|
---|
[4] | 457 | {
|
---|
[14] | 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;
|
---|
[4] | 472 | }
|
---|
| 473 | }
|
---|
| 474 | catch (cf::exception & e)
|
---|
| 475 | {
|
---|
[14] | 476 | sock.send(Protocol::Response().result(parser.type(), false));
|
---|
[4] | 477 | FORWARD_EXCEPTION(e);
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | static int worker(void * arg)
|
---|
| 482 | {
|
---|
| 483 | Runner * runner = reinterpret_cast<Runner *>(arg);
|
---|
| 484 | cf::network::tcp * sock = runner->mSock;
|
---|
| 485 |
|
---|
| 486 | std::string id;
|
---|
| 487 |
|
---|
| 488 | try
|
---|
| 489 | {
|
---|
[14] | 490 | id = workerInitiator(*sock);
|
---|
[4] | 491 |
|
---|
[6] | 492 | Protocol::Response response;
|
---|
[4] | 493 | bool result = true;
|
---|
| 494 |
|
---|
| 495 | while (result)
|
---|
| 496 | {
|
---|
| 497 | std::string message = sock->receive().toString();
|
---|
[6] | 498 | Protocol::Message parser;
|
---|
[4] | 499 | parser.parse(message);
|
---|
| 500 |
|
---|
| 501 | LOG(message);
|
---|
| 502 |
|
---|
[14] | 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 | }
|
---|
[4] | 512 | }
|
---|
| 513 | }
|
---|
| 514 | catch (cf::exception & e)
|
---|
| 515 | {
|
---|
[14] | 516 | LOG(e.stackTrace());
|
---|
[4] | 517 | }
|
---|
| 518 |
|
---|
| 519 | logout(id);
|
---|
| 520 | delete runner;
|
---|
| 521 | return 0;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | static int server(unsigned short port)
|
---|
| 525 | {
|
---|
| 526 | cf::network::tcp sock;
|
---|
| 527 |
|
---|
| 528 | try
|
---|
| 529 | {
|
---|
| 530 | sock.bind(port);
|
---|
| 531 | sock.listen();
|
---|
| 532 | }
|
---|
| 533 | catch(cf::exception & e)
|
---|
| 534 | {
|
---|
| 535 | LOG(e.what());
|
---|
| 536 | return -1;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | while (true)
|
---|
| 540 | {
|
---|
| 541 | try
|
---|
| 542 | {
|
---|
| 543 | cf::network::tcp client = sock.accept();
|
---|
| 544 | Runner * runner = new(std::nothrow) Runner(client, worker);
|
---|
| 545 | if (!runner)
|
---|
| 546 | THROW_EXCEPTION("cannot create thread argument");
|
---|
| 547 | }
|
---|
| 548 | catch(cf::exception & e)
|
---|
| 549 | {
|
---|
| 550 | LOG(e.what());
|
---|
| 551 | }
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | return 0;
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | int main(int argc, char ** argv)
|
---|
| 558 | {
|
---|
| 559 | if (argc != 2)
|
---|
| 560 | {
|
---|
| 561 | std::cerr << "-_-^" << std::endl;
|
---|
| 562 | return -1;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | return server((unsigned short)atoi(argv[1]));
|
---|
| 566 | }
|
---|