Пример #1
0
void AudioSDL::doQueueAudio(gsl::span<const gsl::byte> data) 
{
	std::vector<unsigned char> tmp(data.size_bytes());
	memcpy(tmp.data(), data.data(), data.size_bytes());

	std::unique_lock<std::mutex> lock(mutex);
	queuedSize += data.size_bytes();
	audioQueue.push_back(std::move(tmp));
}
Пример #2
0
void GLBuffer::setData(gsl::span<const gsl::byte> data)
{
	/*
	if (size < size_t(data.size_bytes())) {
		glBufferData(target, data.size_bytes(), data.data(), GL_STREAM_DRAW);
		size = data.size_bytes();
	} else {
		glBufferData(target, size, nullptr, GL_STREAM_DRAW);
		glBufferSubData(target, 0, data.size_bytes(), data.data());
	}
	*/

	if (size < size_t(data.size_bytes())) {
		size = nextPowerOf2(data.size_bytes());
	}
	glBufferData(target, size, nullptr, GL_STREAM_DRAW);
	glBufferSubData(target, 0, data.size_bytes(), data.data());

	glCheckError();
}
Пример #3
0
	IndexBufferPtr RenderingDevice::CreateIndexBuffer(gsl::span<const uint16_t> data) {
		CComPtr<IDirect3DIndexBuffer9> result;

		D3DLOG(mDevice->CreateIndexBuffer(data.size_bytes(),
			D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
			D3DFMT_INDEX16,
			D3DPOOL_DEFAULT,
			&result, nullptr));

		void* dataOut;
		D3DLOG(result->Lock(0, data.size(), &dataOut, D3DLOCK_DISCARD));

		memcpy(dataOut, &data[0], data.size() * sizeof(uint16_t));

		D3DLOG(result->Unlock());

		return std::make_shared<IndexBuffer>(result, data.size());
	}
Пример #4
0
void NetworkService::receivePacket(gsl::span<gsl::byte> received, std::string* error)
{
	if (error) {
		std::cout << "Error receiving packet: " << (*error) << std::endl;
		// Find the owner of this remote endpoint
		for (auto& conn : pimpl->activeConnections) {
			if (conn.second->matchesEndpoint(pimpl->remoteEndpoint)) {
				conn.second->setError(*error);
				conn.second->close();
			}
		}
		return;
	}

	if (received.size_bytes() == 0) {
		return;
	}

	// Read connection id
	short id = -1;
	std::array<unsigned char, 2> bytes;
	auto dst = gsl::as_writeable_bytes(gsl::span<unsigned char, 2>(bytes));
	dst[0] = received[0];
	if (bytes[0] & 0x80) {
		if (received.size_bytes() < 2) {
			// Invalid header
			std::cout << "Invalid header\n";
			return;
		}
		dst[1] = received[1];
		received = received.subspan(2);
		id = short(bytes[0] & 0x7F) | short(bytes[1]);
	} else {
		received = received.subspan(1);
		id = short(bytes[0]);
	}

	// No connection id, check if it's a connection request
	if (id == 0 && isValidConnectionRequest(received)) {
		auto& pending = pimpl->pendingIncomingConnections;
		if (std::find(pending.begin(), pending.end(), pimpl->remoteEndpoint) == pending.end()) {
			pending.push_back(pimpl->remoteEndpoint);
		}
		// Pending connection is valid
		return;
	}

	// Find the owner of this remote endpoint
	auto conn = pimpl->activeConnections.find(id);
	if (conn == pimpl->activeConnections.end()) {
		// Connection doesn't exist, but check the pending slot
		conn = pimpl->activeConnections.find(0);
		if (conn == pimpl->activeConnections.end()) {
			// Nope, give up
			return;
		}
	}

	// Validate that this connection is who it claims to be
	if (conn->second->matchesEndpoint(pimpl->remoteEndpoint)) {
		auto connection = conn->second;

		if (error) {
			// Close the connection if there was an error
			connection->setError(*error);
			connection->close();
		} else {
			try {
				connection->onReceive(received);

				if (conn->first == 0) {
					// Hold on, we're still on 0, re-bind to the id
					short newId = connection->getConnectionId();
					if (newId != 0) {
						pimpl->activeConnections[newId] = connection;
						pimpl->activeConnections.erase(conn);
					}
				}
			} catch (std::exception& e) {
				connection->setError(e.what());
				connection->close();
			} catch (...) {
				connection->setError("Unknown error receiving packet.");
				connection->close();
			}
		}
	}
}