Changeset 12 in chevmsgr
- Timestamp:
- 08/30/15 15:08:31 (9 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/msg.hpp
r11 r12 56 56 Message(); 57 57 58 inlinevoid parse(const std::string & message);58 void parse(const std::string & message); 59 59 60 60 template<typename T> 61 inlineT get(const std::string & key) const61 T get(const std::string & key) const 62 62 { 63 63 return (T)mObject[key]; … … 65 65 66 66 template<typename T> 67 inlinestd::vector<T> getList(const std::string & key) const67 std::vector<T> getList(const std::string & key) const 68 68 { 69 69 std::vector<T> ret; … … 76 76 } 77 77 78 inlinestd::vector<SFriend> getFriendList() const;78 std::vector<SFriend> getFriendList() const; 79 79 80 inlinestd::string type() const;80 std::string type() const; 81 81 }; 82 82 … … 93 93 94 94 public: 95 inlinevoid setUserID(const std::string & id);95 void setUserID(const std::string & id); 96 96 97 inlineconst std::string & getUserID();97 const std::string & getUserID(); 98 98 99 inlinevoid makeTemplate(json::Object & obj, const std::string & type) const;99 void makeTemplate(json::Object & obj, const std::string & type) const; 100 100 101 inlinestd::string sms(const std::string & phone) const;101 std::string sms(const std::string & phone) const; 102 102 103 inlinestd::string join(const std::string & id, const std::string & pw, const std::string & sms);103 std::string join(const std::string & id, const std::string & pw, const std::string & sms); 104 104 105 inlinestd::string login(const std::string & pw) const;105 std::string login(const std::string & pw) const; 106 106 107 inlinestd::string addFriend(const std::string & id) const;107 std::string addFriend(const std::string & id) const; 108 108 109 inlinestd::string openSession(const std::string & to) const;109 std::string openSession(const std::string & to) const; 110 110 111 inlinestd::string chat(const std::string & sessid, const std::string & message, const int sensitive) const;111 std::string chat(const std::string & sessid, const std::string & message, const int sensitive) const; 112 112 113 inlinestd::string getFriendList() const;113 std::string getFriendList() const; 114 114 }; 115 115 … … 117 117 { 118 118 public: 119 inlinevoid makeTemplate(json::Object & obj, const std::string & type) const;119 void makeTemplate(json::Object & obj, const std::string & type) const; 120 120 121 inlinestd::string result(const std::string & requestType, const bool status) const;121 std::string result(const std::string & requestType, const bool status) const; 122 122 123 inlinestd::string friendList(const std::vector<SFriend> & friendList) const;123 std::string friendList(const std::vector<SFriend> & friendList) const; 124 124 125 inlinestd::string openSession(const std::vector<std::string> & idList) const;125 std::string openSession(const std::vector<std::string> & idList) const; 126 126 }; 127 127 }; … … 156 156 157 157 for (size_t iter = 1; iter < strings.size(); iter++) 158 concat += DELIMITER +strings[iter];158 concat += strings[iter]; 159 159 160 160 return concat; -
trunk/msgclnt.cpp
r10 r12 9 9 // -------------------------------------------------------------- 10 10 11 typedef struct S WorkerArg11 typedef struct SMessageQWorkerArg 12 12 { 13 13 cf::network::tcp * socket; 14 14 MessageQ * messageQ; 15 } SWorkerArg; 16 17 int worker(void * arg) 18 { 19 SWorkerArg * inst = (SWorkerArg *) arg; 20 21 Protocol::Message parser; 15 } SMessageQWorkerArg; 16 17 typedef struct SCallbackWorkerArg 18 { 19 MessageQ * messageQ; 20 SCallback callback; 21 } SCallbackWorkerArg; 22 23 static int messageQworekr(void * arg) 24 { 25 SMessageQWorkerArg * inst = (SMessageQWorkerArg *)arg; 26 27 Protocol::Message message; 22 28 cf::bin raw; 23 29 … … 27 33 { 28 34 raw = inst->socket->receive(); 29 parser.parse(raw.toString());35 message.parse(raw.toString()); 30 36 } 31 37 catch (cf::exception & e) … … 38 44 } 39 45 40 inst->messageQ->push( parser);46 inst->messageQ->push(message); 41 47 } 42 48 … … 46 52 } 47 53 54 static inline SConversation toCnversation(const Protocol::Message & message) 55 { 56 SConversation c; 57 58 c.sessid = message.get<std::string>(ProtocolType::SESSION_ID); 59 c.message = message.get<std::string>(ProtocolType::MESSAGE); 60 c.sensitive = message.get<int>(ProtocolType::SENSITIVE); 61 c.isError = message.get<bool>(ProtocolType::RESULT); 62 63 c.from = message.get<std::string>(ProtocolType::FROM); 64 65 return c; 66 } 67 68 static inline SOpenSession toOpenSession(const Protocol::Message & message) 69 { 70 SOpenSession o; 71 72 o.sessid = message.get<std::string>(ProtocolType::SESSION_ID); 73 o.idList = message.getList<std::string>(ProtocolType::ID_LIST); 74 75 return o; 76 } 77 78 static int callbackWorker(void * arg) 79 { 80 SCallbackWorkerArg * inst = (SCallbackWorkerArg *)arg; 81 82 Protocol::Message message; 83 84 while (true) 85 { 86 message = inst->messageQ->pop(ProtocolType::CHAT, false); 87 if (message.type() != ProtocolType::NONE) 88 inst->callback.onChat(toCnversation(message)); 89 90 message = inst->messageQ->pop(ProtocolType::OPEN_SESSION, false); 91 if (message.type() != ProtocolType::NONE) 92 inst->callback.onOpenSession(toOpenSession(message)); 93 } 94 95 free(inst); 96 97 return 0; 98 } 99 48 100 // -------------------------------------------------------------- 49 101 50 void MessageQ::push(const Protocol::Message & parser)102 void MessageQ::push(const Protocol::Message & message) 51 103 { 52 104 mutex.lock(); 53 messageQ.push_back( parser);105 messageQ.push_back(message); 54 106 mutex.unlock(); 55 107 } 56 108 57 Protocol::Message MessageQ::pop(const std::string & requestType )58 { 59 while ( true)109 Protocol::Message MessageQ::pop(const std::string & requestType, bool isWait) 110 { 111 while (isWait) 60 112 { 61 113 SYNCHRONIZED(mutex) 62 114 { 63 Protocol::Message parser= messageQ.front();64 65 if ( parser.type() == requestType)115 Protocol::Message message = messageQ.front(); 116 117 if (message.type() == requestType) 66 118 { 67 119 messageQ.erase(messageQ.begin()); 68 120 69 return parser;121 return message; 70 122 } 71 123 } 72 124 } 125 126 return Protocol::Message(); // return dummy NONE 73 127 } 74 128 … … 76 130 77 131 chev::chev() 78 : listener(worker) 132 : listener(messageQworekr), 133 caller(callbackWorker) 79 134 { 80 135 } … … 96 151 socket.connect(host, port); 97 152 98 S WorkerArg * arg = (SWorkerArg *)malloc(sizeof(SWorkerArg));153 SMessageQWorkerArg * arg = (SMessageQWorkerArg *)malloc(sizeof(SMessageQWorkerArg)); 99 154 if (!arg) 100 155 return false; … … 203 258 try 204 259 { 260 idList.insert(idList.begin(), request.getUserID()); 261 205 262 if (idList.size() == 2) 206 263 std::sort(idList.begin(), idList.end()); 207 264 208 std::string concat = idList[0]; 209 210 for (int iter = 1; iter < idList.size(); iter++) 211 concat += DELIMITER + idList[iter]; 265 std::string concat = joinStrings(idList); 212 266 213 267 if (sessionMap.find(concat) == sessionMap.end()) … … 251 305 } 252 306 253 SConversation chev::listen() 254 { 255 SConversation c; 256 Protocol::Message message = messageQ.pop(ProtocolType::CHAT); 257 258 c.sessid = message.get<std::string>(ProtocolType::SESSION_ID); 259 c.message = message.get<std::string>(ProtocolType::MESSAGE); 260 c.sensitive = message.get<int>(ProtocolType::SENSITIVE); 261 c.isError = message.get<bool>(ProtocolType::RESULT); 262 263 c.from = message.get<std::string>(ProtocolType::FROM); 264 265 return c; 266 } 307 bool chev::listen(const SCallback & callback) 308 { 309 SCallbackWorkerArg * arg = (SCallbackWorkerArg *)malloc(sizeof(SCallbackWorkerArg)); 310 if (!arg) 311 return false; 312 313 arg->messageQ = &messageQ; 314 arg->callback = callback; 315 316 caller.start(arg); 317 318 return true; 319 } -
trunk/msgclnt.h
r8 r12 21 21 } SConversation; 22 22 23 typedef struct SOpenSession 24 { 25 std::string sessid; 26 std::vector<std::string> idList; 27 } SOpenSession; 28 29 typedef struct SCallback 30 { 31 int(*onChat)(SConversation &); 32 int(*onOpenSession)(SOpenSession &); 33 } SCallback; 34 23 35 class MessageQ 24 36 { … … 28 40 29 41 public: 30 void push(const Protocol::Message & parser);42 void push(const Protocol::Message & message); 31 43 32 Protocol::Message pop(const std::string & requestType );44 Protocol::Message pop(const std::string & requestType, bool isWait = true); 33 45 }; 34 46 … … 40 52 cf::network::tcp socket; 41 53 cf::task::thread listener; 54 cf::task::thread caller; 42 55 43 56 MessageQ messageQ; … … 68 81 bool tell(const SConversation & conversation); 69 82 70 SConversation listen();83 bool listen(const SCallback & callback); 71 84 };
Note:
See TracChangeset
for help on using the changeset viewer.