void Wiimote::UpdateButtonsFromMessages(wm_core* buttons) { MessageStack messageStack = MessagePipe::Instance().FilteredStack('k', m_index + 1); if (!messageStack.IsEmpty()) { *buttons = 0; } std::string addRelease; while (!messageStack.IsEmpty()) { wm_core mask; std::string message = messageStack.Pop(); char code; int player; std::istringstream iss(message); iss >> player >> code >> mask; *buttons |= mask; if (mask == 0 && buttons != 0) { addRelease = message; } } if (addRelease.length() > 0) { messageStack.Push(addRelease); } }
int main(int argc, int argv[]) { std::cout << "running....\n"; Message message("topas", "Hello World"), m2("topas", "where are you?"), m3("guest", "Hi"); MessageStack messages; messages.append(&message); messages.append(&m2); messages.append(&m3); pthread_t tid; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); try { pthread_create(&tid, &attr, &game, NULL); while (true) { try{ ServerSocket *new_sock = new ServerSocket; server.accept(*new_sock); pthread_create(&tid, &attr, &rundle, new_sock); } catch (SocketException& e) { std::cout << "Exception was caught:" << e.description() << "\nExiting.\n"; } } } catch (SocketException& e) { std::cout << "Exception was caught:" << e.description() << "\nExiting.\n"; } return 0; }
void EventLoop::runOnce(bool isImmediately) { int retCount = epoll_wait(_epoll, _events, MAX_EPOLL_WAIT, isImmediately ? 0 : _timer.getNextExpireTime()); if (retCount == -1) { if (errno != EINTR) { LCW("EventLoop::runOnce[this0x" << this << "] epoll_wait err! errno=" << strerror(errno) << logSection()); return; //! error } return; } //check timer { _timer.checkTimer(); if (retCount == 0) return;//timeout } for (int i=0; i<retCount; i++) { EventData * pEvent = (EventData *)_events[i].data.ptr; //tagHandle type if (pEvent->_type == EventData::REG_ZSUMMER) { { char buf[200]; while (recv(pEvent->_fd, buf, 200, 0) > 0); } MessageStack msgs; _stackMessagesLock.lock(); msgs.swap(_stackMessages); _stackMessagesLock.unlock(); for (auto pfunc : msgs) { _OnPostHandler * p = (_OnPostHandler*)pfunc; try { (*p)(); } catch (std::runtime_error e) { LCW("OnPostHandler have runtime_error exception. err=" << e.what()); } catch (...) { LCW("OnPostHandler have unknown exception."); } delete p; } } else if (pEvent->_type == EventData::REG_TCP_ACCEPT) { if (pEvent->_tcpacceptPtr) { pEvent->_tcpacceptPtr->onEPOLLMessage(_events[i].events); } } else if (pEvent->_type == EventData::REG_TCP_SOCKET) { if (pEvent->_tcpSocketPtr) { pEvent->_tcpSocketPtr->onEPOLLMessage(_events[i].events); } } else if (pEvent->_type == EventData::REG_UDP_SOCKET) { if (pEvent->_udpsocketPtr) { pEvent->_udpsocketPtr->onEPOLLMessage(_events[i].events); } } else { LCE("EventLoop::runOnce[this0x" << this << "] check register event type failed !! type=" << pEvent->_type << logSection()); } } }
void EventLoop::runOnce(bool isImmediately) { int retCount = epoll_wait(_epoll, _events, 1000, isImmediately ? 0 : _timer.getNextExpireTime()); if (retCount == -1) { if (errno != EINTR) { LCW("EventLoop::runOnce[this0x" << this << "] epoll_wait err! errno=" << strerror(errno) << logSection()); return; //! error } return; } //check timer { _timer.checkTimer(); if (retCount == 0) return;//timeout } for (int i=0; i<retCount; i++) { int eventflag = _events[i].events; tagRegister * pReg = (tagRegister *)_events[i].data.ptr; //tagHandle type if (pReg->_type == tagRegister::REG_ZSUMMER) { char buf[1000]; while (recv(pReg->_fd, buf, 1000, 0) > 0); MessageStack msgs; _stackMessagesLock.lock(); msgs.swap(_stackMessages); _stackMessagesLock.unlock(); for (auto pfunc : msgs) { _OnPostHandler * p = (_OnPostHandler*)pfunc; try { (*p)(); } catch (std::runtime_error e) { LCW("OnPostHandler have runtime_error exception. err=" << e.what()); } catch (...) { LCW("OnPostHandler have unknown exception."); } delete p; } } else if (pReg->_type == tagRegister::REG_TCP_ACCEPT) { if (eventflag & EPOLLIN) { if (pReg->_tcpacceptPtr) { pReg->_tcpacceptPtr->onEPOLLMessage(true); } } else if (eventflag & EPOLLERR || eventflag & EPOLLHUP) { if (pReg->_tcpacceptPtr) { pReg->_tcpacceptPtr->onEPOLLMessage(false); } } } else if (pReg->_type == tagRegister::REG_TCP_SOCKET) { if (eventflag & EPOLLERR || eventflag & EPOLLHUP) { if (pReg->_tcpSocketConnectPtr) { pReg->_tcpSocketConnectPtr->onEPOLLMessage(EPOLLOUT, true); } else if (pReg->_tcpSocketRecvPtr) { pReg->_tcpSocketRecvPtr->onEPOLLMessage(EPOLLIN, true); } else if (pReg->_tcpSocketSendPtr) { pReg->_tcpSocketSendPtr->onEPOLLMessage(EPOLLOUT, true); } } else if (eventflag & EPOLLIN) { if (pReg->_tcpSocketRecvPtr) { pReg->_tcpSocketRecvPtr->onEPOLLMessage(EPOLLIN, false); } } else if (eventflag & EPOLLOUT) { if (pReg->_tcpSocketConnectPtr) { pReg->_tcpSocketConnectPtr->onEPOLLMessage(EPOLLOUT, false); } else if (pReg->_tcpSocketSendPtr) { pReg->_tcpSocketSendPtr->onEPOLLMessage(EPOLLOUT, false); } } } else if (pReg->_type == tagRegister::REG_UDP_SOCKET) { if (pReg->_udpsocketPtr) { pReg->_udpsocketPtr->onEPOLLMessage(pReg->_type, eventflag); } } else { LCE("EventLoop::runOnce[this0x" << this << "] check register event type failed !! type=" << pReg->_type << logSection()); } } }
void* rundle(void* socket){ ServerSocket *client = (ServerSocket *) socket; std::string data; MessageStack messages; UserStack users; User *user; try { user = init_connection(client); } catch (SocketException& e) { printf("Error initial connection in function rundle()\n"); return NULL; } try { users.join(user); user->send("Добро пожаловать, о "); user->send("\n---- // enter \"quit\" for exit\n\n"); user->send(messages.viewLast()); while (true) { data = *user->recive("> "); data = trimr(data); if ( data.compare("quit") == 0) { user->send("Bye! До скорых встреч"); sleep(2); break; } if ( data.compare("help") == 0){ user->send("\r==== Список пользователей ====\n"); } if ( data.compare("who") == 0){ user->send("\r==== Список пользователей ====\n"); std::vector <User*> list = users.list(); for (int i = list.size() - 1; i >= 0; --i){ user->send(list[i]->id); user->send(": "); user->send(list[i]->getName()); user->send("\n"); } continue; } if ( data.length() == 0){ continue; } Message *message = new Message(user->getName(), data); messages.append( message ); users.sendMessage(message->get().c_str()); } } catch (SocketException& e) { std::cout << "Exception was caught:" << e.description() << "\nExiting.\n"; } users.disconnect(user); delete client; return NULL; }