void TcpThread::run() { while (true) { ClientThread* c = clientThreads.add(new ClientThread(crypto)); if (acceptor_->accept(c->getTCPStream())) { //thread per connection threads.add(new p2p::Thread(*c)); } else { std::cout << "Error: Accept failed." << std::endl; return ; } } }
void Server::incomingConnection(int socketDescriptor) { ClientThread *client = new ClientThread(socketDescriptor); connect(client, SIGNAL(newClientMessage(QString)), this, SLOT(processNewClient())); connect(client, SIGNAL(updateClientPPCStatusMessage(QString)), this, SLOT(processUpdateClientPPCStatus())); connect(client, SIGNAL(applyPPCMessage(QString, QString)), this, SIGNAL(applyPPCMessage(QString, QString))); connect(client, SIGNAL(giveBackPPCMessage(QString, QString)), this, SIGNAL(giveBackPPCMessage(QString, QString))); connect(client, SIGNAL(giveBackPPCMessageResult(QString, QString)), this, SIGNAL(giveBackPPCMessageResult(QString, QString))); connect(client, SIGNAL(clientClosed(int)), this, SLOT(threadFinished(int)), Qt::DirectConnection); connect(client, SIGNAL(finished()), client, SLOT(deleteLater())); clientList.append(client); client->start(); }
void Server::sendMsg(const QString &msg, const QString &msgContent) { ClientThread *clSender = (ClientThread*)sender(); QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_1); qDebug() << "Server msg: " << msg; out << quint16(0) << msg << msgContent; out.device()->seek(0); out << quint16(block.size() - sizeof(quint16)); (clSender->getClient())->write(block); }
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; qInstallMessageHandler(myMessageOutput); w.show(); ct.start(); return a.exec(); }
void SDLAsyncRunner::RunClientLoop(client::IRenderer *renderer, client::IAudioDevice *audio) { client::AsyncRenderer *asyncRenderer = new client::AsyncRenderer(renderer, DispatchQueue::GetThreadQueue()); modState = 0; ClientThread *cliThread = new ClientThread(); cliThread->runner = this; cliThread->renderer = asyncRenderer; cliThread->audio = audio; cliThread->Start(); bool editing = false; bool absoluteMouseCoord = true; SPLog("Main event loop started"); try{ while(cliThread->IsAlive()) { modState = SDLRunner::GetModState(); if(currentView){ class SDLEventDispatch: public ConcurrentDispatch { SDLAsyncRunner *runner; SDL_Event ev; public: SDLEventDispatch(SDLAsyncRunner *runner, const SDL_Event& ev): runner(runner), ev(ev){ } virtual void Run() { View *view = runner->currentView; if(view){ runner->ProcessEvent(ev, view); } } }; SDL_Event event; if(SDL_WaitEvent(&event)){ { SDLEventDispatch *disp = new SDLEventDispatch(this, event); // FIXME: cliQueue may be deleted.. if(cliQueue){ disp->StartOn(cliQueue); disp->Release(); }else{ delete disp; } } while(SDL_PollEvent(&event)) { SDLEventDispatch *disp = new SDLEventDispatch(this, event); // FIXME: cliQueue may be deleted.. if(cliQueue){ disp->StartOn(cliQueue); disp->Release(); }else{ delete disp; } } } }else{ SDL_Event event; SDL_WaitEvent(&event); if(event.type == SDL_QUIT){ break; } } PulledState state; { AutoLocker guard(&stateMutex); state = this->state; } bool ed = state.acceptsTextInput; if(ed && !editing) { SDL_StartTextInput(); }else if(!ed && editing){ SDL_StopTextInput(); } editing = ed; if(editing){ SDL_SetTextInputRect(&state.textInputRect); } bool ab = state.needsAbsoluteMouseCoord; if(ab != absoluteMouseCoord) { absoluteMouseCoord = ab; SDL_SetRelativeMouseMode(absoluteMouseCoord?SDL_FALSE:SDL_TRUE); } DispatchQueue::GetThreadQueue()->ProcessQueue(); } }catch(const std::exception& ex){ rendererErrorOccured = true; SPLog("Renderer error:\n%s", ex.what()); cliThread->MarkForAutoDeletion(); SPLog("Main event loop terminated"); throw; } SPLog("Main event loop ended"); if(!clientError.empty()){ SPLog("Client reported an error: \n%s", clientError.c_str()); SPRaise("Client error:\n%s", clientError.c_str()); } }
void ClientServer::incomingConnection(int socketDescriptor)//建立新线程 { ClientThread *thread = new ClientThread(socketDescriptor, this); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); }
void FTPCore::run() { ClientThread *clientthread; QList<ClientThread*> connections; if (!InitWinsock()) { emit onerror("WinSock init failed"); qDebug() << "Winsock stopped"; emit onstopped(); return; } if ( INVALID_SOCKET == (msocket=socket(AF_INET,SOCK_STREAM,0))) { emit onerror("Error socket " + QString("%1").arg(WSAGetLastError())); WSACleanup(); qDebug() << "Winsock stopped"; emit onstopped(); return; } sockaddr_in local_addr; local_addr.sin_family=AF_INET; local_addr.sin_port=htons(CONTROL_PORT); local_addr.sin_addr.s_addr=0; // вызываем bind для связывания if (SOCKET_ERROR == bind(msocket,(sockaddr *) &local_addr, sizeof(local_addr))) { emit onerror("Error bind " + QString("%1").arg(WSAGetLastError())); closesocket(msocket); WSACleanup(); qDebug() << "Winsock stopped"; emit onstopped(); return; } if (listen(msocket, MAX_CLIENTS)) { // Ошибка emit onerror("Error listen " + QString("%1").arg(WSAGetLastError())); closesocket(msocket); WSACleanup(); emit onstopped(); qDebug() << "Winsock stopped"; return; } SOCKET client_socket; // сокет для клиента sockaddr_in client_addr; // адрес клиента int client_addr_size=sizeof(client_addr); u_long flag = 1; ioctlsocket(msocket, FIONBIO, &flag); fd_set read_s; // Множество timeval time_out; // Таймаут int res; emit onstarted(); while (true) { FD_ZERO (&read_s); // Обнуляем множество FD_SET (msocket, &read_s); // Заносим в него наш сокет time_out.tv_sec = 0; time_out.tv_usec = 500000; //Таймаут 0.5 секунды. if (SOCKET_ERROR == (res = select (0, &read_s, NULL, NULL, &time_out) ) ) { emit onerror("Select error in FTPCore"); closesocket(msocket); WSACleanup(); qDebug() << "Winsock stopped"; emit onstopped(); return; } if ((res!=0) && (FD_ISSET (msocket, &read_s)) ) // Использую FD_ISSET только для примера! :) { client_socket=accept(msocket, (sockaddr *)&client_addr, &client_addr_size); QString clientAddr = QString("%1:%2").arg(inet_ntoa(client_addr.sin_addr)).arg(client_addr.sin_port); emit onnewconnection(clientAddr); clientthread = new ClientThread(client_socket, clientAddr); connections.append(clientthread); connect(clientthread, SIGNAL(oncloseconnection(QString)),SIGNAL(oncloseconnection(QString))); clientthread->start(); } if (terminated) { break; } } foreach(ClientThread *client , connections) { client->closeconnection(); qDebug() << "Close connection"; client->wait(); qDebug() << "wait"; delete client; }
//Name: runClient //Parameters: 1 // toPass - ???????? //Returns: ID for client thread //Purpose: Start up the client thread DWORD WINAPI runClient(LPVOID toPass) { ClientThread newThread; newThread.run(toPass); return 0; }
void Server::processUpdateClientPPCStatus() { ClientThread *clSender = (ClientThread*)sender(); sendPPCStatus(clSender->getClient()); }