Pārlūkot izejas kodu

tcp通信完成;

scbc.sat2 5 gadi atpakaļ
vecāks
revīzija
b194793961

+ 2 - 1
VideoCapture/VideoCapture/Device.cpp

@@ -2,13 +2,14 @@
 #include "Device.h"
 #include "Device.h"
 
-std::map<int, CDevice> g_dmap;
+std::map<int, CDevice*> g_dmap;
 CDevice::CDevice()
 {
 }
 
 CDevice::~CDevice()
 {
+	m_obj.CloseSerialPort();
 }
 
 bool CDevice::Open()

+ 1 - 1
VideoCapture/VideoCapture/Device.h

@@ -35,6 +35,6 @@ public:
 	int m_bySpecialdevice;			// -- Modbus Rtu;Modbus Ascii;Modbus Tcp;Snmp;RS232;
 };
 
-extern std::map<int, CDevice> g_dmap;
+extern std::map<int, CDevice*> g_dmap;
 
 #endif

+ 60 - 40
VideoCapture/VideoCapture/IOCPModel.cpp

@@ -766,87 +766,93 @@ void CIOCPModel::_ShowMessage(const CString szFormat,...) const
 #endif
 }
 
-#define PAK_LEN sizeof(_PACKAGE_)
+#define PAK_LEN sizeof(ProHeader)
 void CIOCPModel::_RecvProcess(PER_SOCKET_CONTEXT* pSocketContext, PER_IO_CONTEXT* pIoContext)
 {
-	int plen = sizeof(_PACKAGE_);
 	// 鬼黨관庫;
 	ProHeader* phead = NULL;
 	if (pSocketContext->lastData.size() == 0)
 	{
-		phead = (ProHeader*)pIoContext->m_wsaBuf.buf;
-
-		// 供냥돨관;
-		if (phead->len == pIoContext->m_wsaBuf.len)
-		{
-			_DeviceProc(pSocketContext, (Package*)pIoContext->m_wsaBuf.buf);
-		}
-		// 鬼관;
-		else if (phead->len < pIoContext->m_wsaBuf.len)
+		if (PAK_LEN > pIoContext->m_Overlapped.InternalHigh)
 		{
-			pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_wsaBuf.len);
+			pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh);
 		}
-		// 낚관;
-		else if (phead->len > pIoContext->m_wsaBuf.len)
+		else
 		{
-			_DeviceProc(pSocketContext, (Package*)pIoContext->m_wsaBuf.buf);
-			pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + phead->len, pIoContext->m_wsaBuf.len - phead->len);
+			phead = (ProHeader*)pIoContext->m_wsaBuf.buf;
+
+			// 供냥돨관;
+			if (phead->len == pIoContext->m_Overlapped.InternalHigh)
+			{
+				_DeviceProc(pIoContext, (Package*)pIoContext->m_wsaBuf.buf);
+			}
+			// 鬼관;
+			else if (phead->len > pIoContext->m_Overlapped.InternalHigh)
+			{
+				pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh);
+			}
+			// 낚관;
+			else if (phead->len < pIoContext->m_Overlapped.InternalHigh)
+			{
+				_DeviceProc(pIoContext, (Package*)pIoContext->m_wsaBuf.buf);
+				pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + phead->len, pIoContext->m_Overlapped.InternalHigh - phead->len);
+			}
 		}
 	}
 	else
 	{
-		int lastlen = pIoContext->m_wsaBuf.len;
+		int lastlen = pIoContext->m_Overlapped.InternalHigh;
 		if (pSocketContext->lastData.size() >= PAK_LEN)
 		{
 			phead = (ProHeader*)pSocketContext->lastData.data();
-			if (phead->len <= pSocketContext->lastData.size() + pIoContext->m_wsaBuf.len)
+			if (phead->len <= pSocketContext->lastData.size() + pIoContext->m_Overlapped.InternalHigh)
 			{
-				lastlen = pSocketContext->lastData.size() + pIoContext->m_wsaBuf.len - phead->len;
-				pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, lastlen);
+				lastlen = pSocketContext->lastData.size() + pIoContext->m_Overlapped.InternalHigh - phead->len;
+				pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh - lastlen);
 				// 供憐관;
-				_DeviceProc(pSocketContext, (Package*)pSocketContext->lastData.data());
+				_DeviceProc(pIoContext, (Package*)pSocketContext->lastData.data());
 				// 假岱관;
 				pSocketContext->lastData.clear();
-				if (pIoContext->m_wsaBuf.len > lastlen)
-					pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + lastlen, pIoContext->m_wsaBuf.len - lastlen);
+				if (lastlen)
+					pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + pIoContext->m_Overlapped.InternalHigh - lastlen, lastlen);
 			}
 			else
 			{
-				pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_wsaBuf.len);
+				pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh);
 			}
 		}
 		else
 		{
 			int diflen = PAK_LEN - pSocketContext->lastData.size();
-			if ( diflen > pIoContext->m_wsaBuf.len )
+			if ( diflen > pIoContext->m_Overlapped.InternalHigh )
 			{
-				pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_wsaBuf.len);
+				pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, pIoContext->m_Overlapped.InternalHigh);
 			}
 			else
 			{
 				pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf, diflen);
 				phead = (ProHeader*)pSocketContext->lastData.data();
 				// 供憐관;
