void DxpUpload(TcpConnItemData data)
{
        BinCodes codes;
        codes.bincodes=(uint8_t *)data.data;
        codes.length=data.length;

        IotPacketWithStatus *packet=TransformBinCodesToIotPacket(&codes);
        //write to file
        if(packet)
        {
                if(!packet->statuscode)
                {
                        //验证合法性 合法继续后续操作,不合法断开连接

                        //保存IMEI号和tcp连接的映射关系
                        WorkerThread * pthreadinfo=data.tcpconnitemptr.lock()->pthreadinfo;
                        pthreadinfo->ls_->InsertImeiThreadIntoMap((const char *)packet->packet->IMEI,data.tcpconnitemptr);

                        int log_fd=open("./test.log",O_RDWR|O_CREAT|O_APPEND,S_IRWXU);
                        if(log_fd!=-1)
                                WritePacketToFile(packet->packet,log_fd);
                        //test 返回结果
                        // std::shared_ptr<DataToThread> ret(new DataToThread());
                        // DataToThread ret(data.data,data.sessionid,data.length);

                        // pthreadinfo->PushResultIntoQueue(std::shared_ptr result);
                        // pthreadinfo->NotifyWorkerThread('r');

                        //向REDIS注册,失败写log

                        //向kafka写消息
                }
                else {
                        LOG(WARNING)<<"Decode failed and error num is "<<packet->statuscode;
                }
                FreeIotPacketOnHeap(packet);
        }
}
Пример #2
0
/*
FUNCTION: ReadFromPort
DATE: 11/28/2015
REVISIONS: v4 12/2/2015 - wait state added
DESIGNER: Allen & Dylan & Thomas
PROGRAMMER: Allen & Dylan
INTERFACE: static DWORD WINAPI ReadFromPort(LPVOID lpParam)
			LPVOID lpParam : parameter passed to the thread; intended to be a communications handle
RETURNS: 1

NOTES: This function is intended to be called on a separate thread.
		It has the event driven code to monitor the serial port for INCOMING communications.
		It will send messages to the main window (via WndProc) for OUTGOING communications.
*/
static DWORD WINAPI ReadFromPort(LPVOID lpParam) {
	OVERLAPPED overlapped = { 0 };
	HANDLE hnd = 0;
	BOOL fWaitingOnRead = FALSE;
	overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
	CHAR buffer[PACKETLENGTH] = "";
	DWORD dwEvent;
	SetCommMask(lpParam, EV_RXCHAR);

	while (true) {
		switch (state) {
		// Waiting for ENQ ACKnowledgment
		case WENQACK:
			if (!fWaitingOnRead) {
				if (!WaitCommEvent(lpParam, &dwEvent, &overlapped)) {
					if (GetLastError() == ERROR_IO_PENDING) {
						fWaitingOnRead = TRUE;
					}
				}
			}
			else {
				if (Wait(overlapped.hEvent, TIMEOUT)) {
					if (!ReadFile(lpParam, buffer, 1, NULL, &overlapped)) {
						WaitForSingleObject(overlapped.hEvent, TIMEOUT);
					};

					OutputDebugString("Finished reading in WENQACK\n");
					if (buffer[0] == ACK) {
						SendMessage(hMain, WM_COMMAND, ACK_REC, NULL);
						
					}
					else {
						OutputDebugString("Was not ACK, was ");
						OutputDebugString(buffer);
						OutputDebugString("\n");
						state = WAIT;
					}
					fWaitingOnRead = FALSE;
				}
				else {
					state = IDLE;
				}
			}
			break;
		// Waiting for ACKnowledgment for a packet
		case WACK:
			if (!fWaitingOnRead) {
				if (!WaitCommEvent(lpParam, &dwEvent, &overlapped)) {
					if (GetLastError() == ERROR_IO_PENDING) {
						fWaitingOnRead = TRUE;
					}
				}
			}
			else {
				if (Wait(overlapped.hEvent, TIMEOUT)) {
					ReadFile(lpParam, &buffer, 1, NULL, &overlapped);
					if (buffer[0] == ACK) {
						SendMessage(hMain, WM_COMMAND, ACK_REC, NULL);
						buffer[0] = 0;
					}
					fWaitingOnRead = FALSE;
				}
				else {
					state = WAIT;
				}
			}
			break;
		// a backoff state, can only become a receiver for some time
		case WAIT:
		// default state, can move to sending or receiving
		case IDLE:
			if (!fWaitingOnRead) {
				if ((state == WAIT || state == IDLE) && !WaitCommEvent(lpParam, &dwEvent, &overlapped)) {
					if (GetLastError() == ERROR_IO_PENDING) {
						fWaitingOnRead = TRUE;
					}

				}
			}
			else {
				if ((state == WAIT || state == IDLE) && Wait(overlapped.hEvent, TIMEOUT)) {
					ReadFile(lpParam, &buffer, 1, NULL, &overlapped);
					if (buffer[0] == ENQ) {
						startWriting();
						SendAck(lpParam);
						finishWriting();
						state = RECEIVING;
					}
					// this is what you call a hack to get around race conditions
					else if ((state == WENQACK || state == WACK) && buffer[0] == ACK) {
						SendMessage(hMain, WM_COMMAND, ACK_REC, NULL);
					}
		
					fWaitingOnRead = FALSE;
				}
				else {
					state = IDLE;
				}
			}
			break;
		// anticipating a packet
		case RECEIVING:
			if (!fWaitingOnRead) {
				if (!WaitCommEvent(lpParam, &dwEvent, &overlapped)) {
					if (GetLastError() == ERROR_IO_PENDING) {
						fWaitingOnRead = TRUE;
					}

				}
			}
			else {
				if (Wait(overlapped.hEvent, INFINITE)) {
					receiveBuffer = ReceivePacket(lpParam, overlapped);
					if (ErrorCheck(receiveBuffer) || 1) {
						//DoNonReadingStuff(receiveBuffer);
						Depacketize(receiveBuffer);
						startWriting();
						SendAck(lpParam);
						finishWriting();
						packetsReceived++;
						SetStatistics();
						// open the file for writing and reading

						hnd = CreateFile(FileName, FILE_APPEND_DATA | GENERIC_WRITE | GENERIC_READ, NULL, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
						startWriting();
						if (WritePacketToFile(receiveBuffer, hnd)) {
							UpdateWindowFromFile(hReceive, hnd);
						}
						finishWriting();
						CloseHandle(hnd);

						fWaitingOnRead = FALSE;
						state = IDLE;
						buffer[0] = 0;
						free(receiveBuffer);
					}
				}
				else {
					state = IDLE;
				}
			}
			break;
		}
		// if a packet is in the queue and the process is in idle, attempt to send
		if (state == IDLE && packetBuffer[currentPacket][0] != 0) {
			SendMessage(hMain, WM_COMMAND, ASN_ENQ, NULL);
		}
	}
	return 1;
}
Пример #3
0
	void WritePacketToFile(const std::string &out_file_name,
		MLB::SocketIo::PacketFormat packet_format) const
	{
		MLB::SocketIo::PacketFileWriter out_file(out_file_name, packet_format);
		WritePacketToFile(out_file);
	}