bool WindowListener::x11Event(XEvent *event) { if (event->type == PropertyNotify && event->xproperty.atom == XInternAtom(QX11Info::display(), "_NET_CLIENT_LIST", false)) { bool isNew; QList<Window> oldList = clientList; clientList = getClientList(); Window w = getDifferentWindow(clientList, oldList, &isNew); if (w != None) { if (isNew) emit windowCreated(w); else emit windowDeleted(w); } } return false; }
void MasterServer::idle(const U32 timeDelta) { mNetInterface->checkIncomingPackets(); mNetInterface->processConnections(); // Reread config file if(mReadConfigTimer.update(timeDelta)) { mSettings->readConfigFile(); mReadConfigTimer.reset(); if(motdHasChanged()) { broadcastMotd(); mLastMotd = mSettings->getMotd(); } } // Cleanup, cleanup, everybody cleanup! if(mCleanupTimer.update(timeDelta)) { MasterServerConnection::removeOldEntriesFromRatingsCache(); //<== need non-static access mCleanupTimer.reset(); } // Handle writing our JSON file mJsonWriteTimer.update(timeDelta); if(!mJsonWritingSuspended && mJsonWriteTimer.getCurrent() == 0) { MasterServerConnection::writeClientServerList_JSON(); mJsonWritingSuspended = true; // No more writes until this is cleared mJsonWriteTimer.reset(); // But reset the timer so it start ticking down even if we aren't writing } if(mPingGameJoltTimer.update(timeDelta)) { GameJolt::ping(mSettings, getClientList()); mPingGameJoltTimer.reset(); } if(mLoadEasterEggsTimer.update(timeDelta)) { mEasterEggBasket->loadEasterEggs(); mLoadEasterEggsTimer.reset(); } // Process connections -- cycle through them and check if any have timed out U32 currentTime = Platform::getRealMilliseconds(); for(S32 i = MasterServerConnection::gConnectList.size() - 1; i >= 0; i--) { GameConnectRequest *request = MasterServerConnection::gConnectList[i]; if(currentTime - request->requestTime > (U32)FIVE_SECONDS) { if(request->initiator.isValid()) { ByteBufferPtr ptr = new ByteBuffer((U8 *)MasterRequestTimedOut, strlen(MasterRequestTimedOut) + 1); request->initiator->m2cArrangedConnectionRejected(request->initiatorQueryId, ptr); // 0 = ReasonTimedOut request->initiator->removeConnectRequest(request); } if(request->host.isValid()) request->host->removeConnectRequest(request); MasterServerConnection::gConnectList.erase_fast(i); delete request; } } // Process any delayed disconnects; we use this to avoid repeating and flooding join / leave messages for(S32 i = MasterServerConnection::gLeaveChatTimerList.size() - 1; i >= 0; i--) { MasterServerConnection *c = MasterServerConnection::gLeaveChatTimerList[i]; if(!c || c->mLeaveLobbyChatTimer == 0) MasterServerConnection::gLeaveChatTimerList.erase(i); else { if(currentTime - c->mLeaveLobbyChatTimer > (U32)ONE_SECOND) { c->isInLobbyChat = false; const Vector<MasterServerConnection *> *clientList = getClientList(); for(S32 j = 0; j < clientList->size(); j++) if(clientList->get(j) != c && clientList->get(j)->isInLobbyChat) clientList->get(j)->m2cPlayerLeftGlobalChat(c->mPlayerOrServerName); MasterServerConnection::gLeaveChatTimerList.erase(i); } } } mDatabaseAccessThread->idle(); }
WindowListener::WindowListener() : clientList(getClientList()) { KSystemEventFilter::installEventFilter(this); }
int main(int argc, char* argv[]){ printf("\n" BAR " Running Chat Client %s. " BAR "\n\n", argv[1]); if(argc != 2){ printf("ERROR: You must enter 1 name (no spaces) on the command line.\n"); return EXIT_FAILURE; } int cliPort = getPortFromName(argv[1]); //get the client's port based on name printf("Setting up client for UDP... \n"); int UDPsocket = getUDPSocket(); //obtain socket for UDP struct sockaddr_in cliAddr; setCliAddr(cliPort, UDPsocket, &cliAddr); //set up clients address with port struct sockaddr_in servAddr; //this is the server's address setServAddr(&servAddr); //set up the server addrss /************************* Contact UDP Server **********************/ char sendBuf[BUF_SIZE]; memset(&sendBuf, 0, BUF_SIZE); char recvBuf[BUF_SIZE]; memset(&recvBuf, 0, BUF_SIZE); int addrLen = sizeof(servAddr); char currChar; bool using = true; //when using the program while(using){ //register with UDP server registerClient(UDPsocket, argv[1], (struct sockaddr*) &servAddr, sendBuf); //now we expect to get a response which is a list of people //this linked list is the register of who is an isn't connected CC reg = (CC) malloc(sizeof(struct ConnectedClient)); //acts as head strcpy(reg->name, "CLIENT LIST"); reg->next = NULL; //obtain and display list of clients from UDP server getClientList(reg, UDPsocket, recvBuf); /********************** TRANSITION TO TCP *********************************/ printf("Would you like to wait for a connection or make a connection? (w/m): "); char waitOrMake = getc(stdin); //get a new socket of TCP type int sockTCP = socket(AF_INET, SOCK_STREAM, 0); if(sockTCP < 0){ //if < 0 we have an error deRegister(UDPsocket, argv[1], (struct sockaddr*) &servAddr, sendBuf); perror("We have an error"); return EXIT_FAILURE; } struct sockaddr_in tcpAddr = cliAddr; tcpAddr.sin_port = htons(ntohs(cliAddr.sin_port) + 1); //TCP port will be UDPport++; int nowSocket; //this will be the socket we use to communicate //may come from accepting a connection or starting a connection //we are now going to make a connection char* connectee = NULL; //the name of the person we connect to if(waitOrMake == 'm'){ //if the client wants to make a connection printf("Pick the numbered client you wish to talk to: "); int clientNum = 0; scanf("%d", &clientNum); //get address of other client struct sockaddr_in* cliOfInterest = getAddr(reg, clientNum); connectee = getName(reg, cliOfInterest); printf("Making connection to %s\n", connectee); //make connection to other client printf("Trying connection to socket %d\n", ntohs(cliOfInterest->sin_port)); if(connect(sockTCP, (struct sockaddr*) cliOfInterest, sizeof(*cliOfInterest)) < 0){ deRegister(UDPsocket, argv[1], (struct sockaddr*) &servAddr, sendBuf); perror("We have an error"); return EXIT_FAILURE; } nowSocket = sockTCP; printf("Connection made!\n"); //notify conectee that I connected sprintf(sendBuf, "%s%s%s %s%s", CONN_FANFAIR, "\t\t", argv[1], CONN_NOTICE, CONN_FANFAIR); send(nowSocket, sendBuf, strlen(sendBuf), 0); } else { int currCliSocket; //this will be the socket we recieve a connection on struct sockaddr_in cliOfInterest; int len = sizeof(cliOfInterest); //bind the TCP address (different port) to the TCP socket bind(sockTCP, (struct sockaddr*) &tcpAddr, sizeof(tcpAddr)); printf("Now listening on port %d.\n", ntohs(tcpAddr.sin_port)); listen(sockTCP, 1); //being listening for connections //if we get a connection we record the socket we are using and the address currCliSocket = accept(sockTCP, (struct sockaddr*) &cliOfInterest, &len); if(currCliSocket >= 0){ nowSocket = currCliSocket; printf("Connection being made by another client ...\n"); //name is send over TCP by client } else { deRegister(UDPsocket, argv[1], (struct sockaddr*) &servAddr, sendBuf); perror("Incomming connection failed"); return EXIT_FAILURE; } } //at this point we have a TCP connection with another client //we are now going to run two threads simultaneously //the child thread will be to send chat //the parent thread will be to listen and display //spawn a child process int pid = fork(); if(pid < 0){ //if failed to fork deRegister(UDPsocket, argv[1], (struct sockaddr*) &servAddr, sendBuf); perror("Failed to fork!\n"); return EXIT_FAILURE; } else if(pid != 0) { /*********************** PARENT *****************/ printf("You are now in a chat room.\n" "Type \"" END_CONN "\" to exit the chat room.\n\n"); bool sending = true; //client still wants to chat bool firstTime = true; //start off first time while(sending){ //we will be ready to send until END_CONN //printf("You [%s]: ", timeStr()); printf("%s", KCYN); //make home client cyan memset(&sendBuf, 0, BUF_SIZE); //clear buffer if(firstTime){ //if just come from selection firstTime = false; //next time will not be first time currChar = getc(stdin); //we do another getc() to clear ENTER_KEY } currChar = getc(stdin); //get message to send int i = 0; //while we done hit enter and less than max buff (-1 for '\0') while(((int)currChar != ENTER_KEY) && (i < (BUF_SIZE - 2))){ sendBuf[i] = currChar; i++; currChar = getc(stdin); } sendBuf[i] = '\0'; //if typed in command to end client, end client if(strcmp(sendBuf, END_CONN) == 0){ sending = false; //end the chat room //send message of leaving chatroom send(nowSocket, LEAVING_CHATROOM_MSG, sizeof(LEAVING_CHATROOM_MSG), 0); kill(pid, SIGKILL); //kill child process } else { //send message to server send(nowSocket, sendBuf, BUF_SIZE, 0); } } } else { /*********************************** CHILD **************************/ int nread; char bufbuf[120]; while(true){ //always ready to receive chat --------> until shared memory memset(recvBuf, 0, BUF_SIZE); //clear buffer nread = read(nowSocket, recvBuf, BUF_SIZE); //receive a message recvBuf[nread] = '\0'; //manually terminate string if(strcmp(recvBuf, ACK_MSG) == 0){ //if we received an ACK, we print ACK printf("*** received ***\n\n"); //symbolizes reception of message } else if(strcmp(recvBuf, LEAVING_CHATROOM_MSG) == 0){ //if other client left printf("*** *** *** You're friend left the chatroom. *** *** ***\n"); deRegister(UDPsocket, argv[1], (struct sockaddr*) &servAddr, sendBuf); printf("Press ENTER to exit...\n"); } else if(strstr(recvBuf, CONN_NOTICE) != NULL){ //if it is a connection notice, don't ACK printf("%s%s%s\n\n", KMAG, recvBuf, KCYN); } else { //if normal message send(nowSocket, ACK_MSG, sizeof(ACK_MSG), 0); //send ack of recieved and displayed printf("%s%s%s\n\n", KMAG, recvBuf, KCYN); //weirdly, we needed to do '\n' at the end... } } } close(nowSocket); //kill TCP connection //disconnet before ending deRegister(UDPsocket, argv[1], (struct sockaddr*) &servAddr, sendBuf); } return EXIT_SUCCESS; }