Пример #1
0
int UDPManager::sendDatagram(DataBuffer * data, unsigned long ipAddress, unsigned long port, unsigned long frameCount) {
	if (initErrors || data == NULL || port == 0) return -1;

	sockaddr_in otherAddress;
	otherAddress.sin_family = AF_INET;
	otherAddress.sin_addr.s_addr = ipAddress;
	otherAddress.sin_port = port;

	int packetSize = data->getSize();
	DataBuffer * sendBuffer = new DataBuffer(packetSize + 4);
	//sendBuffer->copy(0, &packetSize, sizeof(int));
	sendBuffer->copy(0, &frameCount, 4);
	sendBuffer->copy(4, data->getData(), data->getSize());

	if (sendto(sock, sendBuffer->getData(), sendBuffer->getSize(), 0, (struct sockaddr *)&otherAddress, sizeof(otherAddress)) < 0) {
		perror("UDP sendto() failed\n");
		delete sendBuffer;
		return -1;
	}

	//printf("UDP Send:");
	//Utilities::printfNetworkPacket(sendBuffer->getData(), sendBuffer->getSize());
	delete sendBuffer;

	return 0;
}
Пример #2
0
DataBuffer * NetworkFunctions::createClearObjectsBuffer()
{
	DataBuffer * tempBuffer = new DataBuffer(5);
	int functionID = EVENT_CLEAR_OBJECTS;
	tempBuffer->copy(0, &functionID, 4);
	tempBuffer->copy(4, "\n", 1);
	return tempBuffer;
}
Пример #3
0
DataBuffer * NetworkFunctions::createDeleteObjectBuffer(unsigned long netID)
{
	DataBuffer * tempBuffer = new DataBuffer(9);
	int functionID = EVENT_DELETE_GAME_OBJECT;
	tempBuffer->copy(0, &functionID, 4);
	tempBuffer->copy(4, &netID, 4);
	tempBuffer->copy(8, "\n", 1);
	return tempBuffer;
}
Пример #4
0
DataBuffer * NetworkFunctions::createUpdateObjectBuffer(unsigned long netID)
{
	GameObject * tempObject = gameObjects->getValue(netID);
	if (tempObject == NULL) return NULL;
	DataBuffer * tempBuffer = tempObject->serialize();
	int functionIndex = EVENT_UPDATE_GAME_OBJECT;
	tempBuffer->copy(0, &functionIndex, 4);
	tempBuffer->copy(4, &netID, 4);
	return tempBuffer;
}
Пример #5
0
DataBuffer * NetworkFunctions::createSpawnHalfHealthKitBuffer(glm::vec3 position, unsigned long netID)
{
	DataBuffer * tempBuffer = new DataBuffer(17);
	int functionIndex = EVENT_SPAWN_HALF_HEALTH_KIT;
	tempBuffer->copy(0, &functionIndex, 4);
	tempBuffer->copy(4, &netID, 4);
	tempBuffer->copy(8, &position.x, 4);
	tempBuffer->copy(12, &position.y, 4);
	tempBuffer->copy(16, &position.z, 4);
	tempBuffer->copy(20, "\n", 1);
	return tempBuffer;
}
Пример #6
0
DataBuffer * NetworkFunctions::createChangeSpellBuffer(int playerID, int elementIndex, int subID) //SubID refers to the index in the element vector, IE the Xth fire spell
{
	if (playerID < 0 || playerID >= players->size() || players->at(playerID) == NULL) return NULL;
	if (elementIndex < 0 || elementIndex > 4) return NULL;

	DataBuffer * tempBuffer = new DataBuffer(17);
	int functionIndex = EVENT_CHANGE_SPELL;
	tempBuffer->copy(0, &functionIndex, 4);
	tempBuffer->copy(4, &playerID, 4);
	tempBuffer->copy(8, &elementIndex, 4);
	tempBuffer->copy(12, &subID, 4);
	tempBuffer->copy(16, "\n", 1);
	return tempBuffer;
}
Пример #7
0
DataBuffer * Spell::createDamageBuffer(int playerID, int targetID, int damage, int spellID)
{
	if (spellID < 0 || spellID >= SPELLID_COUNT) return NULL;

	DataBuffer * tempBuffer = new DataBuffer(21);
	int functionIndex = EVENT_DAMAGE;
	tempBuffer->copy(0, &functionIndex, 4);
	tempBuffer->copy(4, &playerID, 4);
	tempBuffer->copy(8, &targetID, 4);
	tempBuffer->copy(12, &damage, 4);
	tempBuffer->copy(16, &spellID, 4);
	tempBuffer->copy(20, "\n", 1);
	return tempBuffer;
}
Пример #8
0
DataBuffer * NetworkFunctions::createHealBuffer(int playerID, int targetID, int healing, int spellID)
{
	if (playerID < 0 || playerID >= players->size() || players->at(playerID) == NULL) return NULL;
	if (targetID < 0 || targetID >= players->size() || players->at(targetID) == NULL) return NULL;
	if (spellID < 0 || spellID >= SPELLID_COUNT) return NULL;

	DataBuffer * tempBuffer = new DataBuffer(21);
	int functionIndex = EVENT_HEAL;
	tempBuffer->copy(0, &functionIndex, 4);
	tempBuffer->copy(4, &playerID, 4);
	tempBuffer->copy(8, &targetID, 4);
	tempBuffer->copy(12, &healing, 4);
	tempBuffer->copy(16, &spellID, 4);
	tempBuffer->copy(20, "\n", 1);
	return tempBuffer;
}
Пример #9
0
DataBuffer * NetworkFunctions::createAddSpellBuffer(int playerID, int elementIndex, int spellID)
{
	if (playerID < 0 || playerID >= players->size() || players->at(playerID) == NULL) return NULL;
	if (elementIndex < 0 || elementIndex > 4) return NULL;
	if (spellID < 0 || spellID > SPELLID_COUNT) return NULL;

	int functionID = EVENT_ADD_SPELL;

	DataBuffer * tempBuffer = new DataBuffer(17);
	tempBuffer->copy(0, &functionID, 4);
	tempBuffer->copy(4, &playerID, 4);
	tempBuffer->copy(8, &elementIndex, 4);
	tempBuffer->copy(12, &spellID, 4);
	tempBuffer->copy(16, "\n", 1);

	return tempBuffer;
}
Пример #10
0
DataBuffer * Spell::createCentralForceBuffer(GameObject * g, glm::vec3 force, int spellID)
{
	if (spellID < 0 || spellID >= SPELLID_COUNT) return NULL;
	if (g == NULL) return NULL;

	DataBuffer * tempBuffer = g->createSpellCentralForceBuffer(force, spellID);
	tempBuffer->copy(8, &ownerID, 4);
	return tempBuffer;
}
Пример #11
0
DataBuffer * RigidObject::serialize()
{
	if (!physInit) return NULL;

	btVector3 pos = body->getCenterOfMassPosition();
	btVector3 linVel = body->getLinearVelocity();
	btQuaternion orient = body->getOrientation();
	btVector3 angVel = body->getAngularVelocity();

	DataBuffer * tempBuffer = new DataBuffer(getSerializedSize());

	float test = 1.0f;
	memcpy(&test, &pos.x(), 4);

	int uninit = -1;
	tempBuffer->copy(0, &uninit, 4); //function ID
	
	unsigned long netID = getNetID();
	tempBuffer->copy(4, &netID, 4);
	tempBuffer->copy(8,		&pos.x(),		4);
	tempBuffer->copy(12,	&pos.y(),		4);
	tempBuffer->copy(16,	&pos.z(),		4);
	tempBuffer->copy(20,	&linVel.x(),	4);
	tempBuffer->copy(24,	&linVel.y(),	4);
	tempBuffer->copy(28,	&linVel.z(),	4);
	tempBuffer->copy(32,	&orient.x(),	4);
	tempBuffer->copy(36,	&orient.y(),	4);
	tempBuffer->copy(40,	&orient.z(),	4);
	tempBuffer->copy(44,	&orient.w(),	4);
	tempBuffer->copy(48,	&angVel.x(),	4);
	tempBuffer->copy(52,	&angVel.y(),	4);
	tempBuffer->copy(56,	&angVel.z(),	4);

	tempBuffer->copy(60, "\n", 1);

	return tempBuffer;
}
Пример #12
0
DataBuffer * NetworkFunctions::createAttackBuffer(int playerID, int spellID, glm::vec3 pos, glm::vec3 lookAt)
{
	if (playerID < 0 || playerID >= players->size() || players->at(playerID) == NULL) return NULL;

	DataBuffer * tempBuffer = new DataBuffer(37);
	int functionIndex = EVENT_ATTACK;
	tempBuffer->copy(0,  &functionIndex, 4);
	tempBuffer->copy(4,	 &playerID, 4);
	tempBuffer->copy(8,	 &spellID, 4);
	tempBuffer->copy(12, &pos.x, 4);
	tempBuffer->copy(16, &pos.y, 4);
	tempBuffer->copy(20, &pos.z, 4);
	tempBuffer->copy(24, &lookAt.x, 4);
	tempBuffer->copy(28, &lookAt.y, 4);
	tempBuffer->copy(32, &lookAt.z, 4);
	tempBuffer->copy(36, "\n", 1);
	return tempBuffer;
}
Пример #13
0
DataBuffer * Spell::createCreateFadingBeamEffectBuffer(glm::vec3 start, glm::vec3 end, glm::vec3 colour, long duration)
{
	DataBuffer * tempBuffer = new DataBuffer(45);
	int functionIndex = EVENT_CREATE_FADING_BEAM_EFFECT;
	tempBuffer->copy(0, &functionIndex, 4);
	tempBuffer->copy(4, &start.x, 4);
	tempBuffer->copy(8, &start.y, 4);
	tempBuffer->copy(12, &start.z, 4);
	tempBuffer->copy(16, &end.x, 4);
	tempBuffer->copy(20, &end.y, 4);
	tempBuffer->copy(24, &end.z, 4);
	tempBuffer->copy(28, &colour.x, 4);
	tempBuffer->copy(32, &colour.y, 4);
	tempBuffer->copy(36, &colour.z, 4);
	tempBuffer->copy(40, &duration, 4);
	tempBuffer->copy(44, "\n", 1);

	return tempBuffer;
}
Пример #14
0
DataBuffer * NetworkFunctions::createForceBuffer(int playerID, unsigned long netID, glm::vec3 worldPos, glm::vec3 force, int spellID)
{
	if (playerID < 0 || playerID >= players->size() || players->at(playerID) == NULL) return NULL;
	if (spellID < 0 || spellID >= SPELLID_COUNT) return NULL;

	DataBuffer * tempBuffer = new DataBuffer(41);
	int functionIndex = EVENT_FORCE;
	tempBuffer->copy(0, &functionIndex, 4);
	tempBuffer->copy(4, &netID, 4);
	tempBuffer->copy(8, &playerID, 4);
	tempBuffer->copy(12, &worldPos.x, 4);
	tempBuffer->copy(16, &worldPos.y, 4);
	tempBuffer->copy(20, &worldPos.z, 4);
	tempBuffer->copy(24, &force.x, 4);
	tempBuffer->copy(28, &force.y, 4);
	tempBuffer->copy(32, &force.z, 4);
	tempBuffer->copy(36, &spellID, 4);
	tempBuffer->copy(40, "\n", 1);
	return tempBuffer;
}
Пример #15
0
DataBuffer * Fireball::createCreateFireballBuffer()
{
	if (owner == NULL) return NULL;

	DataBuffer * tempBuffer = new DataBuffer(33);
	int functionIndex = EVENT_CREATE_FIREBALL;
	tempBuffer->copy(0, &functionIndex, 4);
	tempBuffer->copy(4, &ownerID, 4);
	tempBuffer->copy(8, &position.x, 4);
	tempBuffer->copy(12, &position.y, 4);
	tempBuffer->copy(16, &position.z, 4);
	tempBuffer->copy(20, &direction.x, 4);
	tempBuffer->copy(24, &direction.y, 4);
	tempBuffer->copy(28, &direction.z, 4);
	tempBuffer->copy(32, "\n", 1);

	return tempBuffer;
}
Пример #16
0
DataBuffer * NetworkFunctions::createCreateGrenadeBuffer(int playerID, glm::vec3 pos, glm::vec3 dir)
{
	if (playerID < 0 || playerID >= (int)players->size() || players->at(playerID) == NULL) return NULL;

	DataBuffer * tempBuffer = new DataBuffer(33);
	int functionIndex = EVENT_CREATE_GRENADE;
	tempBuffer->copy(0, &functionIndex, 4);
	tempBuffer->copy(4, &playerID, 4);
	tempBuffer->copy(8, &pos.x, 4);
	tempBuffer->copy(12, &pos.y, 4);
	tempBuffer->copy(16, &pos.z, 4);
	tempBuffer->copy(20, &dir.x, 4);
	tempBuffer->copy(24, &dir.y, 4);
	tempBuffer->copy(28, &dir.z, 4);
	tempBuffer->copy(32, "\n", 1);

	return tempBuffer;
}
Пример #17
0
DataBuffer * NetworkFunctions::createCreateMeshObjectBuffer(MeshObject * meshObject)
{
	if (meshObject == NULL) return NULL;

	DataBuffer * tempBuffer = new DataBuffer(meshObject->getCreationSize()); //should be 129

	int functionID = EVENT_ADD_MESH_OBJECT;
	tempBuffer->copy(0, &functionID, 4);
	unsigned long netID = meshObject->getNetID();
	tempBuffer->copy(4, &netID, 4);

	string modelName = meshObject->getModelName();
	string textureName = meshObject->getTextureName();
	string shaderName = meshObject->getShaderName();
	string propName = meshObject->getCollisionName();

	char nullBuffer[32]; memset(nullBuffer, '\0', 32); //for (int i = 0; i < 32; i++) nullBuffer[i] = '\0';

	tempBuffer->copy(8, nullBuffer, 32);
	tempBuffer->copy(8, modelName.c_str(), std::min(32, (int)modelName.length()));
	tempBuffer->copy(40, nullBuffer, 32);
	tempBuffer->copy(40, textureName.c_str(), std::min(32, (int)textureName.length()));
	tempBuffer->copy(72, nullBuffer, 32);
	tempBuffer->copy(72, shaderName.c_str(), std::min(32, (int)shaderName.length()));

	glm::vec3 pos = meshObject->getPosition();
	tempBuffer->copy(104, &pos.x, 4);
	tempBuffer->copy(108, &pos.y, 4);
	tempBuffer->copy(112, &pos.z, 4);

	glm::vec3 lookAt = meshObject->getDirection();
	tempBuffer->copy(116, &lookAt.x, 4);
	tempBuffer->copy(120, &lookAt.y, 4);
	tempBuffer->copy(124, &lookAt.z, 4);
	
	tempBuffer->copy(128, "\n", 1);

	return tempBuffer;
}
Пример #18
0
DataBuffer * NetworkFunctions::createCreateCharacterObjectBuffer(CharacterObject * characterObject)
{
	if (characterObject == NULL) return NULL;

	DataBuffer * tempBuffer = new DataBuffer(characterObject->getCreationSize()); //should be 165

	int functionID = EVENT_SPAWN_PLAYER;
	tempBuffer->copy(0, &functionID, 4);
	unsigned long netID = characterObject->getNetID();
	tempBuffer->copy(4, &netID, 4);
	int playerID = characterObject->getPlayerID();
	tempBuffer->copy(8, &playerID, 4);
	
	string modelName = characterObject->getModelName();
	string textureName = characterObject->getTextureName();
	string shaderName = characterObject->getShaderName();
	string propName = characterObject->getCollisionName();
	
	char nullBuffer[32]; memset(nullBuffer, '\0', 32); //for (int i = 0; i < 32; i++) nullBuffer[i] = '\0';

	tempBuffer->copy(12, nullBuffer, 32);
	tempBuffer->copy(12, modelName.c_str(), std::min(32, (int)modelName.length()));
	tempBuffer->copy(44, nullBuffer, 32);
	tempBuffer->copy(44, textureName.c_str(), std::min(32, (int)textureName.length()));
	tempBuffer->copy(76, nullBuffer, 32);
	tempBuffer->copy(76, shaderName.c_str(), std::min(32, (int)shaderName.length()));

	glm::vec3 pos = characterObject->getPosition();
	tempBuffer->copy(108, &pos.x, 4);
	tempBuffer->copy(112, &pos.y, 4);
	tempBuffer->copy(116, &pos.z, 4);

	glm::vec3 lookAt = characterObject->getDirection();
	tempBuffer->copy(120, &lookAt.x, 4);
	tempBuffer->copy(124, &lookAt.y, 4);
	tempBuffer->copy(128, &lookAt.z, 4);

	tempBuffer->copy(132, nullBuffer, 32);
	tempBuffer->copy(132, propName.c_str(), std::min(32, (int)propName.length()));

	tempBuffer->copy(164, "\n", 1);

	return tempBuffer;
}
Пример #19
0
DataBuffer * NetworkFunctions::createSpawnPlayerRequestBuffer(int playerID, glm::vec3 pos, glm::vec3 lookAt, int classID)
{
	DataBuffer * tempBuffer = new DataBuffer(165); //should be 165

	int functionID = EVENT_SPAWN_PLAYER;
	tempBuffer->copy(0, &functionID, 4);
	unsigned long netID = 0;
	tempBuffer->copy(4, &netID, 4); //0 for autoassign
	tempBuffer->copy(8, &playerID, 4);

	string modelName = "player"; 
	string textureName = "error";
	string shaderName = "default";
	string propName = "sphere2";

	if (classID == 0) {
		/* Set model, texture, shader, prop by class here */
	}
	
	char nullBuffer[32]; memset(nullBuffer, '\0', 32); //for (int i = 0; i < 32; i++) nullBuffer[i] = '\0';

	tempBuffer->copy(12, nullBuffer, 32);
	tempBuffer->copy(12, modelName.c_str(), std::min(32, (int)modelName.length()));
	tempBuffer->copy(44, nullBuffer, 32);
	tempBuffer->copy(44, textureName.c_str(), std::min(32, (int)textureName.length()));
	tempBuffer->copy(76, nullBuffer, 32);
	tempBuffer->copy(76, shaderName.c_str(), std::min(32, (int)shaderName.length()));

	tempBuffer->copy(108, &pos.x, 4);
	tempBuffer->copy(112, &pos.y, 4);
	tempBuffer->copy(116, &pos.z, 4);

	tempBuffer->copy(120, &lookAt.x, 4);
	tempBuffer->copy(124, &lookAt.y, 4);
	tempBuffer->copy(128, &lookAt.z, 4);

	tempBuffer->copy(132, nullBuffer, 32);
	tempBuffer->copy(132, propName.c_str(), std::min(32, (int)propName.length()));

	tempBuffer->copy(164, "\n", 1);

	return tempBuffer;
}
Пример #20
0
int NetworkFunctions::translateReceivedEvents(Packet * serverEvents)
{
	while (serverEvents->getReceiveBufferSize() > 0) {
		DataBuffer * tempBuffer = new DataBuffer(MAX_PACKET_SIZE);

		int charCount = 0;
		int functionIndex = -1;
		int bufferLength = -1;
		for (int i = 0; i < serverEvents->getReceiveBufferSize(); i++) {
			tempBuffer->copy(tempBuffer->getSize(), serverEvents->getReceiveBuffer() + i, 1);

			if (charCount == 3) {
				memcpy(&functionIndex, tempBuffer->getData(), 4);
				if (functionIndex < 0 || functionIndex >= FUNCTION_COUNT || functionIndex == 10) {
					while ((serverEvents->getReceiveBuffer()[i]) != '\n') i++;
					tempBuffer->clear();
					continue;
				}
				else {
					bufferLength = bufferSizes[functionIndex];
				}
			}

			if (charCount == 7 && functionIndex == EVENT_UPDATE_GAME_OBJECT) {
				unsigned long netID = 0;
				memcpy(&netID, tempBuffer->getData() + 4, 4);
				GameObject * tempObject = gameObjects->getValue(netID);
				if (tempObject == NULL) {
					while ((serverEvents->getReceiveBuffer()[i]) != '\n') i++;
					tempBuffer->clear();
					continue;
				}
				/*int objectIndex = -1;
				memcpy(&objectIndex, tempBuffer->getData() + 4, 4);
				if (objectIndex < 0 || objectIndex >= gameObjects->size()) {
					while ((serverEvents->getReceiveBuffer()[i]) != '\n') i++;
					tempBuffer->clear();
					continue;
				}*/
				else {
					bufferLength = tempObject->getSerializedSize();
				}
			}

			if (charCount == bufferLength-1) {
				if (tempBuffer->getSize() == 0) {
					tempBuffer->clear();
					continue;
				}

				executeIndexedFunction(tempBuffer);
				tempBuffer->clear();
				charCount = 0;
				continue;
			}

			/*if (serverEvents->getReceiveBuffer()[i] == '\n') {
				if (tempBuffer->getSize() == 0) {
					tempBuffer->clear();
					continue;
				}

				executeIndexedFunction(tempBuffer);

				tempBuffer->clear();
			}*/
			charCount++;
		}
		delete tempBuffer;
		serverEvents->refillReceiveBuffer();
	}
	return 0;
}