mirror of
https://github.com/overte-org/overte.git
synced 2025-04-21 18:44:00 +02:00
Handle new and existing agent Ids
This commit is contained in:
parent
d8b80006b8
commit
a76f8a46cd
6 changed files with 33 additions and 7 deletions
|
@ -50,7 +50,9 @@ AgentList agentList(DOMAIN_LISTEN_PORT);
|
|||
unsigned char * addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) {
|
||||
*currentPosition++ = agentToAdd->getType();
|
||||
|
||||
// FIX THIS - NOT ONE BYTE
|
||||
currentPosition += agentToAdd->getAgentId();
|
||||
// ---
|
||||
currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket());
|
||||
currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket());
|
||||
|
||||
|
|
|
@ -162,6 +162,10 @@ bool Agent::matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, cha
|
|||
&& socketMatch(localSocket, otherLocalSocket);
|
||||
}
|
||||
|
||||
bool Agent::exists(uint16_t *otherAgentId) {
|
||||
return agentId == *otherAgentId;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const Agent* agent) {
|
||||
sockaddr_in *agentPublicSocket = (sockaddr_in *)agent->publicSocket;
|
||||
sockaddr_in *agentLocalSocket = (sockaddr_in *)agent->localSocket;
|
||||
|
|
|
@ -28,6 +28,7 @@ class Agent {
|
|||
bool operator==(const Agent& otherAgent);
|
||||
|
||||
bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType);
|
||||
bool exists(uint16_t *agentId);
|
||||
char getType();
|
||||
void setType(char newType);
|
||||
uint16_t getAgentId();
|
||||
|
|
|
@ -109,10 +109,16 @@ int AgentList::indexOfMatchingAgent(sockaddr *senderAddress) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
int AgentList::unpackAgentId(unsigned char *packedData, uint16_t *agentId) {
|
||||
memcpy(packedData, agentId, sizeof(uint16_t));
|
||||
return sizeof(uint16_t);
|
||||
}
|
||||
|
||||
int AgentList::updateList(unsigned char *packetData, size_t dataBytes) {
|
||||
int readAgents = 0;
|
||||
|
||||
char agentType;
|
||||
uint16_t agentId;
|
||||
|
||||
// assumes only IPv4 addresses
|
||||
sockaddr_in agentPublicSocket;
|
||||
|
@ -125,17 +131,21 @@ int AgentList::updateList(unsigned char *packetData, size_t dataBytes) {
|
|||
|
||||
while((readPtr - startPtr) < dataBytes) {
|
||||
agentType = *readPtr++;
|
||||
readPtr += unpackAgentId(readPtr, (uint16_t *)&agentId);
|
||||
readPtr += unpackSocket(readPtr, (sockaddr *)&agentPublicSocket);
|
||||
readPtr += unpackSocket(readPtr, (sockaddr *)&agentLocalSocket);
|
||||
|
||||
addOrUpdateAgent((sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType);
|
||||
//syncClientAgentList(agentId, (sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType);
|
||||
|
||||
addOrUpdateAgent((sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType, agentId);
|
||||
}
|
||||
|
||||
return readAgents;
|
||||
}
|
||||
|
||||
bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType) {
|
||||
bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId = 0) {
|
||||
std::vector<Agent>::iterator agent;
|
||||
uint16_t thisAgentId;
|
||||
|
||||
for(agent = agents.begin(); agent != agents.end(); agent++) {
|
||||
if (agent->matches(publicSocket, localSocket, agentType)) {
|
||||
|
@ -146,8 +156,15 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket,
|
|||
|
||||
if (agent == agents.end()) {
|
||||
// we didn't have this agent, so add them
|
||||
Agent newAgent = Agent(publicSocket, localSocket, agentType, this->lastAgentId);
|
||||
++this->lastAgentId;
|
||||
|
||||
if (agentId == 0) {
|
||||
thisAgentId = this->lastAgentId;
|
||||
++this->lastAgentId;
|
||||
} else {
|
||||
thisAgentId = agentId;
|
||||
}
|
||||
|
||||
Agent newAgent = Agent(publicSocket, localSocket, agentType, thisAgentId);
|
||||
|
||||
if (socketMatch(publicSocket, localSocket)) {
|
||||
// likely debugging scenario with DS + agent on local network
|
||||
|
|
|
@ -35,9 +35,11 @@ class AgentList {
|
|||
|
||||
std::vector<Agent>& getAgents();
|
||||
UDPSocket& getAgentSocket();
|
||||
|
||||
|
||||
int updateList(unsigned char *packetData, size_t dataBytes);
|
||||
bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType);
|
||||
bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId);
|
||||
int unpackAgentId(unsigned char *packedData, uint16_t *agentId);
|
||||
int packAgentId(unsigned char *packStore, uint16_t agentId);
|
||||
void processAgentData(sockaddr *senderAddress, void *packetData, size_t dataBytes);
|
||||
void updateAgentWithData(sockaddr *senderAddress, void *packetData, size_t dataBytes);
|
||||
void broadcastToAgents(char *broadcastData, size_t dataBytes);
|
||||
|
|
|
@ -115,7 +115,7 @@ bool UDPSocket::receive(sockaddr *recvAddress, void *receivedData, ssize_t *rece
|
|||
socklen_t addressSize = sizeof(&recvAddress);
|
||||
|
||||
*receivedBytes = recvfrom(handle, static_cast<char*>(receivedData), MAX_BUFFER_LENGTH_BYTES,
|
||||
0, recvAddress, reinterpret_cast<int*>(&addressSize));
|
||||
0, recvAddress, &addressSize);
|
||||
|
||||
return (*receivedBytes > 0);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue