summaryrefslogtreecommitdiffstats
path: root/include/network/Socket.hpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-07-29 16:55:16 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-07-29 16:55:16 +0200
commitf942405184c2d6067fb5303b58a225edf7e452b1 (patch)
tree83e70c7e3019e5b195c9caf41194b2113fa76d7f /include/network/Socket.hpp
parent2017-07-26 (diff)
downloadAltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.gz
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.bz2
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.lz
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.xz
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.tar.zst
AltCraft-f942405184c2d6067fb5303b58a225edf7e452b1.zip
Diffstat (limited to 'include/network/Socket.hpp')
-rw-r--r--include/network/Socket.hpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/include/network/Socket.hpp b/include/network/Socket.hpp
new file mode 100644
index 0000000..48bcad9
--- /dev/null
+++ b/include/network/Socket.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include <string>
+
+#include <SFML/Network.hpp>
+
+/**
+ * Platform independent class for working with platform dependent hardware socket
+ * @brief Wrapper around raw sockets
+ * @warning Connection state is based on lifetime of Socket object instance, ie connected at ctor and disconnect at dtor
+ * @todo Replace SFML's socket with WinSock and POSIX's socket implementation
+ */
+class Socket {
+ sf::TcpSocket socket;
+public:
+ /**
+ * Constructs Socket class instance from IP's string and Port number and connects to remote server
+ * @param[in] address IP address of remote server. String should be ANSI and contains 4 one-byte values separated by dots
+ * @param[in] port target port of remote server to connect
+ * @throw std::runtime_error if connection is failed
+ */
+ Socket(std::string address, unsigned short port);
+
+ /**
+ * Destruct Socket instance and disconnect from server
+ * @warning There is no way to force disconnect, except use delete for manually allocated objects and scope of visibility for variables on stack
+ */
+ ~Socket();
+
+ /**
+ * Reads data from socket and write to buffer
+ * @warning This is blocking function, and execution flow will not be returned until all required data is sended
+ * @warning Reported buffer length must be <= actual size of buffer, or memory corruption will be caused
+ * @param[out] buffPtr Pointer to buffer, where data must be placed
+ * @param[in] buffLen Length of data, that must be readed from server and writed to buffer
+ */
+ void Read(unsigned char *buffPtr, size_t buffLen);
+
+ /**
+ * Writes data from buffer to socket
+ * @warning This is blocking function, and execution flow will not be returned until all required data is received
+ * @param[in] buffPtr Pointer to buffer that contain data to send
+ * @param[in] buffLen Length of buffer
+ */
+ void Write(unsigned char *buffPtr, size_t buffLen);
+}; \ No newline at end of file