aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Net/Socket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/Net/Socket.cpp')
-rw-r--r--src/libjin/Net/Socket.cpp366
1 files changed, 183 insertions, 183 deletions
diff --git a/src/libjin/Net/Socket.cpp b/src/libjin/Net/Socket.cpp
index 32d160c..2a1454b 100644
--- a/src/libjin/Net/Socket.cpp
+++ b/src/libjin/Net/Socket.cpp
@@ -2,189 +2,189 @@
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
- }
+ 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::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
- }
- }
-
-} // net
+ 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
+ }
+ }
+
+ } // net
} // jin \ No newline at end of file