aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build/libjin/libjin.vcxproj.filters6
-rw-r--r--libjin/3rdparty/tekcos/tekcos.c12
-rw-r--r--libjin/Net/Net.cpp24
-rw-r--r--libjin/Net/Net.h26
-rw-r--r--libjin/Net/Socket.cpp20
-rw-r--r--libjin/Net/Socket.h1
-rw-r--r--test/04Network/main.cpp29
7 files changed, 98 insertions, 20 deletions
diff --git a/build/libjin/libjin.vcxproj.filters b/build/libjin/libjin.vcxproj.filters
index 261d25d..edc9a94 100644
--- a/build/libjin/libjin.vcxproj.filters
+++ b/build/libjin/libjin.vcxproj.filters
@@ -216,6 +216,9 @@
<ClCompile Include="..\..\libjin\Utils\XML\XML.cpp">
<Filter>Utils\XML</Filter>
</ClCompile>
+ <ClCompile Include="..\..\libjin\Net\Socket.cpp">
+ <Filter>Net</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\libjin\3rdparty\GLee\GLee.h">
@@ -412,6 +415,9 @@
<ClInclude Include="..\..\libjin\Utils\Proxy\lock.h">
<Filter>Utils\Proxy</Filter>
</ClInclude>
+ <ClInclude Include="..\..\libjin\Net\Socket.h">
+ <Filter>Net</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\libjin\README.md" />
diff --git a/libjin/3rdparty/tekcos/tekcos.c b/libjin/3rdparty/tekcos/tekcos.c
index 17ce7ec..a1fc94e 100644
--- a/libjin/3rdparty/tekcos/tekcos.c
+++ b/libjin/3rdparty/tekcos/tekcos.c
@@ -106,7 +106,13 @@ tk_TCPsocket* tk_tcp_open(tk_IPaddress ip)
addr.sin_port = htons(ip.port);
addr.sin_family = AF_INET;
- if (ip.host != INADDR_NONE && (ip.host != INADDR_ANY))
+//ws2def.h
+//#define INADDR_ANY (ULONG)0x00000000
+//#define INADDR_LOOPBACK 0x7f000001
+//#define INADDR_BROADCAST (ULONG)0xffffffff
+//#define INADDR_NONE 0xffffffff
+
+ if (ip.host != INADDR_NONE && ip.host != INADDR_ANY)
{
addr.sin_addr.s_addr = htonl(ip.host);
if (connect(sk->id, (const struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR)
@@ -206,7 +212,6 @@ int tk_tcp_recv(tk_TCPsocket* client, char* buffer, int size, int* len)
*len += l;
buffer += l;
} while (l == 0 || errno == EINTR);
- buffer[0] = '\0';
return 1;
error:
return 0;
@@ -214,7 +219,7 @@ error:
tk_TCPsocket* tk_tcp_accept(tk_TCPsocket* server)
{
- // client
+ // client socket
tk_TCPsocket* csk = (tk_TCPsocket*) malloc(sizeof(tk_TCPsocket));
if (server->type != SOCKET_TSERVER)
{
@@ -360,7 +365,6 @@ int tk_udp_recvfrom(tk_UDPsocket* sk, tk_UDPpack* pack)
return 0;
}
pack->len = n;
- pack->data[n] = '\0';
return 1;
}
diff --git a/libjin/Net/Net.cpp b/libjin/Net/Net.cpp
index e69de29..db39be7 100644
--- a/libjin/Net/Net.cpp
+++ b/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/libjin/Net/Net.h b/libjin/Net/Net.h
index 6b86a45..54ffede 100644
--- a/libjin/Net/Net.h
+++ b/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/libjin/Net/Socket.cpp b/libjin/Net/Socket.cpp
index f688c74..b7c621e 100644
--- a/libjin/Net/Socket.cpp
+++ b/libjin/Net/Socket.cpp
@@ -23,6 +23,24 @@ namespace net
}
}
+ 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;
@@ -138,7 +156,7 @@ namespace net
int Socket::receiveFrom(char* buffer, int size, unsigned int address, unsigned int port)
{
if (type != SocketType::UDP)
- return;
+ return 0;
int len;
#if JIN_NET_TEKCOS
tk_UDPpack pack;
diff --git a/libjin/Net/Socket.h b/libjin/Net/Socket.h
index ec60eb9..0eb27e0 100644
--- a/libjin/Net/Socket.h
+++ b/libjin/Net/Socket.h
@@ -29,6 +29,7 @@ namespace net
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);
diff --git a/test/04Network/main.cpp b/test/04Network/main.cpp
index d24e2b0..6db72ab 100644
--- a/test/04Network/main.cpp
+++ b/test/04Network/main.cpp
@@ -9,6 +9,7 @@ using namespace jin::input;
using namespace jin::audio;
using namespace jin::time;
using namespace jin::thread;
+using namespace jin::net;
Timers timers;
@@ -29,25 +30,22 @@ void onDraw()
}
-void thread2Runner(Thread* t)
+void netWorkThread(Thread* t)
{
- int i = 0;
+ Thread::Variant v = t->demand(1);
+ Socket* socket = (Socket*)v.pointer;
+ char buffer[2048] = {0};
while (true)
{
- if (i++ == 1000000000)
+ Socket* client = socket->accept();
+ if (client != nullptr)
{
- i = 0;
- cout << (char*)(t->demand(1).pointer);
+ client->receive(buffer, sizeof(buffer));
+ cout << buffer;
}
}
}
-void sendFunc(void* p)
-{
- Thread* t = (Thread*)p;
- t->send(1, "hello_");
-}
-
int main(int argc, char* argv[])
{
Game* game = Game::get();
@@ -69,11 +67,14 @@ int main(int argc, char* argv[])
wndSetting.resizable = true;
wnd->init(&wndSetting);
- Thread t("Count", thread2Runner);
+ Net::get()->init(nullptr);
- t.start();
- timers.after(2000, sendFunc, &t);
+ Socket socket(SocketType::TCP, 6438);
+ Thread t("Receive Network Data", netWorkThread);
+ t.start();
+ t.send(1, &socket);
+
game->run();
game->quit();