博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
paho.mqtt.embedded-c MQTTPacket transport.c hacking
阅读量:6279 次
发布时间:2019-06-22

本文共 5551 字,大约阅读时间需要 18 分钟。

/******************************************************************************* *          paho.mqtt.embedded-c MQTTPacket transport.c hacking * 说明: *     跟一下paho.mqtt.embedded-c中的MQTT协议transport.c怎么使用。 *  *                                          2017-12-6 深圳 南山平山村 曾剑锋 ******************************************************************************//******************************************************************************* * Copyright (c) 2014 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v1.0 which accompany this distribution. * * The Eclipse Public License is available at *    http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at *   http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: *    Ian Craggs - initial API and implementation and/or initial documentation *    Sergio R. Caprile - "commonalization" from prior samples and/or documentation extension *******************************************************************************/#include 
#if !defined(SOCKET_ERROR) /** error in socket operation */ #define SOCKET_ERROR -1#endif#if defined(WIN32)/* default on Windows is 64 - increase to make Linux and Windows the same */#define FD_SETSIZE 1024#include
#include
#define MAXHOSTNAMELEN 256#define EAGAIN WSAEWOULDBLOCK#define EINTR WSAEINTR#define EINVAL WSAEINVAL#define EINPROGRESS WSAEINPROGRESS#define EWOULDBLOCK WSAEWOULDBLOCK#define ENOTCONN WSAENOTCONN#define ECONNRESET WSAECONNRESET#define ioctl ioctlsocket#define socklen_t int#else#define INVALID_SOCKET SOCKET_ERROR#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif#if defined(WIN32)#include
#else#include
#include
#endif/**This simple low-level implementation assumes a single connection for a single thread. Thus, a staticvariable is used for that connection.On other scenarios, the user must solve this by taking into account that the current implementation ofMQTTPacket_read() has a function pointer for a function call to get the data to a buffer, but no provisionsto know the caller or other indicator (the socket id): int (*getfn)(unsigned char*, int)*/static int mysock = INVALID_SOCKET;int transport_sendPacketBuffer(int sock, unsigned char* buf, int buflen){ int rc = 0; // 写入socket,buf为字节buffer, buflen为需要写入的字节长度 // rc为最终写入的字节长度 rc = write(sock, buf, buflen); return rc;}int transport_getdata(unsigned char* buf, int count){ // 读取socket,buf为字节buffer, count为需要读取的字节长度 // rc为最终读取的字节长度 int rc = recv(mysock, buf, count, 0); //printf("received %d bytes count %d\n", rc, (int)count); return rc;}int transport_getdatanb(void *sck, unsigned char* buf, int count){ int sock = *((int *)sck); /* sck: pointer to whatever the system may use to identify the transport */ /* this call will return after the timeout set on initialization if no bytes; in your system you will use whatever you use to get whichever outstanding bytes your socket equivalent has ready to be extracted right now, if any, or return immediately */ int rc = recv(sock, buf, count, 0); if (rc == -1) { /* check error conditions from your system here, and return -1 */ return 0; } return rc;}/**return >=0 for a socket descriptor, <0 for an error code@todo Basically moved from the sample without changes, should accomodate same usage for 'sock' for clarity,removing indirections*/// 兼容各种平台下依照addr、port打开socket的方法int transport_open(char* addr, int port){ // 获取mysock指针 int* sock = &mysock; // 传输方式为socket流 int type = SOCK_STREAM; struct sockaddr_in address;#if defined(AF_INET6) struct sockaddr_in6 address6;#endif int rc = -1;#if defined(WIN32) short family;#else sa_family_t family = AF_INET;#endif struct addrinfo *result = NULL; struct addrinfo hints = { 0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL}; static struct timeval tv; *sock = -1; if (addr[0] == '[') ++addr; if ((rc = getaddrinfo(addr, NULL, &hints, &result)) == 0) { struct addrinfo* res = result; /* prefer ip4 addresses */ while (res) { if (res->ai_family == AF_INET) { result = res; break; } res = res->ai_next; }#if defined(AF_INET6) if (result->ai_family == AF_INET6) { address6.sin6_port = htons(port); address6.sin6_family = family = AF_INET6; address6.sin6_addr = ((struct sockaddr_in6*)(result->ai_addr))->sin6_addr; } else#endif if (result->ai_family == AF_INET) { address.sin_port = htons(port); address.sin_family = family = AF_INET; address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr; } else rc = -1; freeaddrinfo(result); } if (rc == 0) { *sock = socket(family, type, 0); if (*sock != -1) {#if defined(NOSIGPIPE) int opt = 1; if (setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0) Log(TRACE_MIN, -1, "Could not set SO_NOSIGPIPE for socket %d", *sock);#endif if (family == AF_INET) rc = connect(*sock, (struct sockaddr*)&address, sizeof(address)); #if defined(AF_INET6) else rc = connect(*sock, (struct sockaddr*)&address6, sizeof(address6)); #endif } } if (mysock == INVALID_SOCKET) return rc; tv.tv_sec = 1; /* 1 second Timeout */ tv.tv_usec = 0; setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); return mysock;}// 主要就是关闭socket了int transport_close(int sock){int rc; rc = shutdown(sock, SHUT_WR); rc = recv(sock, NULL, (size_t)0, 0); rc = close(sock); return rc;}

 

转载地址:http://urnva.baihongyu.com/

你可能感兴趣的文章
HTML-color:rgb()-颜色渐进
查看>>
数据库实例: STOREBOOK > 表空间 > 编辑 表空间: UNDOTBS1
查看>>
Mcad学习笔记之异步编程(AsyncCallback委托,IAsyncResult接口,BeginInvoke方法,EndInvoke方法的使用小总结)...
查看>>
Javascript防冒泡事件与Event对象
查看>>
1.LinQ初体验 简单的示例(原创)
查看>>
Docker容器学习梳理--Volume数据卷使用
查看>>
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
查看>>
ios 人脸检测
查看>>
Android设计中的.9.png
查看>>
CentOS下Mycat+MySQL水平分割负载均衡配置记录
查看>>
Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(2)
查看>>
Java中的泛型
查看>>
linux wa%过高,iostat查看io状况
查看>>
使用 TreeView IE Web 控件
查看>>
我心中的核心组件(可插拔的AOP)~第六回 消息组件~续
查看>>
WPF编游戏系列 之二 图标效果
查看>>
pc/app 项目/功能设计
查看>>
Vue常用经典开源项目汇总参考-海量
查看>>
支付宝面试题大揭露
查看>>
managed domain与unmanaged domain
查看>>