提交 85d05359 编写于 作者: X xuchi

解决方案新增项目:http_server

上级 04cbcc1f
......@@ -150,6 +150,26 @@ namespace doyou {
return false;
}
// 设置客户端IP地址
void SetIp(char* ip)
{
if (nullptr == ip) {
return;
}
// 分析:如果src传入实际字串长度小于para3, 会将src字串拷贝至dest,不足para3长度用空字符填充
// 如果src传入实际字串长度等于para3, 将src字串拷贝至dest
// 如果src传入实际字串长度大于para3, 只将para3指定长度拷贝至dest, 避免数据溢出
strncpy(_ip, ip, INET6_ADDRSTRLEN); // 比strcpy函数更安全,para3-要复制的字符个数。如果源字符串的长度小于n个字符,则目标字符串将被用空字符填充直到其长度为n个字符。
}
// 返回客户端IP地址
char* GetIp()
{
return _ip; // 指针变量间值拷贝,通过指针变量的值可以间接访问指针变量值指向的对象
}
// Iocp网络模型特有代码块
#ifdef CELL_USE_IOCP
// 分析:使用Iocp比使用其它网络模型要多两个IO_DATA_BASE部分,方法有两个
......@@ -205,24 +225,7 @@ namespace doyou {
return (_isPostRecv || _isPostSend); // 逻辑或:只要有一个及其以上为真,则为真
}
// 设置客户端IP地址
void SetIp(char* ip)
{
if (nullptr == ip) {
return;
}
// 分析:如果src传入实际字串长度小于para3, 会将src字串拷贝至dest,不足para3长度用空字符填充
// 如果src传入实际字串长度等于para3, 将src字串拷贝至dest
// 如果src传入实际字串长度大于para3, 只将para3指定长度拷贝至dest, 避免数据溢出
strncpy(_ip, ip, INET6_ADDRSTRLEN); // 比strcpy函数更安全,para3-要复制的字符个数。如果源字符串的长度小于n个字符,则目标字符串将被用空字符填充直到其长度为n个字符。
}
// 返回客户端IP地址
char* GetIp()
{
return _ip; // 指针变量间值拷贝,通过指针变量的值可以间接访问指针变量值指向的对象
}
#endif // CELL_USE_IOCP
......
......@@ -6,7 +6,7 @@
*/
#include<string>
#include<map>
#include"Log.hpp" // 分析:在celllog.hpp里面不能用cellconfig.hpp, 存在循环引用问题(这也是将声明和定义写在同一文件的弊端),会出现重定义问题。如果将声明和定义份文件写,就可以互相引用了,因为可以重复声明。
#include"Log.hpp" // 分析:在log.hpp里面不能用config.hpp, 存在循环引用问题(这也是将声明和定义写在同一文件的弊端),会出现重定义问题。如果将声明和定义份文件写,就可以互相引用了,因为可以重复声明。
namespace doyou {
namespace io { // 增加两层命令空间,避免在其它项目应用时类名出现冲突
......
#ifndef _CELL_LOG_HPP_
#define _CELL_LOG_HPP_
//#include"CELL.hpp"
#include"CELL.hpp"
#include "Task.hpp"
#include <ctime>
//#include <stdio.h>
namespace doyou {
namespace io { // 增加两层命令空间,避免在其它项目应用时类名出现冲突
......
......@@ -76,5 +76,7 @@ IP直接存入黑名单中,所以,服务端获取客户端IP地址很有意
实际记录本地文件或终端输出是另一个线程,如果调用接口线程传入的是一个临时变量,在具体
输出日志线程在访问变量是很可能已经被系统回收,从而记录日志为乱码。
24.在一个进程中,主线程已经退出,子线程还没有退出,子线程会被破退出,同时可能出现系统错误。
// 高性能服务器可采用的网络通信模型三选一:select/epoll/iocp/
//#include "TcpSelectServer.hpp" // using select, both linux and windows.
#include "TcpSelectServer.hpp" // using select, both linux and windows.
//#include "TcpEpollServer.hpp" // using epoll, only linux.
#include "TcpIocpServer.hpp" // using iocp, only windows.
//#include "TcpIocpServer.hpp" // using iocp, only windows.
#include "MsgStream.hpp"
#include "Config.hpp"
......@@ -11,7 +11,7 @@
using namespace doyou::io; // 使用名称空间
// 可使用服务模型(三选一):1-TcpSelectServer 2-TcpEpollServer 3-TcpIocpServer
class MyServer : public TcpIocpServer
class MyServer : public TcpSelectServer
{
public:
MyServer()
......
Info [2023-4-11 23:18:10]Log::setLogPath success,<HelloCpp.log.txt,w>
Info [2023-4-11 23:18:10]a=500
Info [2023-4-11 23:18:10]the first string.
Info [2023-4-11 23:18:10]the second string.
Info [2023-4-11 23:18:10]the third string.
Info [2023-4-11 23:18:10]the four string.
#include "TcpIocpServer.hpp"
//#include "Log.hpp"
#include "Log.hpp"
#include "Config.hpp"
using namespace doyou::io; // 使用名称空间
......
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.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>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{3B0734CE-4E91-498A-9621-AB0385F7AB98}</ProjectGuid>
<RootNamespace>HelloHttp</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</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>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>$(SolutionDir)../bin/$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)../tmp/$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)../bin/$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)../tmp/$(Configuration)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>false</SDLCheck>
<AdditionalIncludeDirectories>..\Depends\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>false</SDLCheck>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Server.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?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="Server.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
\ No newline at end of file
#pragma execution_character_set("utf-8")
#include "Log.hpp"
#include "Config.hpp"
//#include "TcpSelectServer.hpp"
//#include "TcpEpollServer.hpp"
#include "TcpIocpServer.hpp" // 在windows系统下面首选iocp网络模型
using namespace doyou::io; // 命名空间
// 可使用服务模型(三选一):1-TcpSelectServer 2-TcpEpollServer 3-TcpIocpServer
class MyServer : public TcpIocpServer
{
public:
private:
};
int main(int argc, char* args[])
{
// 设置运行日志名称
Log::Instance().setLogPath("ServerLog", "w", false);
Config::Instance().Init(argc, args);
// 解析配置
const char* strIP = Config::Instance().getStr("strIP", "any");
uint16_t nPort = Config::Instance().getInt("nPort", 4567);
int nThread = Config::Instance().getInt("nThread", 1);
if (strcmp(strIP, "any") == 0)
{
strIP = nullptr;
}
MyServer server; // 在栈空间实例化对象
if (Config::Instance().hasKey("-ipv6")) {
CELLLog_Info("-ipv6");
server.InitSocket(AF_INET6);
}
else {
CELLLog_Info("-ipv4");
server.InitSocket();
}
server.Bind(strIP, nPort);
// 分析:服务器同一时刻能监听的最大客户端数量,可以提高服务器连接客户端的效率
server.Listen(SOMAXCONN); // 64
server.Start(nThread);
// 在主线程中等待用户输入命令
while (true)
{
char cmdBuf[256] = {};
scanf("%s", cmdBuf); // 等待用户输入,当前函数为阻塞函数
if (0 == strcmp(cmdBuf, "exit"))
{
server.Close();
break;
}
else {
CELLLog_Info("undefine cmd");
}
}
CELLLog_Info("exit.");
return 0;
}
......@@ -18,6 +18,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestDemo", "TestDemo\TestDe
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HelloCpp", "HelloCpp\HelloCpp.vcxproj", "{14149BAD-FE3D-45FB-8DB8-831E4198C72D}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HelloHttp", "HelloHttp\HelloHttp.vcxproj", "{3B0734CE-4E91-498A-9621-AB0385F7AB98}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
......@@ -44,6 +46,10 @@ Global
{14149BAD-FE3D-45FB-8DB8-831E4198C72D}.Debug|Win32.Build.0 = Debug|Win32
{14149BAD-FE3D-45FB-8DB8-831E4198C72D}.Release|Win32.ActiveCfg = Release|Win32
{14149BAD-FE3D-45FB-8DB8-831E4198C72D}.Release|Win32.Build.0 = Release|Win32
{3B0734CE-4E91-498A-9621-AB0385F7AB98}.Debug|Win32.ActiveCfg = Debug|Win32
{3B0734CE-4E91-498A-9621-AB0385F7AB98}.Debug|Win32.Build.0 = Debug|Win32
{3B0734CE-4E91-498A-9621-AB0385F7AB98}.Release|Win32.ActiveCfg = Release|Win32
{3B0734CE-4E91-498A-9621-AB0385F7AB98}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册