mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 20:56:52 +02:00
check the number of sscanf matches, store sending socket for device to reply
This commit is contained in:
parent
9050642390
commit
e5cd66313a
1 changed files with 29 additions and 21 deletions
|
@ -18,6 +18,7 @@ const int MAX_PACKET_SIZE_BYTES = 1400;
|
||||||
struct PairableDevice {
|
struct PairableDevice {
|
||||||
char identifier[64];
|
char identifier[64];
|
||||||
char name[64];
|
char name[64];
|
||||||
|
sockaddr_in sendingSocket;
|
||||||
sockaddr_in localSocket;
|
sockaddr_in localSocket;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -34,40 +35,47 @@ int indexOfFirstOccurenceOfCharacter(char* haystack, char needle) {
|
||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char* argv[]) {
|
||||||
UDPSocket serverSocket(PAIRING_SERVER_LISTEN_PORT);
|
UDPSocket serverSocket(PAIRING_SERVER_LISTEN_PORT);
|
||||||
|
|
||||||
sockaddr deviceAddress;
|
sockaddr_in deviceAddress;
|
||||||
char deviceData[MAX_PACKET_SIZE_BYTES] = {};
|
char deviceData[MAX_PACKET_SIZE_BYTES] = {};
|
||||||
ssize_t receivedBytes = 0;
|
ssize_t receivedBytes = 0;
|
||||||
|
|
||||||
std::vector<PairableDevice> devices;
|
std::vector<PairableDevice> devices;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (serverSocket.receive(&deviceAddress, &deviceData, &receivedBytes)) {
|
if (serverSocket.receive((sockaddr *)&deviceAddress, &deviceData, &receivedBytes)) {
|
||||||
// create a new PairableDevice
|
// create a new PairableDevice
|
||||||
PairableDevice newDevice = {};
|
PairableDevice newDevice = {};
|
||||||
|
|
||||||
int addressBytes[4];
|
int addressBytes[4];
|
||||||
int socketPort = 0;
|
int socketPort = 0;
|
||||||
|
|
||||||
sscanf(deviceData, "A %s %d.%d.%d.%d:%d %s",
|
int numMatches = sscanf(deviceData, "A %s %d.%d.%d.%d:%d %s",
|
||||||
newDevice.identifier,
|
newDevice.identifier,
|
||||||
&addressBytes[3],
|
&addressBytes[3],
|
||||||
&addressBytes[2],
|
&addressBytes[2],
|
||||||
&addressBytes[1],
|
&addressBytes[1],
|
||||||
&addressBytes[0],
|
&addressBytes[0],
|
||||||
&socketPort,
|
&socketPort,
|
||||||
newDevice.name);
|
newDevice.name);
|
||||||
|
|
||||||
// setup the localSocket for the pairing device
|
if (numMatches >= 6) {
|
||||||
newDevice.localSocket.sin_family = AF_INET;
|
// if we have fewer than 6 matches the packet wasn't properly formatted
|
||||||
newDevice.localSocket.sin_addr.s_addr = (addressBytes[3] |
|
|
||||||
addressBytes[2] << 8 |
|
// setup the localSocket for the pairing device
|
||||||
addressBytes[1] << 16 |
|
newDevice.localSocket.sin_family = AF_INET;
|
||||||
addressBytes[0] << 24);
|
newDevice.localSocket.sin_addr.s_addr = (addressBytes[3] |
|
||||||
newDevice.localSocket.sin_port = socketPort;
|
addressBytes[2] << 8 |
|
||||||
|
addressBytes[1] << 16 |
|
||||||
// push this new device into the vector
|
addressBytes[0] << 24);
|
||||||
printf("Adding device %s (%s) to list\n", newDevice.identifier, newDevice.name);
|
newDevice.localSocket.sin_port = socketPort;
|
||||||
devices.push_back(newDevice);
|
|
||||||
|
// store this device's sending socket so we can talk back to it
|
||||||
|
newDevice.sendingSocket = deviceAddress;
|
||||||
|
|
||||||
|
// push this new device into the vector
|
||||||
|
printf("Adding device %s (%s) to list\n", newDevice.identifier, newDevice.name);
|
||||||
|
devices.push_back(newDevice);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue