本文整理汇总了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 ( );
}
示例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;
}
示例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");
//.........这里部分代码省略.........