1. 增加设置sdk服务器demo
This commit is contained in:
parent
ba96705e86
commit
1cd69fbc1a
@ -54,6 +54,14 @@ if(ENABLE_DEMO)
|
|||||||
)
|
)
|
||||||
target_link_libraries(tcpServer PRIVATE HDSDK)
|
target_link_libraries(tcpServer PRIVATE HDSDK)
|
||||||
|
|
||||||
|
add_executable(setTcpServer demo/setTcpServer.cpp)
|
||||||
|
target_include_directories(setTcpServer PUBLIC
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/Protocol
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/SDK
|
||||||
|
)
|
||||||
|
target_link_libraries(setTcpServer PRIVATE HDSDK)
|
||||||
|
|
||||||
add_executable(time demo/time.cpp)
|
add_executable(time demo/time.cpp)
|
||||||
target_include_directories(time PUBLIC
|
target_include_directories(time PUBLIC
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
|||||||
103
demo/setTcpServer.cpp
Normal file
103
demo/setTcpServer.cpp
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "../SDK/SDKInfo.h"
|
||||||
|
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
struct Proto
|
||||||
|
{
|
||||||
|
IHDProtocol proto;
|
||||||
|
ISDKInfo info;
|
||||||
|
};
|
||||||
|
|
||||||
|
int IsRead = 0;
|
||||||
|
|
||||||
|
|
||||||
|
static bool SendData(const char *data, int len, void *userData) {
|
||||||
|
int fd = (int)(intptr_t)userData;
|
||||||
|
return write(fd, data, len) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ReadXml(const char *xml, int len, int errorCode, void *userData) {
|
||||||
|
Proto *info = (Proto *)userData;
|
||||||
|
++IsRead;
|
||||||
|
if (errorCode != 0) {
|
||||||
|
printf("error code[%d]\n", errorCode);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "ip:" << GetTcpServerInfoIp(info->info) << "\n"
|
||||||
|
<< "port:" << GetTcpServerInfoPort(info->info) << "\n"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (argc < 4) {
|
||||||
|
printf("a.out ip setIp setPort\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (fd < 0) {
|
||||||
|
perror("create socket faild");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_in addrSrv;
|
||||||
|
memset(&addrSrv, 0, sizeof(addrSrv));
|
||||||
|
addrSrv.sin_family = AF_INET;
|
||||||
|
addrSrv.sin_port = htons(10001);
|
||||||
|
addrSrv.sin_addr.s_addr = inet_addr(argv[1]);
|
||||||
|
|
||||||
|
int result = connect(fd, reinterpret_cast<struct sockaddr *>(&addrSrv), sizeof(addrSrv));
|
||||||
|
if (result < 0) {
|
||||||
|
close(fd);
|
||||||
|
perror("Not connect client");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
ISDKInfo sdkInfo = CreateSDKInfo();
|
||||||
|
IHDProtocol proto = CreateProtocol();
|
||||||
|
Proto a;
|
||||||
|
a.proto = proto;
|
||||||
|
a.info = sdkInfo;
|
||||||
|
SetProtocolFunc(proto, kSetSendFunc, (void *)SendData);
|
||||||
|
SetProtocolFunc(proto, kSetSendFuncData, (void *)(intptr_t)(fd));
|
||||||
|
SetProtocolFunc(proto, kSetReadXml, (void *)ReadXml);
|
||||||
|
SetProtocolFunc(proto, kSetReadXmlData, (void *)&a);
|
||||||
|
|
||||||
|
RunProtocol(proto);
|
||||||
|
|
||||||
|
SetTcpServerInfo(sdkInfo, argv[2], atoi(argv[3]));
|
||||||
|
UpdateItem(proto, sdkInfo, kSetTcpServerInfo);
|
||||||
|
|
||||||
|
char buff[1024];
|
||||||
|
for (;;) {
|
||||||
|
int len = read(fd, buff, sizeof(buff));
|
||||||
|
if (len <= 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateReadData(proto, buff, len);
|
||||||
|
if (IsRead > 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FreeSDKInfo(sdkInfo);
|
||||||
|
FreeProtocol(proto);
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user