Beispiel #1
0
Server::Server(QString ipAddr, quint32 port, QString purpose)
:  tcpServer(0), networkSession(0)
{
    Server::clientConnection = NULL;
    Server::purpose = purpose;
    Server::port = port;
    QNetworkConfigurationManager manager;
    if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
        // Get saved network configuration
        QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
        settings.beginGroup(QLatin1String("QtNetwork"));
        const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
        settings.endGroup();

        // If the saved network configuration is not currently discovered use the system default
        QNetworkConfiguration config = manager.configurationFromIdentifier(id);
        if ((config.state() & QNetworkConfiguration::Discovered) !=
            QNetworkConfiguration::Discovered) {
            config = manager.defaultConfiguration();
        }

        networkSession = new QNetworkSession(config, this);
        connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));

        //statusLabel->setText(tr("Opening network session."));
        networkSession->open();
    } else {
        sessionOpened();
    }

        fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
                 << tr("You've got to think about tomorrow.")
                 << tr("You will be surprised by a loud noise.")
                 << tr("You will feel hungry again in another hour.")
                 << tr("You might have mail.")
                 << tr("You cannot kill time without injuring eternity.")
                 << tr("Computers are not intelligent. They only think they are.");
        //connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
        connect(tcpServer, SIGNAL(newConnection()), this, SLOT(openNewConnection()));
        //connect(tcpServer, &QTcpServer::newConnection, this, &Server::openNewConnection);
}
///<summary> Handles the network negotiation of an outgoing connection. Uses synchronous reads and sends. </summary>
///<param name='ipAddress'> The IP address string to connect to. </param>
///<param name='l4Port'> The transport layer port to connection to. </param>
///<param name='protocol'> The protocol the connection will use. </param>
///<param name='out_connectionPointer'> (OUT) The connection object for the newly initialized connection. </param>
///<returns> Error code on failure, result code from remote server on success. <returns>
uint16_t initializeOutgoingConnection(char *ipAddress, uint16_t l4Port, uint8_t protocol, POLYM_CONNECTION_INFO **out_connectionInfo)
{
	POLYM_CONNECTION_INFO *connection_info;

	uint16_t connectionID = allocateNewPeer(&connection_info);

	char l4PortString[6];
	_itoa(l4Port, l4PortString, 10);
	void *connection = openNewConnection(ipAddress, l4PortString, &connection_info, protocol);

	if (connection == NULL)
		return POLY_COMMAND_CONNECT_ERROR_CONNECTION_FAIL;

	uint8_t buffer[500];

	if (6 != sockRecv(connection, buffer, 6)) // recieve "POLY v" from greeting
	{
		sendErrorCode(POLY_COMMAND_CONNECT_ERROR_CONNECTION_FAIL, connection);
		removeConnection(connection_info);
		return POLY_COMMAND_CONNECT_ERROR_CONNECTION_FAIL;
	}

	int index = 6;
	for (; index <= POLYM_GREETING_MAX_LENGTH; ++index) // recieve the rest of the greeting
	{

		if (1 != sockRecv(connection, &buffer[index], 1))
		{
			sendErrorCode(POLY_COMMAND_CONNECT_ERROR_CONNECTION_FAIL, connection);
			removeConnection(connection_info);
			return POLY_COMMAND_CONNECT_ERROR_CONNECTION_FAIL;
		}

		if (buffer[index] == '\n')
		{
			break;
		}
	}

	if (index > POLYM_GREETING_MAX_LENGTH) // greeting is too long, close the connection
		closeConnectionSocket(connection);

	insertShortIntoBuffer(buffer, POLY_ENC_NONE); // encryption mode
	insertShortIntoBuffer(&buffer[2], POLY_REALM_PEER); // realm code

	sockSend(connection, buffer, 4); // send the connection request

	if (2 != sockRecv(connection, buffer, 2))
	{
		sendErrorCode(POLY_COMMAND_CONNECT_ERROR_CONNECTION_FAIL, connection);
		removeConnection(connection_info);
		return POLY_COMMAND_CONNECT_ERROR_CONNECTION_FAIL;
	}

	initializeNewPeerInfo(connection_info, connection, connectionID);

	// TODO: send service update request

	*out_connectionInfo = connection_info;
	return getShortFromBuffer(buffer); //return result code recieved from remote server

}