示例#1
0
void Server::processSocket(cpp3ds::TcpSocket* socket)
{
	cpp3ds::Packet packet;
	cpp3ds::Socket::Status status = socket->receive(packet);
	if (status == cpp3ds::Socket::Done)
	{
		cpp3ds::Packet packetSend;
		NetworkEvent event;

		while (NetworkEvent::packetToEvent(packet, event))
		{
			if (!validateEvent(socket, event))
				continue;

			switch(event.type) {
				case NetworkEvent::Version:
					if (event.server.message.compare(SERVER_VERSION) != 0) {
						packet.clear();
						std::string message = _("Incompatible client version %s (needs %s)", event.server.message.c_str(), SERVER_VERSION);
						packet << NetworkEvent::ServerShutdown << message;
						socket->send(packet);
						removeSocket(socket);
					}
					break;
				case NetworkEvent::PlayerConnected: {
					std::cout << event.player.name << " connected." << std::endl;
					bool collision = false;
					for (auto& player : m_players) {
						if (event.player.name.compare(player.second.getName()) == 0) {
							collision = true;
							break;
						}
					}
					if (collision) {
						cpp3ds::Packet packet;
						packet << NetworkEvent::PlayerNameCollision << event.player.name;
						socket->send(packet);
						break;
					}
					Player player(event.player.name);
					m_players.emplace(socket, player);
					NetworkEvent::eventToPacket(event, packetSend);
					sendToAllSockets(packetSend);
					packetSend.clear();
					if (m_players.size() < MIN_PLAYERS) {
						sendWaitForPlayers(socket, MIN_PLAYERS);
					}
					break;
				}
				case NetworkEvent::Text: {
					NetworkEvent::eventToPacket(event, packetSend);
					if (m_mode != Play)
						break;
					std::string word = event.text.value.toAnsiString();
					std::transform(word.begin(), word.end(), word.begin(), ::tolower);
					if (word.compare(m_currentWord) == 0) {
						std::cout << event.text.name << " won!" << std::endl;
						packetSend << NetworkEvent::RoundWord << word << NetworkEvent::RoundWin << event.text.name;
						for (auto& player : m_players) {
							if (player.second.getName().compare(event.text.name) == 0)
								player.second.incrementScore();
							if (player.first == m_currentDrawer)
								player.second.incrementScore();
						}
						m_roundClock.restart();
						m_mode = Wait;
					}
					break;
				}
				case NetworkEvent::DrawMove:
				case NetworkEvent::DrawEndline:
					m_drawDataPacket << event.type << event.draw.x << event.draw.y;
					NetworkEvent::eventToPacket(event, packetSend);
					m_roundTimeoutClock.restart();
					break;
				case NetworkEvent::DrawClear:
					clearDrawData();
					NetworkEvent::eventToPacket(event, packetSend);
					m_roundTimeoutClock.restart();
					break;
				case NetworkEvent::DrawUndo: {
					m_drawDataPacket << event.type;
					NetworkEvent::eventToPacket(event, packetSend);
					m_roundTimeoutClock.restart();
					break;
				}
				case NetworkEvent::RoundPass:
					packetSend << NetworkEvent::RoundWord << m_currentWord;
					NetworkEvent::eventToPacket(event, packetSend);
					m_roundClock.restart();
					m_mode = Wait;
					break;
				case NetworkEvent::Ping:
					m_pingResponses[socket] = true;
					break;
				default:
					break;
			}
		}
		if (!packetSend.endOfPacket())
			sendToAllSockets(packetSend);

	} else if (status == cpp3ds::Socket::Disconnected || status == cpp3ds::Socket::Error) {
		removeSocket(socket);
	}
}
bool iCalConverter::convert(WString& errorDescription, long* errorCode) {
    errorDescription = TEXT("");
    *errorCode = ERROR_SUCCESS;

    if(!calendar && !iCalendar)
        return false;
    if(calendar && iCalendar)
        return true;
    if(calendar) {

        iCalendar = calendar->toString();

        if(!calendar->getProdID() || !calendar->getProdID()->getValue()) {
            *errorCode = ERROR_KEY_PROPERTY_MISSING;
            errorDescription = TEXT("'PRODID' property is missing");
            delete [] iCalendar; iCalendar = NULL;
            return false;
        }

        if(!calendar->getVersion() || !calendar->getVersion()->getValue()) {
            *errorCode = ERROR_KEY_PROPERTY_MISSING;
            errorDescription = TEXT("'VERSION' property is missing");
            delete [] iCalendar; iCalendar = NULL;
            return false;
        }
        if(calendar->getEvents())
            for(int i=0; i<calendar->getEvents()->size(); i++)
                if(!validateEvent(((Event*)calendar->getEvents()->get(i)), errorDescription, errorCode)) {
                    delete [] iCalendar; iCalendar = NULL;
                    return false;
            }
        if(calendar->getToDos())
            for(int i=0; i<calendar->getToDos()->size(); i++)
                if(!validateTodo(((ToDo*)calendar->getToDos()->get(i)), errorDescription, errorCode)) {
                    delete [] iCalendar; iCalendar = NULL;
                    return false;
                }

        return true;
   }
    if (iCalendar) {

        calendar = new Calendar();
        VObject* vo = VConverter::parse(iCalendar);
        if(!vo) {
            *errorCode = ERROR_PARSING_ERROR;
            errorDescription = TEXT("Invalid VObject returned");
            return false;
        }

        int n = vo->propertiesCount();

        if(wcscmp(vo->getProperty(0)->getName(), TEXT("BEGIN")) ||
            !vo->getProperty(0)->getValue() ||
            wcscmp(vo->getProperty(0)->getValue(), TEXT("VCALENDAR"))) {
                *errorCode = ERROR_KEY_PROPERTY_MISSING;
                errorDescription = TEXT("'BEGIN:VCALENDAR' property is missing");
                return false;
            }

        if(wcscmp(vo->getProperty(n-1)->getName(), TEXT("END")) ||
            !vo->getProperty(n-1)->getValue() ||
            wcscmp(vo->getProperty(n-1)->getValue(), TEXT("VCALENDAR"))) {
                *errorCode = ERROR_KEY_PROPERTY_MISSING;
                errorDescription = TEXT("'END:VCALENDAR' property is missing");
                return false;
            }

        if(!vo->containsProperty(TEXT("VERSION"))) {
            *errorCode = ERROR_KEY_PROPERTY_MISSING;
            errorDescription = TEXT("'VERSION' property is missing");
            return false;
        }

        if(vo->containsProperty(TEXT("VERSION")) &&
            (!vo->getProperty(TEXT("VERSION")) || wcscmp(vo->getProperty(TEXT("VERSION"))->getValue(), TEXT("2.0")))) {
                *errorCode = ERROR_ILLEGAL_VERSION_NUMBER;
                if(vo->getProperty(TEXT("VERSION"))) {
                    errorDescription = TEXT("Illegal version number : ");
                    errorDescription += vo->getProperty(TEXT("VERSION"))->getValue();
                }
                else {
                    errorDescription = TEXT("Illegal version number");
                }
                return false;
            }
        else {
            iCalProperty* prop = new iCalProperty(vo->getProperty(TEXT("VERSION"))->getValue());
            calendar->setVersion(*prop);
            vo->removeProperty(TEXT("VERSION"));
            delete prop;
        }

        if(!vo->containsProperty(TEXT("PRODID")) || !vo->getProperty(TEXT("PRODID"))) {
            *errorCode = ERROR_KEY_PROPERTY_MISSING;
            errorDescription = TEXT("'PRODID' property is missing");
            return false;
        }
        else {
            iCalProperty* prop = new iCalProperty(vo->getProperty(TEXT("PRODID"))->getValue());
            calendar->setProdID(*prop);
            vo->removeProperty(TEXT("PRODID"));
            delete prop;
        }

        if(vo->containsProperty(TEXT("CALSCALE")) ||
            vo->getProperty(TEXT("CALSCALE"))) {
                iCalProperty* prop = new iCalProperty(vo->getProperty(TEXT("CALSCALE"))->getValue());
                calendar->setCalScale(*prop);
                vo->removeProperty(TEXT("CALSCALE"));
                delete prop;
            }

        if(vo->containsProperty(TEXT("METHOD")) ||
            vo->getProperty(TEXT("METHOD"))) {
                iCalProperty* prop = new iCalProperty(vo->getProperty(TEXT("METHOD"))->getValue());
                calendar->setMethod(*prop);
                vo->removeProperty(TEXT("METHOD"));
                delete prop;
            }

        //extract VEVENTs from vo
        Event* ev;
        while((ev = extractEvent(vo, errorDescription, errorCode))) {
            if (!validateEvent(ev, errorDescription, errorCode)) {
                delete ev; ev = NULL;
                return false;
            }
            calendar->addEvent(ev);
            delete ev; ev = NULL;
        }

        //extract VTODOs from vo
        ToDo* task;
        while((task = extractTask(vo, errorDescription, errorCode))) {
            if (!validateTodo(task, errorDescription, errorCode)) {
                delete task; task = NULL;
                return false;
            }
            calendar->addToDo(task);
            delete task; task = NULL;
        }
    }

    return true;
}