int ServerConnection::getFirstBrankIndex_(){ for(int i = 0; i < getClientNum(); ++i){ if(!connected_[i]){ return i; } } if(getClientNum() < MAX_CLIENT_NUM){ clients_.push_back(static_cast<Connection *>(NULL)); ips_.push_back(IPDATA()); connected_.push_back(static_cast<char>(false)); return getClientNum() - 1; } return -1; }
void GameRoomManager::notifyAvailableGameRooms(const id_t client_id) { auto client = ClientManager::getInstance()->getClient(client_id); if (!client) return; RoomResponse noti; bool new_room_flag = true; int room_num = 0; for (auto& pair : rooms) { auto gameroom = pair.second; if (gameroom->isFull() || gameroom->isPlaying()) continue; noti.room_list[room_num++] = pair.first; if (gameroom->getClientNum() == 0) new_room_flag = false; if (room_num >= 32) break; } if (new_room_flag) noti.room_list[room_num++] = createGameRoom(); noti.room_num = room_num; client->sendPacket(noti); }
void ServerConnection::acceptNewConnection(){ int netHandle = GetNewAcceptNetWork(); if(netHandle != -1){ Connection *newConnection = new Connection(netHandle); int tmp; IPDATA ip; switch(status){ case WAIT_CONNECTION_MODE: tmp = getFirstBrankIndex_(); if(tmp == -1){ newConnection->send("ERROR 01: TOO MANY CLIENTS"); delete newConnection; CloseNetWork(netHandle); return; } clients_[tmp] = newConnection; GetNetWorkIP(netHandle, &ips_[tmp]); connected_[tmp] = true; break; case REJECT_CONNECTION_MODE: GetNetWorkIP(netHandle, &ip); for(int i = 0; i < getClientNum(); ++i){ if(!connected_[i] && isSameIP(ip, ips_[i])){ clients_[i] = newConnection; connected_[i] = true; } } newConnection->send("ERROR 02: CONNECTION IS REJECTED"); delete newConnection; CloseNetWork(netHandle); break; } } }
int ServerConnection::findClientFromNetHandle_(int netHandle)const{ for(int i = 0; i < getClientNum(); ++i){ if(connected_[i] && clients_[i]->getNetHandle() == netHandle){ return i; } } return -1; }
int ServerConnection::send(vector<int> &message){ int result = 0; for(int i = 0; i < getClientNum(); ++i){ int tmp = send(i, message); if(tmp < 0){ result = tmp; } } return result; }
int ServerConnection::send(const string &str){ int result = 0; for(int i = 0; i < getClientNum(); ++i){ int tmp = send(i, str); if(tmp < 0){ result = tmp; } } return result; }
static void * clientConn(void* info){ Message* msg; int pid = ((Message*)info)->referer; int id = getClientNum(); addClientNode(pid, id); sendData(pid, fillMessageData("client", "success", ""), sizeof(Message)); while(1){ printf("Waiting for new Message\n"); Message* data = (Message*)listenMessage(pid, sizeof(Message)); if(data != NULL){ if(strcmp(data->resource, "mail") == 0){ if(strcmp(data->method, "send") == 0){ clientReciveMails(data, id); }else if(strcmp(data->method, "receive") == 0){ clientSendEmails(data, id); } }else if(strcmp(data->resource, "client") == 0){ if(strcmp(data->method, "close") == 0){ printf("%d\n", pid); closeConnection(pid); break; } }else{ pushWait(id,data); while((msg = grabMessage(pid)) != NULL){ sendData(pid, msg, sizeof(Message)); } } } } printf("Salio \n"); return NULL; }