diff options
author | chai <chaifix@163.com> | 2018-08-08 23:24:00 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-08-08 23:24:00 +0800 |
commit | 847e5910abcc2431ad284ef843d482807349682d (patch) | |
tree | 41deb02688a59cf14241dbcd0d732df9783502fa | |
parent | 4afb418bf779400005f5452776ec5e4df1d2eda6 (diff) | |
parent | 1019479c017bc42fa48bb5e825d26b5f70b00787 (diff) |
Merge branch 'master' of warmcat.org:/home/git-repo/libjin
27 files changed, 684 insertions, 252 deletions
diff --git a/build/.vs/libjin/v14/.suo b/build/.vs/libjin/v14/.suo Binary files differindex cf42b3a..5c85708 100644 --- a/build/.vs/libjin/v14/.suo +++ b/build/.vs/libjin/v14/.suo diff --git a/build/02Audio/02Audio.vcxproj b/build/02Audio/02Audio.vcxproj index a06df79..5aa3542 100644 --- a/build/02Audio/02Audio.vcxproj +++ b/build/02Audio/02Audio.vcxproj @@ -120,7 +120,7 @@ </Link> </ItemDefinitionGroup> <ItemGroup> - <ClCompile Include="..\..\test\02Audio\audiotest.cpp" /> + <ClCompile Include="..\..\test\02Audio\main.cpp" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\libjin\libjin.vcxproj"> diff --git a/build/02Audio/02Audio.vcxproj.filters b/build/02Audio/02Audio.vcxproj.filters index 30ce2ab..29e09e3 100644 --- a/build/02Audio/02Audio.vcxproj.filters +++ b/build/02Audio/02Audio.vcxproj.filters @@ -15,7 +15,7 @@ </Filter> </ItemGroup> <ItemGroup> - <ClCompile Include="..\..\test\02Audio\audiotest.cpp"> + <ClCompile Include="..\..\test\02Audio\main.cpp"> <Filter>源文件</Filter> </ClCompile> </ItemGroup> diff --git a/build/03Thread/03Thread.vcxproj b/build/03Thread/03Thread.vcxproj index ddd8193..6cad582 100644 --- a/build/03Thread/03Thread.vcxproj +++ b/build/03Thread/03Thread.vcxproj @@ -125,8 +125,7 @@ </ProjectReference> </ItemGroup> <ItemGroup> - <ClCompile Include="..\..\test\03Thread\threadtest.cpp" /> - <ClCompile Include="main.cpp" /> + <ClCompile Include="..\..\test\03Thread\main.cpp" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> diff --git a/build/03Thread/03Thread.vcxproj.filters b/build/03Thread/03Thread.vcxproj.filters index d0d38cf..a9c0e02 100644 --- a/build/03Thread/03Thread.vcxproj.filters +++ b/build/03Thread/03Thread.vcxproj.filters @@ -15,10 +15,7 @@ </Filter> </ItemGroup> <ItemGroup> - <ClCompile Include="..\..\test\03Thread\threadtest.cpp"> - <Filter>源文件</Filter> - </ClCompile> - <ClCompile Include="main.cpp"> + <ClCompile Include="..\..\test\03Thread\main.cpp"> <Filter>源文件</Filter> </ClCompile> </ItemGroup> diff --git a/build/03Thread/main.cpp b/build/03Thread/main.cpp deleted file mode 100644 index ed12172..0000000 --- a/build/03Thread/main.cpp +++ /dev/null @@ -1,154 +0,0 @@ -//#include <SDL2/SDL.h> -//#include <SDL2/SDL_thread.h> -//#include <iostream> -//#include <sstream> -//#include <stdexcept> -// -//class SdlMutex -//{ -//public: -// SdlMutex() -// { -// mutex = SDL_CreateMutex(); -// if (!mutex) throw std::runtime_error("SDL_CreateMutex == NULL"); -// } -// -// ~SdlMutex() -// { -// SDL_DestroyMutex(mutex); -// } -// -// void lock() -// { -// if (SDL_mutexP(mutex) == -1) throw std::runtime_error("SDL_mutexP == -1"); -// // Note: -// // -1 does not mean it was already locked - it means there was an error in locking - -// // if it was locked it will just block - see SDL_mutexP(3) -// } -// -// void unlock() -// { -// if (SDL_mutexV(mutex) == -1) throw std::runtime_error("SDL_mutexV == -1"); -// } -// -// SDL_mutex* underlying() -// { -// return mutex; -// } -//private: -// SDL_mutex* mutex; -//}; -// -//class SdlScopedLock -//{ -//public: -// SdlScopedLock(SdlMutex& mutex) -// : -// mutex(mutex) -// { -// mutex.lock(); -// } -// ~SdlScopedLock() -// { -// try -// { -// this->unlock(); -// } -// catch (const std::exception& e) -// { -// // Destructors should never throw ... -// std::cerr << "SdlScopedLock::~SdlScopedLock - caught : " << e.what() << std::endl; -// } -// } -// void unlock() -// { -// mutex.unlock(); -// } -//private: -// SdlMutex& mutex; -//}; -// -//class ThreadData -//{ -//public: -// ThreadData() -// : -// dataReady(false), -// done(false) -// { -// condition = SDL_CreateCond(); -// } -// -// ~ThreadData() -// { -// SDL_DestroyCond(condition); -// } -// -// // Using stringstream so I can just shift on integers... -// std::stringstream data; -// bool dataReady; -// bool done; -// SdlMutex mutex; -// SDL_cond* condition; -//}; -// -//int threadFunction(void* data) -//{ -// try -// { -// ThreadData* threadData = static_cast< ThreadData* >(data); -// -// for (size_t i = 0; i < 100; i++) -// { -// { -// SdlScopedLock lock(threadData->mutex); -// // Everything in this scope is now syncronized with the mutex -// if (i != 0) threadData->data << ", "; -// threadData->data << i; -// threadData->dataReady = true; -// } // threadData->mutex is automatically unlocked here -// // Its important to note that condition should be signaled after mutex is unlocked -// if (SDL_CondSignal(threadData->condition) == -1) throw std::runtime_error("Failed to signal"); -// } -// { -// SdlScopedLock lock(threadData->mutex); -// threadData->done = true; -// } -// if (SDL_CondSignal(threadData->condition) == -1) throw std::runtime_error("Failed to signal"); -// return 0; -// } -// catch (const std::exception& e) -// { -// std::cerr << "Caught : " << e.what() << std::endl; -// return 1; -// } -//} -// -//int main(int argc, char* argv[]) -//{ -// ThreadData threadData; -// SDL_Thread* thread = SDL_CreateThread(threadFunction, "", &threadData); -// -// while (true) -// { -// SdlScopedLock lock(threadData.mutex); -// while (threadData.dataReady == false && threadData.done == false) -// { -// // NOTE: must call condition wait with mutex already locked -// if (SDL_CondWait(threadData.condition, threadData.mutex.underlying()) == -1) throw std::runtime_error("Failed to wait"); -// } -// // once dataReady == true or threadData.done == true we get here -// std::cout << "Got data = " << threadData.data.str() << std::endl; -// threadData.data.str(""); -// threadData.dataReady = false; -// if (threadData.done) -// { -// std::cout << "child done - ending" << std::endl; -// break; -// } -// } -// -// int status = 99; -// SDL_WaitThread(thread, &status); -// std::cerr << "Thread completed with : " << status << std::endl; -//}
\ No newline at end of file diff --git a/build/04Network/04Network.vcxproj b/build/04Network/04Network.vcxproj new file mode 100644 index 0000000..b992a7a --- /dev/null +++ b/build/04Network/04Network.vcxproj @@ -0,0 +1,133 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>{85071432-24B6-46D4-98D8-DAA63183093C}</ProjectGuid> + <RootNamespace>My04Network</RootNamespace> + <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v140</PlatformToolset> + <CharacterSet>MultiByte</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v140</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>MultiByte</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v140</PlatformToolset> + <CharacterSet>MultiByte</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v140</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>MultiByte</CharacterSet> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Label="Shared"> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup /> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <Optimization>Disabled</Optimization> + <SDLCheck>true</SDLCheck> + <AdditionalIncludeDirectories>$(SolutionDir)..\libjin\;$(SolutionDir)\lib\SDL2-2.0.5\include\</AdditionalIncludeDirectories> + </ClCompile> + <Link> + <AdditionalLibraryDirectories>$(SolutionDir)\lib\SDL2-2.0.5\lib\x86\</AdditionalLibraryDirectories> + <AdditionalDependencies>SDL2main.lib;SDL2.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies> + <SubSystem>Console</SubSystem> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <Optimization>Disabled</Optimization> + <SDLCheck>true</SDLCheck> + </ClCompile> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <Optimization>MaxSpeed</Optimization> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <SDLCheck>true</SDLCheck> + <AdditionalIncludeDirectories>$(SolutionDir)..\libjin\;$(SolutionDir)\lib\SDL2-2.0.5\include\</AdditionalIncludeDirectories> + </ClCompile> + <Link> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <AdditionalLibraryDirectories>$(SolutionDir)\lib\SDL2-2.0.5\lib\x86\</AdditionalLibraryDirectories> + <AdditionalDependencies>SDL2main.lib;SDL2.lib;opengl32.lib;glu32.lib;%(AdditionalDependencies)</AdditionalDependencies> + <SubSystem>Console</SubSystem> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <Optimization>MaxSpeed</Optimization> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <SDLCheck>true</SDLCheck> + </ClCompile> + <Link> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + </Link> + </ItemDefinitionGroup> + <ItemGroup> + <ClCompile Include="..\..\test\04Network\main.cpp" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\libjin\libjin.vcxproj"> + <Project>{407e9199-d39c-4460-b218-0c29ab42483b}</Project> + </ProjectReference> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project>
\ No newline at end of file diff --git a/build/04Network/04Network.vcxproj.filters b/build/04Network/04Network.vcxproj.filters new file mode 100644 index 0000000..01aea0c --- /dev/null +++ b/build/04Network/04Network.vcxproj.filters @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="源文件"> + <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> + <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> + </Filter> + <Filter Include="头文件"> + <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> + <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> + </Filter> + <Filter Include="资源文件"> + <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> + <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> + </Filter> + </ItemGroup> + <ItemGroup> + <ClCompile Include="..\..\test\04Network\main.cpp"> + <Filter>源文件</Filter> + </ClCompile> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/build/libjin.sln b/build/libjin.sln index eb11a5f..7bdc3c3 100644 --- a/build/libjin.sln +++ b/build/libjin.sln @@ -11,6 +11,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "02Audio", "02Audio\02Audio. EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "03Thread", "03Thread\03Thread.vcxproj", "{0E49D105-2032-4825-9FA1-54B1B94E3655}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "04Network", "04Network\04Network.vcxproj", "{85071432-24B6-46D4-98D8-DAA63183093C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -51,6 +53,14 @@ Global {0E49D105-2032-4825-9FA1-54B1B94E3655}.Release|x64.Build.0 = Release|x64 {0E49D105-2032-4825-9FA1-54B1B94E3655}.Release|x86.ActiveCfg = Release|Win32 {0E49D105-2032-4825-9FA1-54B1B94E3655}.Release|x86.Build.0 = Release|Win32 + {85071432-24B6-46D4-98D8-DAA63183093C}.Debug|x64.ActiveCfg = Debug|x64 + {85071432-24B6-46D4-98D8-DAA63183093C}.Debug|x64.Build.0 = Debug|x64 + {85071432-24B6-46D4-98D8-DAA63183093C}.Debug|x86.ActiveCfg = Debug|Win32 + {85071432-24B6-46D4-98D8-DAA63183093C}.Debug|x86.Build.0 = Debug|Win32 + {85071432-24B6-46D4-98D8-DAA63183093C}.Release|x64.ActiveCfg = Release|x64 + {85071432-24B6-46D4-98D8-DAA63183093C}.Release|x64.Build.0 = Release|x64 + {85071432-24B6-46D4-98D8-DAA63183093C}.Release|x86.ActiveCfg = Release|Win32 + {85071432-24B6-46D4-98D8-DAA63183093C}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/build/libjin/libjin.vcxproj b/build/libjin/libjin.vcxproj index d2f70d0..1e91426 100644 --- a/build/libjin/libjin.vcxproj +++ b/build/libjin/libjin.vcxproj @@ -48,6 +48,7 @@ <ClCompile Include="..\..\libjin\Math\Matrix.cpp" /> <ClCompile Include="..\..\libjin\Math\Vector.cpp" /> <ClCompile Include="..\..\libjin\Net\Net.cpp" /> + <ClCompile Include="..\..\libjin\Net\Socket.cpp" /> <ClCompile Include="..\..\libjin\Thread\Thread.cpp" /> <ClCompile Include="..\..\libjin\Tilemap\Tilemap.cpp" /> <ClCompile Include="..\..\libjin\Time\Timer.cpp" /> @@ -106,6 +107,7 @@ <ClInclude Include="..\..\libjin\Math\Vector.h" /> <ClInclude Include="..\..\libjin\modules.h" /> <ClInclude Include="..\..\libjin\Net\Net.h" /> + <ClInclude Include="..\..\libjin\Net\Socket.h" /> <ClInclude Include="..\..\libjin\Physics\Physics.h" /> <ClInclude Include="..\..\libjin\Physics\Rigid.h" /> <ClInclude Include="..\..\libjin\Thread\Thread.h" /> 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 9705fe2..a1fc94e 100644 --- a/libjin/3rdparty/tekcos/tekcos.c +++ b/libjin/3rdparty/tekcos/tekcos.c @@ -13,18 +13,18 @@ enum { TK_SUCCESS = 0, - TK_INITFAILED, // init tekcos failed + TK_INITFAILED, // init tekcos failed - TK_COULDNETCREATESOCKET, // couldn't create socket. - TK_CONNECTFAILED, // connect to remote server failed. - TK_BINDSOCKETFAILED, // couldn't bind socket to port - TK_LISTENSOCKETFAILED, // couldn't listen to port - TK_INVALIDSOCKET, // invalid socket - TK_WRONGSOCKETTPYE, // wrong socket type + TK_COULDNETCREATESOCKET, // couldn't create socket. + TK_CONNECTFAILED, // connect to remote server failed. + TK_BINDSOCKETFAILED, // couldn't bind socket to port + TK_LISTENSOCKETFAILED, // couldn't listen to port + TK_INVALIDSOCKET, // invalid socket + TK_WRONGSOCKETTPYE, // wrong socket type - TK_INVALIDTARGET, // cant sendto. + TK_INVALIDTARGET, // cant sendto. - TK_UNKNOWN, // unknown + TK_UNKNOWN, // unknown }; // error code @@ -74,7 +74,6 @@ uint32 tk_strtohl(const char* str) { struct in_addr inaddr; inet_pton(AF_INET, str, (void*)&inaddr); - // host long return ntohl(inaddr.s_addr); } @@ -105,11 +104,16 @@ tk_TCPsocket* tk_tcp_open(tk_IPaddress ip) struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_port = htons(ip.port); - // 32bit address and 16bit port number. addr.sin_family = AF_INET; - if (ip.host != INADDR_NONE && (ip.host != INADDR_ANY)) - { // connet to an existed remote server. +//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) { @@ -117,8 +121,9 @@ tk_TCPsocket* tk_tcp_open(tk_IPaddress ip) goto error; } sk->type = SOCKET_TCLIENT; - }else - { // create a listenning server. + } + else + { addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sk->id, (const struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) @@ -148,12 +153,12 @@ int tk_tcp_close(tk_TCPsocket* sk) { if (sk->id == INVALID_SOCKET) goto error; -#ifdef _WIN32 + #ifdef _WIN32 closesocket(sk->id); -#else - close(sk->id); -#endif - free(sk); + #else + close(sk->id); + #endif + free(sk); return 1; } error: @@ -161,23 +166,23 @@ error: return 0; } -int tk_tcp_send(tk_TCPsocket* client, const void* buffer, int bsize, int* sent) +int tk_tcp_send(tk_TCPsocket* client, const void* buffer, int size, int* sent) { - // byte poiter. const char *data = (const char*)buffer; if (client->type != SOCKET_TCLIENT) - {// only cliednt can send stuff. + { state = TK_WRONGSOCKETTPYE; goto error; } - //send(sk->id, buffer, bsize, 0); - int left = bsize; - if(sent) *sent = 0; + int left = size; + if(sent) + *sent = 0; int len = 0; do { len = send(client->id, data, left, 0); if (len > 0) { - if(sent) *sent += len; + if(sent) + *sent += len; left -= len; data += len; } @@ -187,24 +192,26 @@ error: return 0; } -int tk_tcp_recv(tk_TCPsocket* client, char* buffer, int bsize, int* len) +int tk_tcp_recv(tk_TCPsocket* client, char* buffer, int size, int* len) { *len = 0; if (client->type != SOCKET_TCLIENT) - {// only client can be recieved + { state = TK_WRONGSOCKETTPYE; goto error; } + int l = 0; do { - *len = recv(client->id, buffer, bsize, 0); - if (*len == SOCKET_ERROR) + l = recv(client->id, buffer, size - *len, 0); + if (l == SOCKET_ERROR) { state = TK_UNKNOWN; goto error; } - } while (*len == 0 || errno == EINTR); // if was suspended, recieve again. - buffer[*len] = '\0'; + *len += l; + buffer += l; + } while (l == 0 || errno == EINTR); return 1; error: return 0; @@ -212,9 +219,10 @@ error: tk_TCPsocket* tk_tcp_accept(tk_TCPsocket* server) { + // client socket tk_TCPsocket* csk = (tk_TCPsocket*) malloc(sizeof(tk_TCPsocket)); if (server->type != SOCKET_TSERVER) - {// only server can accept connection + { state = TK_WRONGSOCKETTPYE; goto error; } @@ -240,38 +248,38 @@ error: int tk_tcp_nonblocking(tk_TCPsocket* sk) { -#if defined(__BEOS__) && defined(SO_NONBLOCK) + #if defined(__BEOS__) && defined(SO_NONBLOCK) { long b = 1; setsockopt(sk->id, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b)); } -#elif defined(WIN32) + #elif defined(WIN32) { unsigned long mode = 1; ioctlsocket(sk->id, FIONBIO, &mode); } -#elif defined(__OS2__) + #elif defined(__OS2__) { int dontblock = 1; ioctl(sk->id, FIONBIO, &dontblock); } -#endif + #endif return 1; } int tk_tcp_blocking(tk_TCPsocket* sk) { -#if defined(__BEOS__) && defined(SO_NONBLOCK) + #if defined(__BEOS__) && defined(SO_NONBLOCK) { long b = 0; setsockopt(sk->id, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b)); } -#elif defined(WIN32) + #elif defined(WIN32) { unsigned long mode = 0; ioctlsocket(sk->id, FIONBIO, &mode); } -#elif defined(__OS2__) + #elif defined(__OS2__) { int dontblock = 0; ioctl(sk->id, FIONBIO, &dontblock); @@ -307,11 +315,11 @@ tk_UDPsocket* tk_udp_open(uint16 portnumber) goto error; } if (portnumber <= 0) - {// client, just return + { return sk; } else - {// server, need to bind address to socket + { struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_addr.s_addr = htonl(INADDR_ANY); @@ -357,7 +365,6 @@ int tk_udp_recvfrom(tk_UDPsocket* sk, tk_UDPpack* pack) return 0; } pack->len = n; - pack->data[n] = '\0'; return 1; } @@ -367,11 +374,11 @@ int tk_udp_close(tk_UDPsocket* sk) { if (sk->id != INVALID_SOCKET) { -#ifdef _WIN32 + #ifdef _WIN32 closesocket(sk->id); -#else - close(sk->id); -#endif + #else + close(sk->id); + #endif } free(sk); } @@ -383,4 +390,4 @@ int tk_freepack(tk_UDPpack* pack) free(pack->data); free(pack); return 1; -} +}
\ No newline at end of file diff --git a/libjin/3rdparty/tekcos/tekcos.h b/libjin/3rdparty/tekcos/tekcos.h index e7d8662..a212aae 100644 --- a/libjin/3rdparty/tekcos/tekcos.h +++ b/libjin/3rdparty/tekcos/tekcos.h @@ -1,3 +1,4 @@ +// copyright chai #ifndef _TEKCOS_H #define _TEKCOS_H @@ -56,17 +57,11 @@ typedef struct // otherwise, connect to a remote server with given // ip address. tk_TCPsocket* tk_tcp_open(tk_IPaddress ip); - int tk_tcp_close(tk_TCPsocket* sk); - int tk_tcp_send(tk_TCPsocket* client, const void* buffer, int bsize, int* len); - int tk_tcp_recv(tk_TCPsocket* client, char* buffer, int bsize, int* len); - tk_TCPsocket* tk_tcp_accept(tk_TCPsocket* server); - int tk_tcp_nonblocking(tk_TCPsocket* sk); - int tk_tcp_blocking(tk_TCPsocket* sk); /* @@ -89,15 +84,10 @@ typedef struct }tk_UDPpack; tk_UDPsocket* tk_udp_open(uint16 portnumber); - int tk_udp_close(tk_UDPsocket* sk); - int tk_udp_sendto(tk_UDPsocket* sk, tk_UDPpack* pack); - int tk_udp_recvfrom(tk_UDPsocket* sk, tk_UDPpack* pack); - int tk_freepack(tk_UDPpack* pack); - // Get error message if some errors occured. const char* tk_errmsg(); diff --git a/libjin/Common/Data.h b/libjin/Common/Data.h index 38b8c7d..7fcc389 100644 --- a/libjin/Common/Data.h +++ b/libjin/Common/Data.h @@ -4,10 +4,27 @@ namespace jin { - struct DataBuffer + class DataBuffer { - unsigned int len; - char data[0]; + public: + DataBuffer(int n) + : len(n) + { + buffer = new char[len]; + memset(buffer, 0, len); + } + ~DataBuffer() + { + delete[] buffer; + } + char* operator&() + { + return buffer; + } + + private: + char* buffer; + int len; }; } // jin diff --git a/libjin/Common/Subsystem.hpp b/libjin/Common/Subsystem.hpp index f4e270b..c3af3dc 100644 --- a/libjin/Common/Subsystem.hpp +++ b/libjin/Common/Subsystem.hpp @@ -14,7 +14,7 @@ namespace jin struct Setting {}; typedef Setting SettingBase; - bool init(const SettingBase* setting) + bool init(const SettingBase* setting = nullptr) { static bool success = initSystem(setting); return success; 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 new file mode 100644 index 0000000..b7c621e --- /dev/null +++ b/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/libjin/Net/Socket.h b/libjin/Net/Socket.h new file mode 100644 index 0000000..0eb27e0 --- /dev/null +++ b/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 diff --git a/libjin/Thread/Thread.cpp b/libjin/Thread/Thread.cpp index 374778b..064d3db 100644 --- a/libjin/Thread/Thread.cpp +++ b/libjin/Thread/Thread.cpp @@ -296,6 +296,13 @@ namespace thread return v; } + void Thread::remove(int slot) + { + lock(); + common->remove(slot); + unlock(); + } + } // thread } // jin diff --git a/libjin/Thread/Thread.h b/libjin/Thread/Thread.h index aeb0d9e..37c978d 100644 --- a/libjin/Thread/Thread.h +++ b/libjin/Thread/Thread.h @@ -42,23 +42,36 @@ namespace thread class Thread { public: - - union Variant + struct Variant { - int integer; - bool boolean; - char character; - const char* cstring; - void* pointer; - float real; - - Variant() {}; - Variant(int i) : integer(i) {}; - Variant(float f) : real(f) {}; - Variant(bool b) : boolean(b) {}; - Variant(char c) : character(c) {}; - Variant(const char* s) : cstring(s) {}; - Variant(void* p) : pointer(p) {}; + enum Type + { + NONE = 0, + INTERGER, + BOOLEAN, + CHARACTER, + CSTRING, + POINTER, + REAL, + }; + Type type; + union + { + int integer; + bool boolean; + char character; + const char* cstring; + void* pointer; + float real; + }; + Variant() :type(NONE) {}; + Variant(const Variant& v){ memcpy(this, &v, sizeof(v)); } + Variant(int i) : integer(i), type(INTERGER) {}; + Variant(float f) : real(f), type(REAL) {}; + Variant(bool b) : boolean(b), type(BOOLEAN) {}; + Variant(char c) : character(c), type(CHARACTER) {}; + Variant(const char* s) : cstring(s), type(CSTRING) {}; + Variant(void* p) : pointer(p), type(POINTER) {}; }; private: @@ -76,6 +89,8 @@ namespace thread static const int SLOT_ERROR = -1; static const int SLOT_WARN = -2; + static const int SLOT_INFO = -3; + static const int SLOT_DEBUG = -4; private: std::map<int, Variant> share; // threads shared value @@ -91,6 +106,7 @@ namespace thread bool receive(int slot); Variant fetch(int slot); Variant demand(int slot); + void remove(int slot); const char* getName(); bool isRunning(); void lock(); diff --git a/libjin/Utils/Log.h b/libjin/Utils/Log.h index b047402..50ec3c8 100644 --- a/libjin/Utils/Log.h +++ b/libjin/Utils/Log.h @@ -65,19 +65,19 @@ void Loghelper::log(Level _level, const char* _fmt, ...) switch (_level) { case LV_ERROR: - levelStr = "Error: "; + levelStr = "[Jin Error]:"; break; case LV_WARN: - levelStr = "Warn: "; + levelStr = "[Jin Warn]:"; break; case LV_INFO: - levelStr = "Info: "; + levelStr = "[Jin Info]:"; break; case LV_DEBUG: - levelStr = "Debug: "; + levelStr = "[Jin Debug]:"; break; default: - levelStr = "Unknown: "; + levelStr = "[Jin Unknown]:"; break; } char buffer[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 }; diff --git a/libjin/jin.h b/libjin/jin.h index ad83fae..239fddd 100644 --- a/libjin/jin.h +++ b/libjin/jin.h @@ -15,9 +15,9 @@ #include "Time/Timer.h" #include "Thread/Thread.h" -const char* JIN_VERSION = "Jin 0.1"; -const char* JIN_AUTHOR = "Chai"; -const char* JIN_RELEASE = "Jin 0.1.1"; -const int JIN_VERSION_NUM = 101; +#define JIN_VERSION "Jin 0.1"; +#define JIN_AUTHOR "Chai"; +#define JIN_RELEASE "Jin 0.1.1"; +#define JIN_VERSION_NUM 101; #endif // __JIN_H
\ No newline at end of file diff --git a/libjin/modules.h b/libjin/modules.h index 06c14df..6cc9336 100644 --- a/libjin/modules.h +++ b/libjin/modules.h @@ -20,6 +20,7 @@ #define JIN_MODULES_MATH 1 #define JIN_MODULES_NET 1 +#define JIN_NET_TEKCOS 1 #define JIN_MODULES_PHYSICS 1 #define JIN_PHYSICS_BOX2D 1 diff --git a/test/02Audio/audiotest.cpp b/test/02Audio/main.cpp index 4576cfa..4576cfa 100644 --- a/test/02Audio/audiotest.cpp +++ b/test/02Audio/main.cpp diff --git a/test/03Thread/threadtest.cpp b/test/03Thread/main.cpp index a792e53..a792e53 100644 --- a/test/03Thread/threadtest.cpp +++ b/test/03Thread/main.cpp diff --git a/test/04Network/main.cpp b/test/04Network/main.cpp new file mode 100644 index 0000000..6db72ab --- /dev/null +++ b/test/04Network/main.cpp @@ -0,0 +1,84 @@ +#include <iostream> +#include <functional> +#include "jin.h" + +using namespace std; +using namespace jin::core; +using namespace jin::graphics; +using namespace jin::input; +using namespace jin::audio; +using namespace jin::time; +using namespace jin::thread; +using namespace jin::net; + +Timers timers; + +void onEvent(jin::input::Event* e) +{ + static Game* game = Game::get(); + if (e->type == EventType::QUIT) + game->stop(); +} + +void onUpdate(int ms) +{ + timers.update(ms); +} + +void onDraw() +{ + +} + +void netWorkThread(Thread* t) +{ + Thread::Variant v = t->demand(1); + Socket* socket = (Socket*)v.pointer; + char buffer[2048] = {0}; + while (true) + { + Socket* client = socket->accept(); + if (client != nullptr) + { + client->receive(buffer, sizeof(buffer)); + cout << buffer; + } + } +} + +int main(int argc, char* argv[]) +{ + Game* game = Game::get(); + Game::Setting setting; + setting.eventHandler = onEvent; + setting.updater = onUpdate; + setting.drawer = onDraw; + setting.loader = nullptr; + game->init(&setting); + + Window* wnd = Window::get(); + Window::Setting wndSetting; + wndSetting.width = 400; + wndSetting.height = 300; + wndSetting.title = "test"; + wndSetting.fps = 60; + wndSetting.vsync = false; + wndSetting.fullscreen = false; + wndSetting.resizable = true; + wnd->init(&wndSetting); + + Net::get()->init(nullptr); + + Socket socket(SocketType::TCP, 6438); + + Thread t("Receive Network Data", netWorkThread); + t.start(); + t.send(1, &socket); + + game->run(); + + game->quit(); + wnd->quit(); + + return 0; +}
\ No newline at end of file |