109 lines
2.2 KiB
C
109 lines
2.2 KiB
C
|
|
/**
|
|||
|
|
* socket 服务端
|
|||
|
|
* */
|
|||
|
|
|
|||
|
|
#ifndef SOCKETENGINE_H
|
|||
|
|
#define SOCKETENGINE_H
|
|||
|
|
|
|||
|
|
#include "AppCommon.h"
|
|||
|
|
#include "MyYaml.h"
|
|||
|
|
#include "myutils.h"
|
|||
|
|
#include "EngineBase.h"
|
|||
|
|
#include "EngineFactory.h"
|
|||
|
|
|
|||
|
|
#include <iostream>
|
|||
|
|
#include <unistd.h>
|
|||
|
|
#include <sys/socket.h>
|
|||
|
|
#include <sys/types.h>
|
|||
|
|
#include <netinet/in.h>
|
|||
|
|
#include <thread>
|
|||
|
|
#include <signal.h>
|
|||
|
|
#include <arpa/inet.h>
|
|||
|
|
|
|||
|
|
#include <unistd.h>
|
|||
|
|
#include <sys/select.h> // select
|
|||
|
|
#include <sys/ioctl.h>
|
|||
|
|
#include <sys/time.h>
|
|||
|
|
|
|||
|
|
|
|||
|
|
using namespace ai_matrix;
|
|||
|
|
|
|||
|
|
class SocketEngine : public ai_matrix::EngineBase
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
SocketEngine();
|
|||
|
|
~SocketEngine();
|
|||
|
|
|
|||
|
|
APP_ERROR Init() override;
|
|||
|
|
APP_ERROR DeInit() override;
|
|||
|
|
APP_ERROR Process() override;
|
|||
|
|
|
|||
|
|
bool Socket_(int port);
|
|||
|
|
|
|||
|
|
bool Bind();
|
|||
|
|
|
|||
|
|
bool Listen(int queue_len = 20);
|
|||
|
|
|
|||
|
|
bool Accept();
|
|||
|
|
|
|||
|
|
bool Run();
|
|||
|
|
|
|||
|
|
bool Recv(int nums);
|
|||
|
|
|
|||
|
|
static void *sendInfo(void* arg);
|
|||
|
|
// 发送消息给指定IP的客户端
|
|||
|
|
static bool SendClient(std::map<int, std::pair<std::string, int> > mmap, const std::string ip, const std::string message);
|
|||
|
|
// 发送消息给所有的客户端
|
|||
|
|
static bool SendAllClient(std::map<int, std::pair<std::string, int> > mmap, const std::string message);
|
|||
|
|
|
|||
|
|
static void* heart_handler(void* arg);
|
|||
|
|
//解析命令
|
|||
|
|
static bool getOrder(const std::string &recv, Json::Value &order);
|
|||
|
|
|
|||
|
|
//组织接口反馈内容
|
|||
|
|
std::string getFeedBack(const std::string poundNo, const std::string type, const std::string info);
|
|||
|
|
|
|||
|
|
std::string HexToStr(const std::string &str);
|
|||
|
|
|
|||
|
|
std::string StrToHex(const std::string &str);
|
|||
|
|
|
|||
|
|
private:
|
|||
|
|
|
|||
|
|
int channel_;
|
|||
|
|
|
|||
|
|
std::string rfidIP_;
|
|||
|
|
std::string strPort0_;
|
|||
|
|
|
|||
|
|
// soket开启状态
|
|||
|
|
int socketOpenType_;
|
|||
|
|
// 端口
|
|||
|
|
int socketPort_;
|
|||
|
|
// 队列最大长度
|
|||
|
|
int socketQueueLen_;
|
|||
|
|
|
|||
|
|
const int QUEUE_SIZE = 20;// socket缓冲队列大小
|
|||
|
|
|
|||
|
|
struct sockaddr_in server_addr;
|
|||
|
|
|
|||
|
|
socklen_t server_addr_len;
|
|||
|
|
|
|||
|
|
int listen_fd; // 监听的fd
|
|||
|
|
|
|||
|
|
int max_fd; // 最大的fd
|
|||
|
|
|
|||
|
|
fd_set master_set; // 所有fd集合,包括监听fd和客户端fd
|
|||
|
|
|
|||
|
|
fd_set working_set; // 工作集合
|
|||
|
|
|
|||
|
|
struct timeval timeout;
|
|||
|
|
|
|||
|
|
std::string Heart_Beat;
|
|||
|
|
|
|||
|
|
std::map<int, std::pair<std::string, int> > mmap; // 记录连接的客户端fd--><ip, count>
|
|||
|
|
|
|||
|
|
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
ENGINE_REGIST(SocketEngine)
|
|||
|
|
|
|||
|
|
#endif
|