-				if ( phead->len == PAK_LEN + pIoContext->m_wsaBuf.len - diflen )
+				if ( phead->len == PAK_LEN + pIoContext->m_Overlapped.InternalHigh - diflen )
 				{
-					_DeviceProc(pSocketContext, (Package*)pIoContext->m_wsaBuf.buf);
+					_DeviceProc(pIoContext, (Package*)pIoContext->m_wsaBuf.buf);
 				}
 				// 鬼관;
-				else if ( phead->len > PAK_LEN + pIoContext->m_wsaBuf.len - diflen)
+				else if ( phead->len > PAK_LEN + pIoContext->m_Overlapped.InternalHigh - diflen)
 				{
-					pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + diflen, pIoContext->m_wsaBuf.len - diflen);
+					pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + diflen, pIoContext->m_Overlapped.InternalHigh - diflen);
 				}
 				// 낚관;
-				else if (phead->len < PAK_LEN + pIoContext->m_wsaBuf.len - diflen)
+				else if (phead->len < PAK_LEN + pIoContext->m_Overlapped.InternalHigh - diflen)
 				{
 					// 莉供냥관;
 					pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + diflen, phead->len - PAK_LEN);
-					_DeviceProc(pSocketContext, (Package*)pIoContext->m_wsaBuf.buf);
+					_DeviceProc(pIoContext, (Package*)pIoContext->m_wsaBuf.buf);
 					pSocketContext->lastData.clear();
-					int last = pIoContext->m_wsaBuf.len - diflen - phead->len + PAK_LEN;
+					int last = pIoContext->m_Overlapped.InternalHigh - diflen - phead->len + PAK_LEN;
 					if (last)
 					{						
-						pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + pIoContext->m_wsaBuf.len - last, last);
+						pSocketContext->lastData.append(pIoContext->m_wsaBuf.buf + pIoContext->m_Overlapped.InternalHigh - last, last);
 					}
 				}
 			}
@@ -854,14 +860,14 @@ void CIOCPModel::_RecvProcess(PER_SOCKET_CONTEXT* pSocketContext, PER_IO_CONTEXT
 	}
 }
 
