diff options
author | chai <chaifix@163.com> | 2020-02-11 11:29:07 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-02-11 11:29:07 +0800 |
commit | 160e1299ef3d95f8e8c48706d7f61dd3dc6c6b60 (patch) | |
tree | abe5ae5242d9cc6caf6edf103e662c44e978fca0 /src/libjin/net/socket.cpp | |
parent | e095043485d1d298571af6d9eca7f0db9009ea7a (diff) |
Diffstat (limited to 'src/libjin/net/socket.cpp')
-rw-r--r-- | src/libjin/net/socket.cpp | 370 |
1 files changed, 185 insertions, 185 deletions
diff --git a/src/libjin/net/socket.cpp b/src/libjin/net/socket.cpp index 3d59a57..cbea27f 100644 --- a/src/libjin/net/socket.cpp +++ b/src/libjin/net/socket.cpp @@ -2,189 +2,189 @@ namespace JinEngine { - namespace Net - { - Socket::Socket(const Socket& socket) - : mHandle(socket.mHandle) - , mType(socket.mType) - { - } - - Socket::Socket(const SocketInformation& info) - : mType(info.type) - { - if (mType == SocketType::TCP) - { - tk_IPaddress ip; - ip.host = info.address; - ip.port = info.port; - mHandle.tcpHandle = tk_tcp_open(ip); - } - else if (mType == SocketType::UDP) - { - mHandle.udpHandle = tk_udp_open(info.port); - } - } - - Socket::Socket(SocketType type, const char* address, unsigned short port) - { - mType = type; - if (mType == SocketType::TCP) - { - tk_IPaddress ip; - #if jin_net == jin_net_tekcos - ip.host = tk_strtohl(address); - ip.port = port; - mHandle.tcpHandle = tk_tcp_open(ip); - #endif - } - else if (mType == SocketType::UDP) - { - mHandle.udpHandle = tk_udp_open(port); - } - } - - Socket::Socket(SocketType type, unsigned int address, unsigned short port) - { - mType = type; - if (mType == SocketType::TCP) - { - tk_IPaddress ip; - ip.host = address; - ip.port = port; - mHandle.tcpHandle = tk_tcp_open(ip); - } - else if (mType == SocketType::UDP) - { - mHandle.udpHandle = tk_udp_open(port); - } - } - - Socket::Socket(SocketType type, unsigned short port) - { - mType = type; - if (mType == SocketType::TCP) - { - tk_IPaddress ip; - ip.host = 0; - ip.port = port; - mHandle.tcpHandle = tk_tcp_open(ip); - } - else if (mType == SocketType::UDP) - { - mHandle.udpHandle = tk_udp_open(port); - } - } - - #if jin_net == jin_net_tekcos - - Socket::Socket(const tk_TCPsocket& tcphandle) - { - mHandle.tcpHandle = tcphandle; - } - - Socket::Socket(const tk_UDPsocket& udphandle) - { - mHandle.udpHandle = udphandle; - } - - #endif // jin_net == jin_net_tekcos - - Socket::~Socket() - { - } - - void Socket::configureBlocking(bool blocking) - { - if (mType != SocketType::TCP) - return; - #if jin_net == jin_net_tekcos - if (blocking) - tk_tcp_blocking(&mHandle.tcpHandle); - else - tk_tcp_nonblocking(&mHandle.tcpHandle); - #endif - } - - Socket* Socket::accept() - { - if (mType != SocketType::TCP) - return nullptr; - Socket* client; - #if jin_net == jin_net_tekcos - tk_TCPsocket socket = tk_tcp_accept(&mHandle.tcpHandle); - client = new Socket(socket); - #endif - return client; - } - - int Socket::receive(char* buffer, int size) - { - if (mType != SocketType::TCP) - return 0; - #if jin_net == jin_net_tekcos - int len; - tk_tcp_recv(&mHandle.tcpHandle, buffer, size, &len); - return len; - #endif - } - - int Socket::send(char* buffer, int size) - { - if (mType != SocketType::TCP) - return 0; - #if jin_net == jin_net_tekcos - int len; - tk_tcp_send(&mHandle.tcpHandle, buffer, size, &len); - return len; - #endif - } - - void Socket::sendTo(char* buffer, int size, unsigned int address, unsigned int port) - { - if (mType != SocketType::UDP) - return; - #if jin_net == jin_net_tekcos - tk_UDPpack pack; - pack.data = buffer; - pack.len = size; - pack.ip.host = address; - pack.ip.port = port; - tk_udp_sendto(&mHandle.udpHandle, &pack); - #endif - } - - int Socket::receiveFrom(char* buffer, int size, unsigned int address, unsigned int port) - { - if (mType != SocketType::UDP) - return 0; - int len; - #if jin_net == jin_net_tekcos - tk_UDPpack pack; - pack.data = buffer; - pack.len = size; - pack.ip.host = address; - pack.ip.port = port; - tk_udp_recvfrom(&mHandle.udpHandle, &pack); - return pack.len; - #endif - } - - void Socket::close() - { - if (mType == SocketType::TCP) - { - #if jin_net == jin_net_tekcos - tk_tcp_close(&mHandle.tcpHandle); - #endif - } - else if (mType == SocketType::UDP) - { - #if jin_net == jin_net_tekcos - tk_udp_close(&mHandle.udpHandle); - #endif - } - } - - } // namespace Net + namespace Net + { + Socket::Socket(const Socket& socket) + : mHandle(socket.mHandle) + , mType(socket.mType) + { + } + + Socket::Socket(const SocketInformation& info) + : mType(info.type) + { + if (mType == SocketType::TCP) + { + tk_IPaddress ip; + ip.host = info.address; + ip.port = info.port; + mHandle.tcpHandle = tk_tcp_open(ip); + } + else if (mType == SocketType::UDP) + { + mHandle.udpHandle = tk_udp_open(info.port); + } + } + + Socket::Socket(SocketType type, const char* address, unsigned short port) + { + mType = type; + if (mType == SocketType::TCP) + { + tk_IPaddress ip; + #if jin_net == jin_net_tekcos + ip.host = tk_strtohl(address); + ip.port = port; + mHandle.tcpHandle = tk_tcp_open(ip); + #endif + } + else if (mType == SocketType::UDP) + { + mHandle.udpHandle = tk_udp_open(port); + } + } + + Socket::Socket(SocketType type, unsigned int address, unsigned short port) + { + mType = type; + if (mType == SocketType::TCP) + { + tk_IPaddress ip; + ip.host = address; + ip.port = port; + mHandle.tcpHandle = tk_tcp_open(ip); + } + else if (mType == SocketType::UDP) + { + mHandle.udpHandle = tk_udp_open(port); + } + } + + Socket::Socket(SocketType type, unsigned short port) + { + mType = type; + if (mType == SocketType::TCP) + { + tk_IPaddress ip; + ip.host = 0; + ip.port = port; + mHandle.tcpHandle = tk_tcp_open(ip); + } + else if (mType == SocketType::UDP) + { + mHandle.udpHandle = tk_udp_open(port); + } + } + + #if jin_net == jin_net_tekcos + + Socket::Socket(const tk_TCPsocket& tcphandle) + { + mHandle.tcpHandle = tcphandle; + } + + Socket::Socket(const tk_UDPsocket& udphandle) + { + mHandle.udpHandle = udphandle; + } + + #endif // jin_net == jin_net_tekcos + + Socket::~Socket() + { + } + + void Socket::configureBlocking(bool blocking) + { + if (mType != SocketType::TCP) + return; + #if jin_net == jin_net_tekcos + if (blocking) + tk_tcp_blocking(&mHandle.tcpHandle); + else + tk_tcp_nonblocking(&mHandle.tcpHandle); + #endif + } + + Socket* Socket::accept() + { + if (mType != SocketType::TCP) + return nullptr; + Socket* client; + #if jin_net == jin_net_tekcos + tk_TCPsocket socket = tk_tcp_accept(&mHandle.tcpHandle); + client = new Socket(socket); + #endif + return client; + } + + int Socket::receive(char* buffer, int size) + { + if (mType != SocketType::TCP) + return 0; + #if jin_net == jin_net_tekcos + int len; + tk_tcp_recv(&mHandle.tcpHandle, buffer, size, &len); + return len; + #endif + } + + int Socket::send(char* buffer, int size) + { + if (mType != SocketType::TCP) + return 0; + #if jin_net == jin_net_tekcos + int len; + tk_tcp_send(&mHandle.tcpHandle, buffer, size, &len); + return len; + #endif + } + + void Socket::sendTo(char* buffer, int size, unsigned int address, unsigned int port) + { + if (mType != SocketType::UDP) + return; + #if jin_net == jin_net_tekcos + tk_UDPpack pack; + pack.data = buffer; + pack.len = size; + pack.ip.host = address; + pack.ip.port = port; + tk_udp_sendto(&mHandle.udpHandle, &pack); + #endif + } + + int Socket::receiveFrom(char* buffer, int size, unsigned int address, unsigned int port) + { + if (mType != SocketType::UDP) + return 0; + int len; + #if jin_net == jin_net_tekcos + tk_UDPpack pack; + pack.data = buffer; + pack.len = size; + pack.ip.host = address; + pack.ip.port = port; + tk_udp_recvfrom(&mHandle.udpHandle, &pack); + return pack.len; + #endif + } + + void Socket::close() + { + if (mType == SocketType::TCP) + { + #if jin_net == jin_net_tekcos + tk_tcp_close(&mHandle.tcpHandle); + #endif + } + else if (mType == SocketType::UDP) + { + #if jin_net == jin_net_tekcos + tk_udp_close(&mHandle.udpHandle); + #endif + } + } + + } // namespace Net } // namespace JinEngine
\ No newline at end of file |