From 0f9d72e2c1ead7d0dbf61fad93f1eb404496d64f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 16:14:17 -0700 Subject: [PATCH 1/3] keep one last PairableDevice and one last RequestingClient --- pairing-server/src/main.cpp | 69 +++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/pairing-server/src/main.cpp b/pairing-server/src/main.cpp index 3d7a36f14c..3c7c24c141 100644 --- a/pairing-server/src/main.cpp +++ b/pairing-server/src/main.cpp @@ -24,6 +24,15 @@ struct PairableDevice { sockaddr_in localSocket; }; +struct RequestingClient { + char address[INET_ADDRSTRLEN]; + int port; +}; + +UDPSocket serverSocket(PAIRING_SERVER_LISTEN_PORT); +PairableDevice *lastDevice = NULL; +RequestingClient *lastClient = NULL; + int indexOfFirstOccurenceOfCharacter(char* haystack, char needle) { int currentIndex = 0; @@ -34,70 +43,80 @@ int indexOfFirstOccurenceOfCharacter(char* haystack, char needle) { return currentIndex; } +void sendLastClientToLastDevice() { + char pairData[INET_ADDRSTRLEN + 6] = {}; + int bytesWritten = sprintf(pairData, "%s:%d", ::lastClient->address, ::lastClient->port); + + ::serverSocket.send((sockaddr*) &::lastDevice->sendingSocket, pairData, bytesWritten); +} + int main(int argc, const char* argv[]) { - UDPSocket serverSocket(PAIRING_SERVER_LISTEN_PORT); sockaddr_in senderSocket; char senderData[MAX_PACKET_SIZE_BYTES] = {}; ssize_t receivedBytes = 0; - std::vector devices; - while (true) { - if (serverSocket.receive((sockaddr *)&senderSocket, &senderData, &receivedBytes)) { - + if (::serverSocket.receive((sockaddr *)&senderSocket, &senderData, &receivedBytes)) { if (senderData[0] == 'A') { // this is a device reporting itself as available - // create a new PairableDevice - PairableDevice newDevice = {}; + PairableDevice tempDevice = {}; char deviceAddress[INET_ADDRSTRLEN] = {}; int socketPort = 0; int numMatches = sscanf(senderData, "Available %s%[^:]:%d %s", - newDevice.identifier, + tempDevice.identifier, deviceAddress, &socketPort, - newDevice.name); + tempDevice.name); if (numMatches >= 3) { - // if we have fewer than 6 matches the packet wasn't properly formatted + // if we have fewer than 3 matches the packet wasn't properly formatted + // otherwise copy the tempDevice to the persisting lastDevice + *::lastDevice = tempDevice; // setup the localSocket for the pairing device - newDevice.localSocket.sin_family = AF_INET; - inet_pton(AF_INET, deviceAddress, &newDevice); - newDevice.localSocket.sin_port = socketPort; + ::lastDevice->localSocket.sin_family = AF_INET; + inet_pton(AF_INET, deviceAddress, &::lastDevice); + ::lastDevice->localSocket.sin_port = socketPort; // store this device's sending socket so we can talk back to it - newDevice.sendingSocket = senderSocket; + ::lastDevice->sendingSocket = senderSocket; // push this new device into the vector printf("Adding device %s (%s) - %s:%d to list\n", - newDevice.identifier, - newDevice.name, + ::lastDevice->identifier, + ::lastDevice->name, deviceAddress, socketPort); - devices.push_back(newDevice); + if (::lastClient) { + sendLastClientToLastDevice(); + } } } else if (senderData[0] == 'F') { // this is a client looking to pair with a device // send the most recent device this address so it can attempt to pair - char requestorAddress[INET_ADDRSTRLEN] = {}; - int requestorPort = 0; + RequestingClient tempClient = {}; - int requestorMatches = sscanf(senderData, "Find %[^:]:%d", requestorAddress, &requestorPort); - printf("Find request from interface client at %s:%d\n", requestorAddress, requestorPort); + int requestorMatches = sscanf(senderData, "Find %[^:]:%d", + tempClient.address, + &tempClient.port); if (requestorMatches == 2) { - PairableDevice lastDevice = devices[devices.size() - 1]; + // good data, copy the tempClient to the persisting lastInterfaceClient + *::lastClient = tempClient; - char pairData[INET_ADDRSTRLEN + 6] = {}; - sprintf(pairData, "%s:%d", requestorAddress, requestorPort); + printf("Find request from interface client at %s:%d\n", + ::lastClient->address, + ::lastClient->port); - serverSocket.send((sockaddr*) &lastDevice.sendingSocket, pairData, strlen(pairData)); + if (::lastDevice) { + sendLastClientToLastDevice(); + } } } } From cc4492bad8088156fdc55b84473985b03da99891 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 16:29:43 -0700 Subject: [PATCH 2/3] fix memory breakages after previous change --- pairing-server/src/main.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pairing-server/src/main.cpp b/pairing-server/src/main.cpp index 3c7c24c141..96a4606eae 100644 --- a/pairing-server/src/main.cpp +++ b/pairing-server/src/main.cpp @@ -66,7 +66,7 @@ int main(int argc, const char* argv[]) { char deviceAddress[INET_ADDRSTRLEN] = {}; int socketPort = 0; - int numMatches = sscanf(senderData, "Available %s%[^:]:%d %s", + int numMatches = sscanf(senderData, "Available %s %[^:]:%d %s", tempDevice.identifier, deviceAddress, &socketPort, @@ -74,23 +74,24 @@ int main(int argc, const char* argv[]) { if (numMatches >= 3) { // if we have fewer than 3 matches the packet wasn't properly formatted - // otherwise copy the tempDevice to the persisting lastDevice - *::lastDevice = tempDevice; // setup the localSocket for the pairing device - ::lastDevice->localSocket.sin_family = AF_INET; + tempDevice.localSocket.sin_family = AF_INET; inet_pton(AF_INET, deviceAddress, &::lastDevice); - ::lastDevice->localSocket.sin_port = socketPort; + tempDevice.localSocket.sin_port = socketPort; // store this device's sending socket so we can talk back to it - ::lastDevice->sendingSocket = senderSocket; + tempDevice.sendingSocket = senderSocket; // push this new device into the vector - printf("Adding device %s (%s) - %s:%d to list\n", - ::lastDevice->identifier, - ::lastDevice->name, + printf("New last device is %s (%s) at %s:%d\n", + tempDevice.identifier, + tempDevice.name, deviceAddress, socketPort); + + // copy the tempDevice to the persisting lastDevice + ::lastDevice = new PairableDevice(tempDevice); if (::lastClient) { sendLastClientToLastDevice(); @@ -107,10 +108,10 @@ int main(int argc, const char* argv[]) { &tempClient.port); if (requestorMatches == 2) { - // good data, copy the tempClient to the persisting lastInterfaceClient - *::lastClient = tempClient; + // good data, copy the tempClient to the persisting lastInterfaceClient + ::lastClient = new RequestingClient(tempClient); - printf("Find request from interface client at %s:%d\n", + printf("New last client is at %s:%d\n", ::lastClient->address, ::lastClient->port); From 0b9b8a14d14b2763bbd08aa3b05f219b367eb453 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 16:36:55 -0700 Subject: [PATCH 3/3] couple of type squishes --- pairing-server/src/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pairing-server/src/main.cpp b/pairing-server/src/main.cpp index 96a4606eae..0422251403 100644 --- a/pairing-server/src/main.cpp +++ b/pairing-server/src/main.cpp @@ -30,8 +30,8 @@ struct RequestingClient { }; UDPSocket serverSocket(PAIRING_SERVER_LISTEN_PORT); -PairableDevice *lastDevice = NULL; -RequestingClient *lastClient = NULL; +PairableDevice* lastDevice = NULL; +RequestingClient* lastClient = NULL; int indexOfFirstOccurenceOfCharacter(char* haystack, char needle) { int currentIndex = 0; @@ -57,7 +57,7 @@ int main(int argc, const char* argv[]) { ssize_t receivedBytes = 0; while (true) { - if (::serverSocket.receive((sockaddr *)&senderSocket, &senderData, &receivedBytes)) { + if (::serverSocket.receive((sockaddr*) &senderSocket, &senderData, &receivedBytes)) { if (senderData[0] == 'A') { // this is a device reporting itself as available