Commit 3176f1c8 by NitefullWind

1. First commit.

parents
Debug/
Release/
ipch/
*.sdf
*.suo
*.user
*.filters
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FreemudAPI", "FreemudAPI\FreemudAPI.vcxproj", "{18C1BF83-598C-42F3-B8AD-B770B3F405C0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{18C1BF83-598C-42F3-B8AD-B770B3F405C0}.Debug|Win32.ActiveCfg = Debug|Win32
{18C1BF83-598C-42F3-B8AD-B770B3F405C0}.Debug|Win32.Build.0 = Debug|Win32
{18C1BF83-598C-42F3-B8AD-B770B3F405C0}.Release|Win32.ActiveCfg = Release|Win32
{18C1BF83-598C-42F3-B8AD-B770B3F405C0}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
// FreemudAPI.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include <stdio.h>
#include <errno.h>
#include <winsock2.h>
#include <Windows.h>
#pragma comment(lib, "ws2_32.lib")
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define SERVERIP "127.0.0.1"
#define SERVERPORT 34952
#define FM_ERROR -1;
#define MAXLINE 4096
#define FM_ERROR_TIMEOUT "{\"statusCode\": 104, \"msg\": \"本地Socket通讯超时.\"}"
#define Log(_Format, ...) {printf("Function:%s, Line:%d: ", __FUNCTION__, __LINE__); printf(_Format, __VA_ARGS__); printf("\n");}
static int read_cnt;
static char *read_ptr;
static char read_buf[MAXLINE];
static int my_read(int fd, char *ptr)
{
if (read_cnt <= 0) {
again:
if ((read_cnt = recv(fd, read_buf, sizeof(read_buf), 0)) < 0) {
if (errno == EINTR)
goto again;
return (-1);
}
else if (read_cnt == 0)
return (0);
read_ptr = read_buf;
}
read_cnt--;
*ptr = *read_ptr++;
return(1);
}
int readline(int fd, void *vptr, size_t maxlen)
{
int n, rc;
char c, *ptr;
ptr = (char*)vptr;
for (n = 1; n<maxlen; n++) {
if ((rc = my_read(fd, &c)) == 1) {
*ptr++ = c;
if (c == '\n')
break; /* newline is stored, like fgets() */
}
else if (rc == 0) {
*ptr = 0;
return(n - 1); /* EOF, n-1 */
}
else {
return (-1); /* error, errno set by read() */
}
}
*ptr = 0; /* null terminatr like fgets() */
return (n);
}
extern "C" {
__declspec(dllexport) int __stdcall FMGetResponse(const char* req, char* rsp)
{
WSADATA wsa;
SOCKET _socket;
struct sockaddr_in _server;
bool isOk = true;
Log("Initialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
Log("Error. Startup error: %d", WSAGetLastError());
isOk = false;
}
if(isOk && (_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
Log("Could not create socket: %d", WSAGetLastError());
isOk = false;
}
_server.sin_addr.S_un.S_addr = (ULONG)inet_addr(SERVERIP);
_server.sin_family = AF_INET;
_server.sin_port = htons(SERVERPORT);
// 设置超时时间
const int sendTimeOut = 1 * 60 * 1000;
const int recvTimeOut = 5 * 60 * 1000;
setsockopt(_socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&sendTimeOut, sizeof(int));
setsockopt(_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&recvTimeOut, sizeof(int));
if (isOk && connect(_socket, (struct sockaddr *)&_server, sizeof(_server)) < 0)
{
Log("Error. connect error: %d", WSAGetLastError());
isOk = false;
}
if (isOk && send(_socket, req, strlen(req), 0) < 0)
{
Log("Error. Send error: %d", WSAGetLastError());
isOk = false;
}
if (isOk) {
Log("Send data: %s", req);
}
int recv_size = SOCKET_ERROR;
if (isOk && (recv_size = recv(_socket, rsp, 40960, 0)) == SOCKET_ERROR)
//if (isOk && (recv_size = readline(_socket, rsp, 40960)) == SOCKET_ERROR)
//if (isOk && (recv_size = my_read(_socket, rsp)) == SOCKET_ERROR)
{
Log("Error. Recv error: %d", WSAGetLastError());
isOk = false;
}
if (isOk) {
Log("Recv size: %d, Recv data: %s", recv_size, rsp);
} else if (WSAGetLastError() == 10060) {
strcpy(rsp, FM_ERROR_TIMEOUT);
}
closesocket(_socket);
WSACleanup();
return recv_size;
}
}
\ No newline at end of file
LIBRARY
EXPORTS
FMGetResponse
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.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>{18C1BF83-598C-42F3-B8AD-B770B3F405C0}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>FreemudAPI</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</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'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;FREEMUDAPI_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>FreemudAPI.def</ModuleDefinitionFile>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;FREEMUDAPI_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<ModuleDefinitionFile>FreemudAPI.def</ModuleDefinitionFile>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<None Include="FreemudAPI.def" />
<None Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsManaged>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
</PrecompiledHeader>
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsManaged>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
</PrecompiledHeader>
</ClCompile>
<ClCompile Include="FreemudAPI.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="FreemudAPI.rc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
========================================================================
动态链接库:FreemudAPI 项目概述
========================================================================
应用程序向导已为您创建了此 FreemudAPI DLL。
本文件概要介绍组成 FreemudAPI 应用程序的每个文件的内容。
FreemudAPI.vcxproj
这是使用应用程序向导生成的 VC++ 项目的主项目文件,
其中包含生成该文件的 Visual C++
的版本信息,以及有关使用应用程序向导选择的平台、配置和项目功能的信息。
FreemudAPI.vcxproj.filters
这是使用“应用程序向导”生成的 VC++ 项目筛选器文件。
它包含有关项目文件与筛选器之间的关联信息。 在 IDE
中,通过这种关联,在特定节点下以分组形式显示具有相似扩展名的文件。
例如,“.cpp”文件与“源文件”筛选器关联。
FreemudAPI.cpp
这是主 DLL 源文件。
此 DLL 在创建时不导出任何符号。 因此,在生成此 DLL 时
生成时不会产生 .lib 文件。 如果希望此项目
成为其他某个项目的项目依赖项,则需要
添加代码以从 DLL 导出某些符号,
以便产生一个导出库,或者,也可以在项目“属性页”对话框中的
“链接器”文件夹中,将“常规”属性页上的
“忽略输入库”属性设置为“是”。
/////////////////////////////////////////////////////////////////////////////
其他标准文件:
StdAfx.h,StdAfx.cpp
这些文件用于生成名为 FreemudAPI.pch 的预编译头 (PCH) 文件和
名为 StdAfx.obj 的预编译类型文件。
/////////////////////////////////////////////////////////////////////////////
其他注释:
应用程序向导使用“TODO:”注释来指示应添加或自定义的源代码部分。
/////////////////////////////////////////////////////////////////////////////
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by FreemudAPI.rc
// ¶һĬֵ
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
// stdafx.cpp : 只包括标准包含文件的源文件
// FreemudAPI.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
// TODO: 在 STDAFX.H 中
// 引用任何所需的附加头文件,而不是在此文件中引用
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的信息
// Windows 头文件:
#include <windows.h>
// TODO: 在此处引用程序需要的其他头文件
#pragma once
// 包括 SDKDDKVer.h 将定义可用的最高版本的 Windows 平台。
// 如果要为以前的 Windows 平台生成应用程序,请包括 WinSDKVer.h,并将
// WIN32_WINNT 宏设置为要支持的平台,然后再包括 SDKDDKVer.h。
#include <SDKDDKVer.h>
# FreemudAPI
## 导出函数
```c++
int FMGetResponse(const char *req, char *rsp)
```
#### 参数
- req: 请求Json字符
- rsp: 响应Json字符
#### 返回
返回收到的数据长度。如果为负数则代表出错。
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment