You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.5 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*--- 基于2Dcode 通信协议 的功能实现类---
通讯使用TCP
本地作为服务端
端口: 8687
消息结束符: #
字段间隔符: ,
数据分隔符: ;
指令支持:
1. GetStatus# 获取当前状态已连接回复1
2. GetCode_1-1# 获取单个数据 1-1 行列
3. GetAllCode# 获取所有数据
异常回复:-1#
*PS: 每个类对象仅允许一个客户端连接
*PS: 所有对外接口返回0标识成功
*/
#pragma once
#ifndef _CMM_MAPPING_H
#define _CMM_MAPPING_H
#include "sysapi.h"
#include <vector>
using std::vector;
enum MapStatus
{//连接状态
Map_NoInit = 0, //未初始化、创建socket失败
Map_Connected, //已连接
Map_Disconnect, //客户端未连接
};
struct TMappingData
{//2d code数据
int col;
int row;
int len; //长度
char* pszdata;
};
//CMapping
class CManage;
class CMapping
{
public:
CMapping();
virtual ~CMapping();
int GetStatus() { return _status; }
int Init(const char* pszserver = NULL); //初始化服务端传入NULL则绑定任意IP自动绑定端口8687
int Deinit(); //关闭连接,反初始化
int Clear(); //清空当前缓冲区+发送区
int ReplaceSending(); //将缓冲区替换发送区
//设置缓冲区数据 -- 根据行列替换或增加 格式2,5,1,ZD1912031456,1
//不需要增加消息结束符和数据分割符, pszdata不可以传NULL
int SetData(int row, int col, const char* pszdata);
int AnalysisRegister(char* buff);
int AnslyStatus(char* buff);
int AnslysisStart(char* buff);
protected:
void StartRun(); //任务开始
void StopRun();
void Close(); //关闭连接
void ChkStatus(); //检查状态
void RecvMsg();
void SendMsg(const char* psz); //发送消息
void ParseMsg(const char* psz, int len); //解析并处理消息
//获取所有数据传入NULL指针返回new字符串
void GetAllSendData(char* &psz);
//获取单个数据传入NULL指针返回new字符串
void GetSendData(int col, int row, char* &psz);
void Release(vector<TMappingData>& vec); //释放数据
int FindEnd(const char* pszmsg); //查找结束符
//线程函数
int Process();
static int WINAPI ThreadFun(void* param);
private:
int _skt; //socket句柄
int _status; //当前状态
bool _brun; //线程运行标识
Handle _hThread; //线程句柄
Handle _hSection; //临界区
Handle _hSecSend; //发送区临界区
int _len; //当前消息长度
char _msg[MAX_BUF_LEN]; //单条消息缓冲区
char _client[MAX_NAME_LEN]; //客户端IP地址
ushort _port;
vector<TMappingData> _vecSend; //发送区
vector<TMappingData> _vecSet; //缓存区
};
#endif