Browse Source

adding overloads from sendMessage()
everything will need to run through onMessageReceived now, which will eventually be a pure virtual function to be overridden by an application implementing our socket_listener

logicp 5 years ago
parent
commit
4f0fb8f72f
3 changed files with 34 additions and 6 deletions
  1. 7 0
      headers/socket_listener.hpp
  2. 3 3
      headers/types.hpp
  3. 24 3
      src/socket_listener.cpp

+ 7 - 0
headers/socket_listener.hpp

@@ -41,6 +41,10 @@ class SocketListener : public SendInterface {
   virtual void sendMessage(int client_socket_fd,
                            std::weak_ptr<char[]> w_buffer_ptr) override;
 
+  void sendMessage(int client_socket_fd, char* message, bool short_message);
+
+  void sendMessage(int client_socket_fd, char* message, size_t size);
+
   MessageHandler createMessageHandler(std::function<void()> cb);
   /**
    * Perform intialization work
@@ -63,6 +67,9 @@ class SocketListener : public SendInterface {
   // private methods
   int createSocket();
 
+  void onMessageReceived(int client_socket_fd,
+                         std::weak_ptr<char[]> w_buffer_ptr);
+
   int waitForConnection(int listening);
 
   void handleClientSocket(int client_socket_fd,

+ 3 - 3
headers/types.hpp

@@ -4,13 +4,13 @@
 #include <string>
 
 #define MAX_BUFFER_SIZE (49152)
+#define SMALL_BUFFER_SIZE (8192)
 
 template <typename MessageProcessor>
-void MessageHandler(MessageProcessor processor, int client_socket_fd, std::string message) {
+void MessageHandler(MessageProcessor processor, int client_socket_fd,
+                    std::string message) {
   processor(client_socket_fd, message);
 }
 
-
-
 #endif  //__TYPES_H__
 

+ 24 - 3
src/socket_listener.cpp

@@ -57,6 +57,11 @@ SocketListener::MessageHandler SocketListener::createMessageHandler(
   return MessageHandler(cb);
 }
 
+void SocketListener::onMessageReceived(int client_socket_fd,
+                                       std::weak_ptr<char[]> w_buffer_ptr) {
+  sendMessage(client_socket_fd, w_buffer_ptr);
+}
+
 /**
  * sendMessage
  * @method
@@ -75,6 +80,22 @@ void SocketListener::sendMessage(int client_socket_fd,
   }
 }
 
+void SocketListener::sendMessage(int client_socket_fd, char* message,
+                                 bool short_message) {
+  if (short_message) {
+    send(client_socket_fd, message, static_cast<size_t>(SMALL_BUFFER_SIZE) + 1,
+         0);
+  } else {
+    send(client_socket_fd, message, static_cast<size_t>(MAX_BUFFER_SIZE) + 1,
+         0);
+  }
+}
+
+void SocketListener::sendMessage(int client_socket_fd, char buffer[],
+                                 size_t size) {
+  send(client_socket_fd, buffer, size + 1, 0);
+}
+
 /**
  * init
  * TODO: Initialize buffer memory, if buffer is to be a class member
@@ -149,7 +170,7 @@ void SocketListener::run() {
         std::weak_ptr<char[]> w_buffer_ptr(s_buffer_ptr);
         std::function<void()> message_send_fn = [this, client_socket_fd,
                                                  w_buffer_ptr]() {
-          this->sendMessage(client_socket_fd, w_buffer_ptr);
+          this->onMessageReceived(client_socket_fd, w_buffer_ptr);
         };
         MessageHandler message_handler = createMessageHandler(message_send_fn);
         std::cout << "Pushing client to queue" << std::endl;
@@ -213,8 +234,8 @@ int SocketListener::createSocket() {
 /**
  * waitForConnection
  * @method
- * Takes first connection on queue of pending connections, creates a new socket
- * and returns its file descriptor
+ * Takes first connection on queue of pending connections, creates a new
+ * socket and returns its file descriptor
  */
 int SocketListener::waitForConnection(int listening_socket) {
   int client_socket_fd = accept(listening_socket, NULL, NULL);