当前位置: 首页>>代码示例>>C++>>正文


C++ Tcp::send方法代码示例

本文整理汇总了C++中Tcp::send方法的典型用法代码示例。如果您正苦于以下问题:C++ Tcp::send方法的具体用法?C++ Tcp::send怎么用?C++ Tcp::send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Tcp的用法示例。


在下文中一共展示了Tcp::send方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: dispatch

    bool dispatch(Msg& msg)
    {
        PT_BEGIN (  );
        while(true)
        {
            PT_YIELD_UNTIL( msg.is(_tcp,SIG_RXD) || msg.is(_usb,SIG_RXD) );
            if ( msg.is(_tcp,SIG_RXD))
            {
                MqttIn* mqttIn = (MqttIn*)msg.data;
                Str str(256);
                str << "MQTT TCP->USB:";
                mqttIn->toString(str);
                logger.info()<< str;
                logger.flush();
                usb.send(*mqttIn->getBytes());
            }
            else if ( msg.is(_usb,SIG_RXD))
            {
                MqttIn* mqttIn = (MqttIn*)msg.data;
                Str str(256);
                str << "MQTT USB->TCP:";
                mqttIn->toString(str);
                logger.info()<< str;
                logger.flush();

                if ( _tcp->isConnected() )
                {
                    if ( mqttIn->type() == MQTT_MSG_CONNECT ) // simulate a reply
                    {
                        MqttOut m(10);
                        m.ConnAck(0);
                        //                           uint8_t CONNACK[]={0x20,0x02,0x00,0x00};
                        logger.info()<< "CONNACK virtual,already tcp connected";
                        logger.flush();
                        usb.send(m);
                    }
                    else
                    {
                        tcp.send(*mqttIn->getBytes());
                    }
                }
                else
                {
                    if ( mqttIn->type() == MQTT_MSG_CONNECT )
                    {
                        tcp.connect();
                        tcp.send(*mqttIn->getBytes());
                    }
                    else
                    {
                        logger.info()<< "dropped packet, not connected.";
                        logger.flush();
                        usb.disconnect();
                    }
                }
            }
        }
        PT_END (  );
    }
开发者ID:vortex314,项目名称:projects,代码行数:59,代码来源:main.cpp

示例2: sendCb

void Tcp::sendCb(void *arg) {
	sends_cb++;
	struct espconn* pconn = (struct espconn*) arg;
	Tcp *pTcp = findTcp(pconn);
	if ( pTcp) {
		pTcp->_state = READY;
		pTcp->send();
//		pTcp->right().tell(pTcp->self(), REPLY(TXD), 0);
	} else {
		LOGF("Tcp not found");
	}
	return;
}
开发者ID:vortex314,项目名称:ea_actor,代码行数:13,代码来源:Tcp.cpp

示例3: fopen

HTTPResponse *HTTPClient::httpPostFile(std::string URL, std::string file) {
	ssize_t fileSize = Utils::fileSize(file.c_str());
	if (fileSize == -1) return NULL;

	FILE *fp = fopen(file.c_str(), "r");
	if (fp == NULL) {
		// Woops!
		return NULL;
	}
	uint8_t *buf = (uint8_t *) malloc(fileSize);
	memset(buf, 0, (size_t) fileSize);
	fread(buf, fileSize, 1, fp);
	fclose(fp);

	HTTPResponse *resp = new HTTPResponse();

	Tcp client;
	char serverName[100];
	unsigned short serverPort = 80;
	size_t pathOffset = hostFromURL(URL.c_str(), serverName, &serverPort);

	if (client.connectTo(std::string(serverName), serverPort)) {
		Log::d("Connect to server OK");
		char linebuf[1024];

		snprintf(linebuf, 1024, "POST %s HTTP/1.1", URL.c_str() + pathOffset);
		client.println(linebuf);

		if (serverPort != 80) {
			snprintf(linebuf, 1024, "Host: %s:%i", serverName, serverPort);
		} else {
			snprintf(linebuf, 1024, "Host: %s", serverName);
		}
		client.println(linebuf);

		client.println("Content-Type: application/octet-stream");
		client.println("Connection: close");

		std::string contentLength = "Content-Length: " + Utils::toString((int) fileSize);
		client.println(contentLength.c_str());
		client.println("");

		client.send(buf, fileSize);

		Log::d("HTTP Request to %s sent", URL.c_str());

		// Request sent, wait for reply
		unsigned long reqTime = Utils::millis();
		while (!client.available() && (Utils::millis() - reqTime < HTTP_RESPONSE_TIMEOUT_VALUE)) {
#ifndef NOWATCHDOG
			Watchdog::heartBeat();
#endif
		}

		if (client.available()) {
			char rBuffer[300 + 1];
			memset(rBuffer, 0, 300 + 1);
			int s = getLine(client, (uint8_t *) rBuffer, 300);

			Log::d("buffer response[%i]: %s", s, rBuffer);

			if (strncmp(rBuffer, "HTTP/1.", 7) == 0) {
				resp->error = HTTP_OK;
				resp->responseCode = getResponseCode(rBuffer);

				// Read headers
				do {
					s = getLine(client, (uint8_t *) rBuffer, 300);
					if (s > 0 && strlen(rBuffer) != 0) {
						char *dppos = strchr(rBuffer, ':');
						*dppos = 0;
						if (*(dppos + 1) == ' ') {
							dppos++;
						}
						dppos++;
						resp->headers[std::string(rBuffer)] = std::string(dppos);
					}
				} while (s > 0 && strlen(rBuffer) != 0);

				resp->body = NULL;
				if (resp->headers.count("Content-Length") == 1) {
					size_t bodySize = (size_t) atol(resp->headers["Content-Length"].c_str());
					resp->body = (uint8_t *) malloc(bodySize + 1);
					memset(resp->body, 0, bodySize + 1);
					client.readall(resp->body, bodySize);
				}
			} else {
				Log::e("HTTP malformed reply");
				resp->error = HTTP_MALFORMED_REPLY;
			}
		} else {
			Log::e("HTTP request timeout");
			resp->error = HTTP_REQUEST_TIMEOUT;
		}
	} else {
		Log::e("HTTP connection timeout");
		resp->error = HTTP_CONNECTION_TIMEOUT;
	}
	free(buf);
	Log::d("Stopping tcp client");
//.........这里部分代码省略.........
开发者ID:sapienzaapps,项目名称:galileo-terremoti,代码行数:101,代码来源:HTTPClient.cpp


注:本文中的Tcp::send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。