diff options
Diffstat (limited to 'src/libjin/Net')
-rw-r--r-- | src/libjin/Net/Net.cpp | 24 | ||||
-rw-r--r-- | src/libjin/Net/Net.h | 26 | ||||
-rw-r--r-- | src/libjin/Net/Socket.cpp | 189 | ||||
-rw-r--r-- | src/libjin/Net/Socket.h | 58 |
4 files changed, 296 insertions, 1 deletions
diff --git a/src/libjin/Net/Net.cpp b/src/libjin/Net/Net.cpp index e69de29..db39be7 100644 --- a/src/libjin/Net/Net.cpp +++ b/src/libjin/Net/Net.cpp @@ -0,0 +1,24 @@ +#include "Net.h" + +namespace jin +{ +namespace net +{ + + bool Net::initSystem(const SettingBase* setting) + { + #ifdef _WIN32 + #if JIN_NET_TEKCOS + tk_init(); + #endif + #endif + return true; + } + + void Net::quitSystem() + { + + } + +} +} diff --git a/src/libjin/Net/Net.h b/src/libjin/Net/Net.h index 6b86a45..54ffede 100644 --- a/src/libjin/Net/Net.h +++ b/src/libjin/Net/Net.h @@ -1,6 +1,30 @@ #ifndef __JIN_NET_H #define __JIN_NET_H +#include "../modules.h" +#if JIN_MODULES_NET +#include "../Common/Subsystem.hpp" +#include "Socket.h" +namespace jin +{ +namespace net +{ -#endif
\ No newline at end of file + class Net : public Subsystem<Net> + { + public: + + protected: + Net() {}; + ~Net() {}; + SINGLETON(Net); + bool initSystem(const SettingBase* setting) override; + void quitSystem() override; + }; + +} +} + +#endif // JIN_MODULES_NET +#endif // __JIN_NET_H
\ No newline at end of file diff --git a/src/libjin/Net/Socket.cpp b/src/libjin/Net/Socket.cpp new file mode 100644 index 0000000..b7c621e --- /dev/null +++ b/src/libjin/Net/Socket.cpp @@ -0,0 +1,189 @@ +#include "Socket.h" + +namespace jin +{ +namespace net +{ + + Socket::Socket(SocketInformation info) + : tcpHandle(nullptr) + , udpHandle(nullptr) + { + type = info.type; + if (type == SocketType::TCP) + { + tk_IPaddress ip; + ip.host = info.address; + ip.port = info.port; + tcpHandle = tk_tcp_open(ip); + } + else if (type == SocketType::UDP) + { + udpHandle = tk_udp_open(info.port); + } + } + + Socket::Socket(SocketType type, const char* address, unsigned short port) + { + this->type = type; + if (type == SocketType::TCP) + { + tk_IPaddress ip; + #if JIN_NET_TEKCOS + ip.host = tk_strtohl(address); + ip.port = port; + tcpHandle = tk_tcp_open(ip); + #endif + } + else if (type == SocketType::UDP) + { + udpHandle = tk_udp_open(port); + } + } + + Socket::Socket(SocketType type, unsigned int address, unsigned short port) + { + this->type = type; + if (type == SocketType::TCP) + { + tk_IPaddress ip; + ip.host = address; + ip.port = port; + tcpHandle = tk_tcp_open(ip); + } + else if (type == SocketType::UDP) + { + udpHandle = tk_udp_open(port); + } + } + + Socket::Socket(SocketType type, unsigned short port) + { + this->type = type; + if (type == SocketType::TCP) + { + tk_IPaddress ip; + ip.host = 0; + ip.port = port; + tcpHandle = tk_tcp_open(ip); + } + else if (type == SocketType::UDP) + { + udpHandle = tk_udp_open(port); + } + } + +#if JIN_NET_TEKCOS + + Socket::Socket(tk_TCPsocket* handle) + : tcpHandle(handle) + , udpHandle(nullptr) + { + } + + Socket::Socket(tk_UDPsocket* handle) + : tcpHandle(nullptr) + , udpHandle(handle) + { + } + +#endif // JIN_NET_TEKCOS + + Socket::~Socket() + { + } + + void Socket::configureBlocking(bool blocking) + { + if (type != SocketType::TCP) + return; + #if JIN_NET_TEKCOS + if (blocking) + tk_tcp_blocking(tcpHandle); + else + tk_tcp_nonblocking(tcpHandle); + #endif + } + + Socket* Socket::accept() + { + if (type != SocketType::TCP) + return nullptr; + Socket* client; + #if JIN_NET_TEKCOS + tk_TCPsocket* socket = tk_tcp_accept(tcpHandle); + client = new Socket(socket); + #endif + return client; + } + + int Socket::receive(char* buffer, int size) + { + if (type != SocketType::TCP) + return 0; + #if JIN_NET_TEKCOS + int len; + tk_tcp_recv(tcpHandle, buffer, size, &len); + return len; + #endif + } + + int Socket::send(char* buffer, int size) + { + if (type != SocketType::TCP) + return 0; + #if JIN_NET_TEKCOS + int len; + tk_tcp_send(tcpHandle, buffer, size, &len); + return len; + #endif + } + + void Socket::sendTo(char* buffer, int size, unsigned int address, unsigned int port) + { + if (type != SocketType::UDP) + return; + #if JIN_NET_TEKCOS + tk_UDPpack pack; + pack.data = buffer; + pack.len = size; + pack.ip.host = address; + pack.ip.port = port; + tk_udp_sendto(udpHandle, &pack); + #endif + } + + int Socket::receiveFrom(char* buffer, int size, unsigned int address, unsigned int port) + { + if (type != SocketType::UDP) + return 0; + int len; + #if JIN_NET_TEKCOS + tk_UDPpack pack; + pack.data = buffer; + pack.len = size; + pack.ip.host = address; + pack.ip.port = port; + tk_udp_recvfrom(udpHandle, &pack); + return pack.len; + #endif + } + + void Socket::close() + { + if (type == SocketType::TCP) + { + #if JIN_NET_TEKCOS + tk_tcp_close(tcpHandle); + #endif + } + else if (type == SocketType::UDP) + { + #if JIN_NET_TEKCOS + tk_udp_close(udpHandle); + #endif + } + } + +} +}
\ No newline at end of file diff --git a/src/libjin/Net/Socket.h b/src/libjin/Net/Socket.h new file mode 100644 index 0000000..0eb27e0 --- /dev/null +++ b/src/libjin/Net/Socket.h @@ -0,0 +1,58 @@ +#ifndef __JIN_NET_SOCKET_H +#define __JIN_NET_SOCKET_H +#include "../modules.h" +#if JIN_MODULES_NET + +#include "../3rdparty/tekcos/tekcos.h" + +namespace jin +{ +namespace net +{ + + enum SocketType + { + TCP, + UDP + }; + + struct SocketInformation + { + unsigned int address; + unsigned short port; + SocketType type; + }; + + class Socket + { + public: + Socket(SocketInformation socketInformation); + Socket(SocketType type, unsigned short port); + Socket(SocketType type, unsigned int address, unsigned short port); + Socket(SocketType type, const char* address, unsigned short port); + ~Socket(); + + void configureBlocking(bool bocking); + + Socket* accept(); + int receive(char* buffer, int size); + int send(char* buffer, int size); + void sendTo(char* buffer, int size, unsigned int address, unsigned int port); + int receiveFrom(char* buffer, int size, unsigned int address, unsigned int port); + void close(); + + private: + #if JIN_NET_TEKCOS + Socket(tk_TCPsocket* tcpHandle); + Socket(tk_UDPsocket* udpHandle); + tk_TCPsocket* tcpHandle; + tk_UDPsocket* udpHandle; + #endif + SocketType type; + }; + +} // net +} // jin + +#endif // JIN_MODULES_NET +#endif // __JIN_NET_SOCKET_H
\ No newline at end of file |