From 7d5f055547e70fa93ee9ac944e62f8d657b9dc55 Mon Sep 17 00:00:00 2001 From: chai Date: Fri, 19 Oct 2018 08:36:44 +0800 Subject: =?UTF-8?q?*=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/Net/Net.cpp | 24 ------ src/libjin/Net/Net.h | 31 ------- src/libjin/Net/Socket.cpp | 190 ------------------------------------------- src/libjin/Net/Socket.h | 62 -------------- src/libjin/Net/je_net.cpp | 24 ++++++ src/libjin/Net/je_net.h | 32 ++++++++ src/libjin/Net/je_socket.cpp | 190 +++++++++++++++++++++++++++++++++++++++++++ src/libjin/Net/je_socket.h | 62 ++++++++++++++ src/libjin/Net/net.cpp | 24 ------ src/libjin/Net/net.h | 31 ------- 10 files changed, 308 insertions(+), 362 deletions(-) delete mode 100644 src/libjin/Net/Net.cpp delete mode 100644 src/libjin/Net/Net.h delete mode 100644 src/libjin/Net/Socket.cpp delete mode 100644 src/libjin/Net/Socket.h create mode 100644 src/libjin/Net/je_net.cpp create mode 100644 src/libjin/Net/je_net.h create mode 100644 src/libjin/Net/je_socket.cpp create mode 100644 src/libjin/Net/je_socket.h delete mode 100644 src/libjin/Net/net.cpp delete mode 100644 src/libjin/Net/net.h (limited to 'src/libjin/Net') diff --git a/src/libjin/Net/Net.cpp b/src/libjin/Net/Net.cpp deleted file mode 100644 index 940b239..0000000 --- a/src/libjin/Net/Net.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "Net.h" - -namespace jin -{ - namespace net - { - - bool Net::initSystem(const SettingBase* setting) - { - #ifdef _WIN32 - #if LIBJIN_NET_TEKCOS - tk_init(); - #endif - #endif - return true; - } - - void Net::quitSystem() - { - - } - - } // namespace net -} // namespace jin \ No newline at end of file diff --git a/src/libjin/Net/Net.h b/src/libjin/Net/Net.h deleted file mode 100644 index 3324d11..0000000 --- a/src/libjin/Net/Net.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __LIBJIN_NET_H -#define __LIBJIN_NET_H -#include "../configuration.h" -#if LIBJIN_MODULES_NET - -#include "../Common/Subsystem.hpp" -#include "Socket.h" - -namespace jin -{ - namespace net - { - - class Net : public Subsystem - { - public: - - protected: - singleton(Net); - Net() {}; - ~Net() {}; - bool initSystem(const SettingBase* setting) override; - void quitSystem() override; - - }; - - } // namespace net -} // namespace jin - -#endif // LIBJIN_MODULES_NET -#endif // __LIBJIN_NET_H diff --git a/src/libjin/Net/Socket.cpp b/src/libjin/Net/Socket.cpp deleted file mode 100644 index 770b47d..0000000 --- a/src/libjin/Net/Socket.cpp +++ /dev/null @@ -1,190 +0,0 @@ -#include "Socket.h" - -namespace jin -{ - namespace net - { - Socket::Socket(const Socket& socket) - : handle(socket.handle) - , type(socket.type) - { - } - - Socket::Socket(const SocketInformation& info) - : type(info.type) - { - if (type == SocketType::TCP) - { - tk_IPaddress ip; - ip.host = info.address; - ip.port = info.port; - handle.tcpHandle = tk_tcp_open(ip); - } - else if (type == SocketType::UDP) - { - handle.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 LIBJIN_NET_TEKCOS - ip.host = tk_strtohl(address); - ip.port = port; - handle.tcpHandle = tk_tcp_open(ip); - #endif - } - else if (type == SocketType::UDP) - { - handle.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; - handle.tcpHandle = tk_tcp_open(ip); - } - else if (type == SocketType::UDP) - { - handle.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; - handle.tcpHandle = tk_tcp_open(ip); - } - else if (type == SocketType::UDP) - { - handle.udpHandle = tk_udp_open(port); - } - } - - #if LIBJIN_NET_TEKCOS - - Socket::Socket(const tk_TCPsocket& tcphandle) - { - handle.tcpHandle = tcphandle; - } - - Socket::Socket(const tk_UDPsocket& udphandle) - { - handle.udpHandle = udphandle; - } - - #endif // LIBJIN_NET_TEKCOS - - Socket::~Socket() - { - } - - void Socket::configureBlocking(bool blocking) - { - if (type != SocketType::TCP) - return; - #if LIBJIN_NET_TEKCOS - if (blocking) - tk_tcp_blocking(&handle.tcpHandle); - else - tk_tcp_nonblocking(&handle.tcpHandle); - #endif - } - - Socket* Socket::accept() - { - if (type != SocketType::TCP) - return nullptr; - Socket* client; - #if LIBJIN_NET_TEKCOS - tk_TCPsocket socket = tk_tcp_accept(&handle.tcpHandle); - client = new Socket(socket); - #endif - return client; - } - - int Socket::receive(char* buffer, int size) - { - if (type != SocketType::TCP) - return 0; - #if LIBJIN_NET_TEKCOS - int len; - tk_tcp_recv(&handle.tcpHandle, buffer, size, &len); - return len; - #endif - } - - int Socket::send(char* buffer, int size) - { - if (type != SocketType::TCP) - return 0; - #if LIBJIN_NET_TEKCOS - int len; - tk_tcp_send(&handle.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 LIBJIN_NET_TEKCOS - tk_UDPpack pack; - pack.data = buffer; - pack.len = size; - pack.ip.host = address; - pack.ip.port = port; - tk_udp_sendto(&handle.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 LIBJIN_NET_TEKCOS - tk_UDPpack pack; - pack.data = buffer; - pack.len = size; - pack.ip.host = address; - pack.ip.port = port; - tk_udp_recvfrom(&handle.udpHandle, &pack); - return pack.len; - #endif - } - - void Socket::close() - { - if (type == SocketType::TCP) - { - #if LIBJIN_NET_TEKCOS - tk_tcp_close(&handle.tcpHandle); - #endif - } - else if (type == SocketType::UDP) - { - #if LIBJIN_NET_TEKCOS - tk_udp_close(&handle.udpHandle); - #endif - } - } - - } // namespace net -} // namespace jin \ No newline at end of file diff --git a/src/libjin/Net/Socket.h b/src/libjin/Net/Socket.h deleted file mode 100644 index 04d3ab0..0000000 --- a/src/libjin/Net/Socket.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef __LIBJIN_NET_SOCKET_H -#define __LIBJIN_NET_SOCKET_H -#include "../configuration.h" -#if LIBJIN_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() {}; - Socket(const Socket& socket); - Socket(const 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(); - - protected: - #if LIBJIN_NET_TEKCOS - Socket(const tk_TCPsocket& tcpHandle); - Socket(const tk_UDPsocket& udpHandle); - union - { - tk_TCPsocket tcpHandle; - tk_UDPsocket udpHandle; - } handle; - #endif - SocketType type; - - }; - - } // namespace net -} // namespace jin - -#endif // LIBJIN_MODULES_NET -#endif // __LIBJIN_NET_SOCKET_H diff --git a/src/libjin/Net/je_net.cpp b/src/libjin/Net/je_net.cpp new file mode 100644 index 0000000..c417108 --- /dev/null +++ b/src/libjin/Net/je_net.cpp @@ -0,0 +1,24 @@ +#include "je_net.h" + +namespace jin +{ + namespace net + { + + bool Net::initSystem(const SettingBase* setting) + { + #ifdef _WIN32 + #if LIBJIN_NET_TEKCOS + tk_init(); + #endif + #endif + return true; + } + + void Net::quitSystem() + { + + } + + } // namespace net +} // namespace jin \ No newline at end of file diff --git a/src/libjin/Net/je_net.h b/src/libjin/Net/je_net.h new file mode 100644 index 0000000..7718085 --- /dev/null +++ b/src/libjin/Net/je_net.h @@ -0,0 +1,32 @@ +#ifndef __LIBJIN_NET_H +#define __LIBJIN_NET_H +#include "../core/je_configuration.h" +#if LIBJIN_MODULES_NET + +#include "../common/je_subsystem.hpp" + +#include "je_socket.h" + +namespace jin +{ + namespace net + { + + class Net : public Subsystem + { + public: + + protected: + singleton(Net); + Net() {}; + ~Net() {}; + bool initSystem(const SettingBase* setting) override; + void quitSystem() override; + + }; + + } // namespace net +} // namespace jin + +#endif // LIBJIN_MODULES_NET +#endif // __LIBJIN_NET_H diff --git a/src/libjin/Net/je_socket.cpp b/src/libjin/Net/je_socket.cpp new file mode 100644 index 0000000..3a8be7f --- /dev/null +++ b/src/libjin/Net/je_socket.cpp @@ -0,0 +1,190 @@ +#include "je_socket.h" + +namespace jin +{ + namespace net + { + Socket::Socket(const Socket& socket) + : handle(socket.handle) + , type(socket.type) + { + } + + Socket::Socket(const SocketInformation& info) + : type(info.type) + { + if (type == SocketType::TCP) + { + tk_IPaddress ip; + ip.host = info.address; + ip.port = info.port; + handle.tcpHandle = tk_tcp_open(ip); + } + else if (type == SocketType::UDP) + { + handle.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 LIBJIN_NET_TEKCOS + ip.host = tk_strtohl(address); + ip.port = port; + handle.tcpHandle = tk_tcp_open(ip); + #endif + } + else if (type == SocketType::UDP) + { + handle.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; + handle.tcpHandle = tk_tcp_open(ip); + } + else if (type == SocketType::UDP) + { + handle.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; + handle.tcpHandle = tk_tcp_open(ip); + } + else if (type == SocketType::UDP) + { + handle.udpHandle = tk_udp_open(port); + } + } + + #if LIBJIN_NET_TEKCOS + + Socket::Socket(const tk_TCPsocket& tcphandle) + { + handle.tcpHandle = tcphandle; + } + + Socket::Socket(const tk_UDPsocket& udphandle) + { + handle.udpHandle = udphandle; + } + + #endif // LIBJIN_NET_TEKCOS + + Socket::~Socket() + { + } + + void Socket::configureBlocking(bool blocking) + { + if (type != SocketType::TCP) + return; + #if LIBJIN_NET_TEKCOS + if (blocking) + tk_tcp_blocking(&handle.tcpHandle); + else + tk_tcp_nonblocking(&handle.tcpHandle); + #endif + } + + Socket* Socket::accept() + { + if (type != SocketType::TCP) + return nullptr; + Socket* client; + #if LIBJIN_NET_TEKCOS + tk_TCPsocket socket = tk_tcp_accept(&handle.tcpHandle); + client = new Socket(socket); + #endif + return client; + } + + int Socket::receive(char* buffer, int size) + { + if (type != SocketType::TCP) + return 0; + #if LIBJIN_NET_TEKCOS + int len; + tk_tcp_recv(&handle.tcpHandle, buffer, size, &len); + return len; + #endif + } + + int Socket::send(char* buffer, int size) + { + if (type != SocketType::TCP) + return 0; + #if LIBJIN_NET_TEKCOS + int len; + tk_tcp_send(&handle.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 LIBJIN_NET_TEKCOS + tk_UDPpack pack; + pack.data = buffer; + pack.len = size; + pack.ip.host = address; + pack.ip.port = port; + tk_udp_sendto(&handle.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 LIBJIN_NET_TEKCOS + tk_UDPpack pack; + pack.data = buffer; + pack.len = size; + pack.ip.host = address; + pack.ip.port = port; + tk_udp_recvfrom(&handle.udpHandle, &pack); + return pack.len; + #endif + } + + void Socket::close() + { + if (type == SocketType::TCP) + { + #if LIBJIN_NET_TEKCOS + tk_tcp_close(&handle.tcpHandle); + #endif + } + else if (type == SocketType::UDP) + { + #if LIBJIN_NET_TEKCOS + tk_udp_close(&handle.udpHandle); + #endif + } + } + + } // namespace net +} // namespace jin \ No newline at end of file diff --git a/src/libjin/Net/je_socket.h b/src/libjin/Net/je_socket.h new file mode 100644 index 0000000..ab6a561 --- /dev/null +++ b/src/libjin/Net/je_socket.h @@ -0,0 +1,62 @@ +#ifndef __LIBJIN_NET_SOCKET_H +#define __LIBJIN_NET_SOCKET_H +#include "../core/je_configuration.h" +#if LIBJIN_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() {}; + Socket(const Socket& socket); + Socket(const 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(); + + protected: + #if LIBJIN_NET_TEKCOS + Socket(const tk_TCPsocket& tcpHandle); + Socket(const tk_UDPsocket& udpHandle); + union + { + tk_TCPsocket tcpHandle; + tk_UDPsocket udpHandle; + } handle; + #endif + SocketType type; + + }; + + } // namespace net +} // namespace jin + +#endif // LIBJIN_MODULES_NET +#endif // __LIBJIN_NET_SOCKET_H diff --git a/src/libjin/Net/net.cpp b/src/libjin/Net/net.cpp deleted file mode 100644 index 940b239..0000000 --- a/src/libjin/Net/net.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "Net.h" - -namespace jin -{ - namespace net - { - - bool Net::initSystem(const SettingBase* setting) - { - #ifdef _WIN32 - #if LIBJIN_NET_TEKCOS - tk_init(); - #endif - #endif - return true; - } - - void Net::quitSystem() - { - - } - - } // namespace net -} // namespace jin \ No newline at end of file diff --git a/src/libjin/Net/net.h b/src/libjin/Net/net.h deleted file mode 100644 index 3324d11..0000000 --- a/src/libjin/Net/net.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __LIBJIN_NET_H -#define __LIBJIN_NET_H -#include "../configuration.h" -#if LIBJIN_MODULES_NET - -#include "../Common/Subsystem.hpp" -#include "Socket.h" - -namespace jin -{ - namespace net - { - - class Net : public Subsystem - { - public: - - protected: - singleton(Net); - Net() {}; - ~Net() {}; - bool initSystem(const SettingBase* setting) override; - void quitSystem() override; - - }; - - } // namespace net -} // namespace jin - -#endif // LIBJIN_MODULES_NET -#endif // __LIBJIN_NET_H -- cgit v1.1-26-g67d0