summaryrefslogtreecommitdiff
path: root/test/clsocket
diff options
context:
space:
mode:
authorErwin Coumans <erwin.coumans@gmail.com>2017-02-19 10:25:55 -0800
committerErwin Coumans <erwin.coumans@gmail.com>2017-02-19 10:25:55 -0800
commit957266b12185cf5b4b81816906ea85839bf463b2 (patch)
tree2f2bc0c04546986e3b5d9bbb4a8dc5bb5081832b /test/clsocket
parentcfd35840f0fa278e635f3f9279df944e9f58118a (diff)
downloadbullet3-957266b12185cf5b4b81816906ea85839bf463b2.tar.gz
add tiny cross-platform TCP socket library from https://github.com/DFHack/clsocket
(todo: hook-up to Bullet cmake/premake build systems, and implement TCP alternative for UDP server)
Diffstat (limited to 'test/clsocket')
-rw-r--r--test/clsocket/EchoServer.cpp44
-rw-r--r--test/clsocket/QueryDayTime.cpp49
-rw-r--r--test/clsocket/RecvAsync.cpp101
3 files changed, 194 insertions, 0 deletions
diff --git a/test/clsocket/EchoServer.cpp b/test/clsocket/EchoServer.cpp
new file mode 100644
index 000000000..4a27d278f
--- /dev/null
+++ b/test/clsocket/EchoServer.cpp
@@ -0,0 +1,44 @@
+
+#include "PassiveSocket.h" // Include header for active socket object definition
+
+#define MAX_PACKET 4096
+
+int main(int argc, char **argv)
+{
+ CPassiveSocket socket;
+ CActiveSocket *pClient = NULL;
+
+ //--------------------------------------------------------------------------
+ // Initialize our socket object
+ //--------------------------------------------------------------------------
+ socket.Initialize();
+
+ socket.Listen("127.0.0.1", 6789);
+
+ while (true)
+ {
+ if ((pClient = socket.Accept()) != NULL)
+ {
+ //----------------------------------------------------------------------
+ // Receive request from the client.
+ //----------------------------------------------------------------------
+ if (pClient->Receive(MAX_PACKET))
+ {
+ //------------------------------------------------------------------
+ // Send response to client and close connection to the client.
+ //------------------------------------------------------------------
+ pClient->Send( pClient->GetData(), pClient->GetBytesReceived() );
+ pClient->Close();
+ }
+
+ delete pClient;
+ }
+ }
+
+ //-----------------------------------------------------------------------------
+ // Receive request from the client.
+ //-----------------------------------------------------------------------------
+ socket.Close();
+
+ return 1;
+}
diff --git a/test/clsocket/QueryDayTime.cpp b/test/clsocket/QueryDayTime.cpp
new file mode 100644
index 000000000..a853abfab
--- /dev/null
+++ b/test/clsocket/QueryDayTime.cpp
@@ -0,0 +1,49 @@
+
+#include <string.h>
+#include "ActiveSocket.h" // Include header for active socket object definition
+
+int main(int argc, char **argv)
+{
+ CActiveSocket socket; // Instantiate active socket object (defaults to TCP).
+ char time[50];
+
+ memset(&time, 0, 50);
+
+ //--------------------------------------------------------------------------
+ // Initialize our socket object
+ //--------------------------------------------------------------------------
+ socket.Initialize();
+
+ //--------------------------------------------------------------------------
+ // Create a connection to the time server so that data can be sent
+ // and received.
+ //--------------------------------------------------------------------------
+// if (socket.Open("time-C.timefreq.bldrdoc.gov", 13))
+ if (socket.Open("127.0.0.1", 6789))
+ {
+ //----------------------------------------------------------------------
+ // Send a requtest the server requesting the current time.
+ //----------------------------------------------------------------------
+ const char* data = "Hello!";
+ int len = strlen(data);
+
+ if (socket.Send((const uint8 *)"HELLO\n", len))
+ {
+ //----------------------------------------------------------------------
+ // Receive response from the server.
+ //----------------------------------------------------------------------
+ socket.Receive(len);
+ uint8* data = socket.GetData();
+ memcpy(&time, data, len);
+ printf("%s\n", time);
+
+ //----------------------------------------------------------------------
+ // Close the connection.
+ //----------------------------------------------------------------------
+ socket.Close();
+ }
+ }
+
+
+ return 1;
+}
diff --git a/test/clsocket/RecvAsync.cpp b/test/clsocket/RecvAsync.cpp
new file mode 100644
index 000000000..d4e415fd0
--- /dev/null
+++ b/test/clsocket/RecvAsync.cpp
@@ -0,0 +1,101 @@
+#include <pthread.h>
+#include "PassiveSocket.h"
+
+#ifdef WIN32
+#include <windows.h>
+
+ // usually defined with #include <unistd.h>
+ static void sleep( unsigned int seconds )
+ {
+ Sleep( seconds * 1000 );
+ }
+#endif
+
+#define MAX_PACKET 4096
+#define TEST_PACKET "Test Packet"
+
+struct thread_data
+{
+ const char *pszServerAddr;
+ short int nPort;
+ int nNumBytesToReceive;
+ int nTotalPayloadSize;
+};
+
+
+void *CreateTCPEchoServer(void *param)
+{
+ CPassiveSocket socket;
+ CActiveSocket *pClient = NULL;
+ struct thread_data *pData = (struct thread_data *)param;
+ int nBytesReceived = 0;
+
+ socket.Initialize();
+ socket.Listen(pData->pszServerAddr, pData->nPort);
+
+ if ((pClient = socket.Accept()) != NULL)
+ {
+ while (nBytesReceived != pData->nTotalPayloadSize)
+ {
+ if (nBytesReceived += pClient->Receive(pData->nNumBytesToReceive))
+ {
+ pClient->Send((const uint8 *)pClient->GetData(), pClient->GetBytesReceived());
+ }
+ }
+
+ sleep(100);
+
+ delete pClient;
+ }
+
+ socket.Close();
+
+ return NULL;
+}
+
+int main(int argc, char **argv)
+{
+ pthread_t threadId;
+ struct thread_data thData;
+ CActiveSocket client;
+ char result[1024];
+
+ thData.pszServerAddr = "127.0.0.1";
+ thData.nPort = 6789;
+ thData.nNumBytesToReceive = 1;
+ thData.nTotalPayloadSize = (int)strlen(TEST_PACKET);
+
+ pthread_create(&threadId, 0, CreateTCPEchoServer, &thData);
+ sleep(1); // allow a second for the thread to create and listen
+
+ client.Initialize();
+ client.SetNonblocking();
+
+ if (client.Open("127.0.0.1", 6789))
+ {
+ if (client.Send((uint8 *)TEST_PACKET, strlen(TEST_PACKET)))
+ {
+ int numBytes = -1;
+ int bytesReceived = 0;
+
+ client.Select();
+
+ while (bytesReceived != strlen(TEST_PACKET))
+ {
+ numBytes = client.Receive(MAX_PACKET);
+
+ if (numBytes > 0)
+ {
+ bytesReceived += numBytes;
+ memset(result, 0, 1024);
+ memcpy(result, client.GetData(), numBytes);
+ printf("received %d bytes: '%s'\n", numBytes, result);
+ }
+ else
+ {
+ printf("Received %d bytes\n", numBytes);
+ }
+ }
+ }
+ }
+}