Browse Source

switching to overridable implementation
fixing variable names
no longer providing a function pointer for receiving messages

Emmanuel Buckshi 5 years ago
parent
commit
2b54286581
9 changed files with 97 additions and 179 deletions
  1. 3 0
      .gitignore
  2. 1 1
      CMakeLists.txt
  3. 0 105
      TcpListener.cpp
  4. 0 42
      headers/TcpListener.h
  5. 8 3
      headers/constants.h
  6. 10 0
      headers/listen_interface.h
  7. 49 0
      headers/socket_listener.h
  8. 2 9
      main.cpp
  9. 24 19
      socket_listener.cpp

+ 3 - 0
.gitignore

@@ -1,3 +1,6 @@
 Makefile
 CMakeCache.txt
+CMakeFiles
+*.cmake
+tags
 ws_server

+ 1 - 1
CMakeLists.txt

@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 2.8)
 project(ws_server)
 
-set(SOURCES "main.cpp" "tcplistener.cpp")
+set(SOURCES "main.cpp" "socket_listener.cpp")
 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -std=c++17")
 add_executable(${PROJECT_NAME} ${SOURCES})
 target_include_directories(${PROJECT_NAME} PRIVATE

+ 0 - 105
TcpListener.cpp

@@ -1,105 +0,0 @@
-#include <iostream>
-#include <sys/types.h>
-#include <unistd.h>
-#include <netdb.h>
-#include <arpa/inet.h>
-#include <string.h>
-#include <string>
-
-#include "headers/TcpListener.h"
-#include "headers/constants.h"
-#include <sys/socket.h>
-int listening () {
-    return socket(AF_INET, SOCK_STREAM, 0);
-}
-
-CTcpListener::CTcpListener(std::string ipAddress, int port, MessageReceivedHandler handler)
-  : m_ipAddress(ipAddress), m_port(port), MessageReceived(handler) {
-
-}
-
-
-    // destructor
-CTcpListener::~CTcpListener() {
-  cleanup();
-}
-
-// Send message to client
-void CTcpListener::sendMessage(int clientSocket, std::string msg) {
-  send(clientSocket, msg.c_str(), msg.size() +1, 0);
-}
-
-// Initialize
-bool CTcpListener::init() {
-  std::cout << "Initializing socket listener" << std::endl;
-  return true;
-}
-
-// Main process loop
-void CTcpListener::run() {
-
-  char* buf[MAX_BUFFER_SIZE];
-
-  while (true) {
-    int listening = createSocket();
-
-    if (listening == SOCKET_ERROR) {
-      std::cout << "Socket error: shutting down server" << std::endl;
-      break;
-    }
-    int socket = waitForConnection(listening);
-
-    if (socket != SOCKET_ERROR) {
-      close(listening);
-
-      while (true) {
-        memset(buf, 0, MAX_BUFFER_SIZE);
-        int bytesReceived = 0;
-        bytesReceived = recv(socket, buf, MAX_BUFFER_SIZE, 0);
-        if (bytesReceived > 0) {
-            const char* constString = (const char*) buf;
-            std::cout << "Received: " << constString << std::endl;
-            MessageReceived(this, socket, std::string(constString));
-        } else {
-            std::cout << "client disconnected" << std::endl;
-            break;
-        }
-      }
-      close(socket);
-    }
-  }
-}
-
-// Cleanup
-void CTcpListener::cleanup() {
-  std::cout << "Cleaning up" << std::endl;
-}
-
-int CTcpListener::createSocket() {
-  int listening = socket(AF_INET, SOCK_STREAM, 0);
-
-  if (listening != SOCKET_ERROR) {
-
-    sockaddr_in hint;
-    hint.sin_family = AF_INET;
-    hint.sin_port = htons(m_port);
-    inet_pton(AF_INET, m_ipAddress.c_str(), &hint.sin_addr);
-
-    int bindOk = bind(listening, (sockaddr*)&hint, sizeof(hint));
-    if (bindOk != SOCKET_ERROR) {
-      int listenOk = listen(listening, SOMAXCONN);
-      if (listenOk == SOCKET_ERROR) {
-        return -1;
-      }
-    } else {
-      return -1;
-    }
-  }
-
-  return listening;
-}
-
-int CTcpListener::waitForConnection(int listening) {
-  int client = accept(listening, NULL, NULL);
-  return client;
-}

+ 0 - 42
headers/TcpListener.h

@@ -1,42 +0,0 @@
-#include <sys/socket.h>
-#include <string>
-
-#define MAX_BUFFER_SIZE (49152)
-class TcpListener;
-
-typedef void (*MessageReceivedHandler)(TcpListener* listener, int socketId,
-                                       std::string msg);
-
-class TcpListener {
- public:
-  // constructor
-  TcpListener(std::string ipAddress, int port, MessageReceivedHandler handler);
-
-  // destructor
-  ~TcpListener();
-
-  // public methods
-
-  // Send message to client
-  void sendMessage(int clientSocket, std::string msg);
-
-  // Initialize
-  bool init();
-
-  // Main process loop
-  void run();
-
-  // Cleanup
-  void cleanup();
-
- private:
-  // private methods
-  int createSocket();
-
-  int waitForConnection(int listening);
-
-  // private members
-  std::string m_ipAddress;
-  int m_port;
-  MessageReceivedHandler MessageReceived;
-};

+ 8 - 3
headers/constants.h

@@ -1,7 +1,12 @@
-#if !defined(TRX_SOCKET_CONSTANTS)
+#ifndef __CONSTANTS_H__
+#define __CONSTANTS_H__
+#ifndef TRX_SOCKET_CONSTANTS
 #define TRX_SOCKET_CONSTANTS 1
 
-int const SOCKET_ERROR = -1;
-int const SOCKET_OK = 0;
+const int SOCKET_ERROR = -1;
+const int SOCKET_OK = 0;
+const int WAIT_SOCKET_FAILURE = -1;
+const int WAIT_SOCKET_SUCCESS = 0;
 
 #endif
+#endif  // __CONSTANTS_H__

+ 10 - 0
headers/listen_interface.h

@@ -0,0 +1,10 @@
+#ifndef __LISTEN_INTERFACE_H__
+#define __LISTEN_INTERFACE_H__
+
+#include <string>
+
+class ListenInterface {
+  virtual void onMessageReceived(int socket_id, std::string message) = 0;
+};
+
+#endif  // __LISTEN_INTERFACE_H__

+ 49 - 0
headers/socket_listener.h

@@ -0,0 +1,49 @@
+#ifndef __SOCKET_LISTENER_H__
+#define __SOCKET_LISTENER_H__
+
+#include <sys/socket.h>
+#include <string>
+#include "listen_interface.h"
+
+#define MAX_BUFFER_SIZE (49152)
+
+// typedef void (*MessageReceivedHandler)(SocketListener* listener, int
+// socketId,
+//                                       std::string msg);
+
+class SocketListener : ListenInterface {
+ public:
+  // constructor
+  SocketListener(std::string ipAddress, int port);
+
+  // destructor
+  ~SocketListener();
+
+  // public methods
+
+  // Send message to client
+  void sendMessage(int socket, std::string msg);
+
+  // Initialize
+  bool init();
+
+  // Main process loop
+  void run();
+
+  // Cleanup
+  void cleanup();
+
+  void onMessageReceived(int socket_id, std::string message);
+
+ private:
+  // private methods
+  int createSocket();
+
+  int waitForConnection(int listening);
+
+  // private members
+  std::string m_ip_address;
+  int m_port;
+};
+
+#endif  // __SOCKET_LISTENER_H__

+ 2 - 9
main.cpp

@@ -1,12 +1,9 @@
 #include <iostream>
 #include <string>
-#include "headers/tcplistener.h"
-
-void listener_message_received(TcpListener* listener, int client,
-                               std::string msg);
+#include "headers/socket_listener.h"
 
 int main() {
-  TcpListener server("0.0.0.0", 9009, listener_message_received);
+  SocketListener server("0.0.0.0", 9009);
 
   if (server.init()) {
     server.run();
@@ -14,7 +11,3 @@ int main() {
   return 0;
 }
 
-void listener_message_received(TcpListener* listener, int client,
-                               std::string msg) {
-  listener->sendMessage(client, msg);
-}

+ 24 - 19
tcplistener.cpp → socket_listener.cpp

@@ -8,29 +8,28 @@
 
 #include <sys/socket.h>
 #include "headers/constants.h"
-#include "headers/tcplistener.h"
+#include "headers/socket_listener.h"
 int listening() { return socket(AF_INET, SOCK_STREAM, 0); }
 
-TcpListener::TcpListener(std::string ipAddress, int port,
-                         MessageReceivedHandler handler)
-    : m_ipAddress(ipAddress), m_port(port), MessageReceived(handler) {}
+SocketListener::SocketListener(std::string ip_address, int port)
+    : m_ip_address(ip_address), m_port(port) {}
 
 // destructor
-TcpListener::~TcpListener() { cleanup(); }
+SocketListener::~SocketListener() { cleanup(); }
 
 // Send message to client
-void TcpListener::sendMessage(int clientSocket, std::string msg) {
+void SocketListener::sendMessage(int clientSocket, std::string msg) {
   send(clientSocket, msg.c_str(), msg.size() + 1, 0);
 }
 
 // Initialize
-bool TcpListener::init() {
+bool SocketListener::init() {
   std::cout << "Initializing socket listener" << std::endl;
   return true;
 }
 
 // Main process loop
-void TcpListener::run() {
+void SocketListener::run() {
   char* buf[MAX_BUFFER_SIZE];
 
   while (true) {
@@ -50,9 +49,10 @@ void TcpListener::run() {
         int bytesReceived = 0;
         bytesReceived = recv(socket, buf, MAX_BUFFER_SIZE, 0);
         if (bytesReceived > 0) {
+          // TODO: implement a proper C++ cast
           const char* constString = (const char*)buf;
           std::cout << "Received: " << constString << std::endl;
-          MessageReceived(this, socket, std::string(constString));
+          onMessageReceived(socket, std::string(constString));
         } else {
           std::cout << "client disconnected" << std::endl;
           break;
@@ -64,32 +64,37 @@ void TcpListener::run() {
 }
 
 // Cleanup
-void TcpListener::cleanup() { std::cout << "Cleaning up" << std::endl; }
+void SocketListener::cleanup() { std::cout << "Cleaning up" << std::endl; }
 
-int TcpListener::createSocket() {
+int SocketListener::createSocket() {
   int listening = socket(AF_INET, SOCK_STREAM, 0);
 
   if (listening != SOCKET_ERROR) {
+    // TODO: whatsup with the variable name "hint" ?
     sockaddr_in hint;
     hint.sin_family = AF_INET;
     hint.sin_port = htons(m_port);
-    inet_pton(AF_INET, m_ipAddress.c_str(), &hint.sin_addr);
+    inet_pton(AF_INET, m_ip_address.c_str(), &hint.sin_addr);
 
-    int bindOk = bind(listening, (sockaddr*)&hint, sizeof(hint));
-    if (bindOk != SOCKET_ERROR) {
-      int listenOk = listen(listening, SOMAXCONN);
-      if (listenOk == SOCKET_ERROR) {
-        return -1;
+    int bind_result = bind(listening, (sockaddr*)&hint, sizeof(hint));
+    if (bind_result != SOCKET_ERROR) {
+      int listen_result = listen(listening, SOMAXCONN);
+      if (listen_result == SOCKET_ERROR) {
+        return WAIT_SOCKET_FAILURE;
       }
     } else {
-      return -1;
+      return WAIT_SOCKET_FAILURE;
     }
   }
 
   return listening;
 }
 
-int TcpListener::waitForConnection(int listening) {
+int SocketListener::waitForConnection(int listening) {
   int client = accept(listening, NULL, NULL);
   return client;
 }
+
+void SocketListener::onMessageReceived(int socket_id, std::string message) {
+  sendMessage(socket_id, message);
+}