-void CIOCPModel::_DeviceProc(PER_SOCKET_CONTEXT* pSocketContext, Package* pak)
+void CIOCPModel::_DeviceProc(PER_IO_CONTEXT* pIoContext, Package* pak)
 {
 	if ( pak->header.version != 0xAA )
 	{
 		return;
 	}
 
-	cJSON* pJson = cJSON_Parse((const char*)pak->buf);
+	cJSON* pJson = cJSON_Parse((const char*)&pak->buf);
 	if ( pJson == NULL )
 	{
 		return;
@@ -885,8 +891,8 @@ void CIOCPModel::_DeviceProc(PER_SOCKET_CONTEXT* pSocketContext, Package* pak)
 	}
 
 	// 蕨눔왯랙箇寧즈;
-	auto value = iter->second;
-	std::string readdata = value.SendCommond(reqj.device_cmd);
+	auto dev = iter->second;
+	std::string readdata = dev->SendCommond(reqj.device_cmd);
 
 	ResponseJson repj;
 	repj.device_id = reqj.device_id;
@@ -896,10 +902,24 @@ void CIOCPModel::_DeviceProc(PER_SOCKET_CONTEXT* pSocketContext, Package* pak)
 	pJson = cJSON_CreateObject();
 	cJSON_AddNumberToObject(pJson, "device_id", repj.device_id);
 	cJSON_AddStringToObject(pJson, "device_name", repj.device_name.c_str());
-	cJSON_AddNumberToObject(pJson, "device_cmd_result", repj.device_cmd_result.c_str());
+	cJSON_AddStringToObject(pJson, "device_cmd_result", repj.device_cmd_result.c_str());
 
 	char* pjdata = cJSON_Print(pJson);
-	send(pSocketContext->m_Socket, pjdata, strlen(pjdata), 0);
+
+	Package reponse_pak;
+	reponse_pak.header.version = 0xAB;
+	reponse_pak.header.len = strlen(pjdata) + PAK_LEN;
+	reponse_pak.buf = new byte[strlen(pjdata)];
+	memcpy(reponse_pak.buf, pjdata, strlen(pjdata));
+	if ( pjdata)
+		free(pjdata);
+	
+	int ret = send(pIoContext->m_sockAccept, (const char*)&reponse_pak, reponse_pak.header.len, 0);
+	delete[]reponse_pak.buf;
+	if ( ret == -1 )
+	{
+		DWORD dwEr = GetLastError();
+	}
 }
 
 /////////////////////////////////////////////////////////////////////

+ 5 - 1
VideoCapture/VideoCapture/IOCPModel.h

@@ -15,6 +15,9 @@
 // 默认IP地址
 #define DEFAULT_IP            _T("127.0.0.1")
 
+#pragma pack(push)
+#pragma pack(1)
+
 typedef struct _PRO_HEADER_
 {
 	byte	version;
@@ -27,6 +30,7 @@ typedef struct _PACKAGE_
 	ProHeader header;
 	byte	*buf; // json格式;
 }Package;
+#pragma pack(pop)
 
 typedef struct _REQUEST_JSON_
 {
@@ -261,7 +265,7 @@ protected:
 	void _ShowMessage( const CString szFormat,...) const;
 
 	void _RecvProcess(PER_SOCKET_CONTEXT* pSocketContext, PER_IO_CONTEXT* pIoContext);
-	void _DeviceProc(PER_SOCKET_CONTEXT* pSocketContext, Package *pak);
+	void _DeviceProc(PER_IO_CONTEXT* pIoContext, Package *pak);
 private:
 
 	HANDLE                       m_hShutdownEvent;              // 用来通知线程系统退出的事件,为了能够更好的退出线程

+ 16 - 17
VideoCapture/VideoCapture/VideoCapture.cpp

@@ -71,7 +71,7 @@ CVideoCaptureApp::CVideoCaptureApp() noexcept
 
 CVideoCaptureApp theApp;
 
-
+CIOCPModel g_iocp;
 // CVideoCaptureApp 初始化
 
 BOOL CVideoCaptureApp::InitInstance()
@@ -112,22 +112,21 @@ BOOL CVideoCaptureApp::InitInstance()
 	Global::GetConfig();
 	Global::WriteTextLog("程序启动");
 
-	CDevice dev;
-	dev.m_iBaudrate = 115200;
-	dev.m_iDatabit = 8;
-	dev.m_iPort = 3;
-	dev.m_iParitybit = 0;
-	dev.m_iStopbit = 1;
-	dev.m_iID = 1;
-	dev.m_strDeviceName = "Chroma22293";
-	dev.m_iDeviceType = 1;
-	dev.Open();
-
-	g_dmap.insert(std::pair<int, CDevice>(dev.m_iID,dev));
-
-	CIOCPModel iocp;
-	iocp.LoadSocketLib();
-	iocp.Start(5566);
+	CDevice *pdev = new CDevice();
+	pdev->m_iBaudrate = 115200;
+	pdev->m_iDatabit = 8;
+	pdev->m_iPort = 3;
+	pdev->m_iParitybit = 0;
+	pdev->m_iStopbit = 1;
+	pdev->m_iID = 1;
+	pdev->m_strDeviceName = "Chroma22293";
+	pdev->m_iDeviceType = 1;
+	pdev->Open();
+
+	g_dmap.insert(std::pair<int, CDevice*>(pdev->m_iID, pdev));
+
+	g_iocp.LoadSocketLib();
+	g_iocp.Start(5566);
 
 	// 初始化 OLE 库
 	if (!AfxOleInit())