paho.mqtt.embedded-c MQTTPacket transport.c hacking
/*******************************************************************************
* 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 <sys/types.h> #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 <winsock2.h>
#include <ws2tcpip.h>
#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 <sys/socket.h>
#include <sys/param.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#endif #if defined(WIN32)
#include <Iphlpapi.h>
#else
#include <sys/ioctl.h>
#include <net/if.h>
#endif /**
This simple low-level implementation assumes a single connection for a single thread. Thus, a static
variable is used for that connection.
On other scenarios, the user must solve this by taking into account that the current implementation of
MQTTPacket_read() has a function pointer for a function call to get the data to a buffer, but no provisions
to 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 = ;
// 写入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, );
//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, );
if (rc == -) {
/* check error conditions from your system here, and return -1 */
return ;
}
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 = -;
#if defined(WIN32)
short family;
#else
sa_family_t family = AF_INET;
#endif
struct addrinfo *result = NULL;
struct addrinfo hints = {, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, , NULL, NULL, NULL};
static struct timeval tv; *sock = -;
if (addr[] == '[')
++addr; if ((rc = getaddrinfo(addr, NULL, &hints, &result)) == )
{
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 = -; freeaddrinfo(result);
} if (rc == )
{
*sock = socket(family, type, );
if (*sock != -)
{
#if defined(NOSIGPIPE)
int opt = ; if (setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != )
Log(TRACE_MIN, -, "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 second Timeout */
tv.tv_usec = ;
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), );
rc = close(sock); return rc;
}
paho.mqtt.embedded-c MQTTPacket transport.c hacking的更多相关文章
- paho.mqtt.embedded-c MQTTPacket pub0sub1.c hacking
/******************************************************************************* * paho.mqtt.embedde ...
- Paho - MQTT C Cient的实现
来自我的CSDN博客 在前几天,我大致了解了一下Paho C项目,并对其的一些内容进行了翻译.俗话说,光说不练假把戏,今天就给大家讲一下使用Paho的客户端库文件实现MQTT C Client的过 ...
- [3] MQTT,mosquitto,Eclipse Paho---怎样使用 Eclipse Paho MQTT工具来发送订阅MQTT消息?
在上两节,笔者主要介绍了 MQTT,mosquitto,Eclipse Paho的基本概念已经怎样安装mosquitto. 在这个章节我们就来看看怎样用 Eclipse Paho MQTT工具来发送接 ...
- vc2015编译paho.mqtt.c-1.1.0
vc2015打开“\paho.mqtt.c-1.1.0\Windows Build\Paho C MQTT APIs.sln” 将文件“\paho.mqtt.c-1.1.0\src\VersionIn ...
- paho.mqtt.c打印日志
mqtt中自身就带有日志系统Log.h和Log.c,这些日志文件是在客户端调用MQTTClient_create函数是初始化的,MQTTClient_create源码如下: int MQTTClien ...
- Eclipse Paho MQTT Utility
下载地址: https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho. ...
- 3.MQTT paho
一.概述 遥测传输 (MQTT) 是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放.简单.轻量.易于实现.这些特点使它适用于受限环境.例如,但不仅限于此: 网络代价昂贵,带宽低.不可靠. 在 ...
- Paho -物联网 MQTT C Cient的实现和详解
概述 在文章Paho - MQTT C Cient的实现中,我介绍了如何使用Paho开源项目创建MQTTClient_pulish客户端.但只是简单的介绍了使用方法,而且客户端的结果与之前介绍的并 ...
- MQTT和paho(一)
参考链接:http://blog.csdn.net/yangzl2008/article/details/8861069 一.mqtt 1.简单介绍 http://mqtt.org/software ...
随机推荐
- Jmeter 爬Boss中注册和登录
图文验证码生成部分: Get请求到 https://m.zhipin.com/captcha/?randomKey=9dHY2lyVpxg0wFQRmzJqE5bpL4iRkhhR 每次访问新生成一个 ...
- Cocoapods 报警告Automatically assigning platform ios with version 9.0 on target....
Automatically assigning platform iOS with version 9.0 on target 你的工程名称 because no platform was speci ...
- 递归C++
递归C++ 一.递归简介 自己调用自己 二.递归写法 2.1 写法介绍 先写出问题的递推公式 递归部分的边界条件就是递推公式中的边界条件 递归部分的主体部分就是递推公式中的主体部分 2.2 实例 (1 ...
- 微信公众号菜单添加小程序,miniprogram,pagepath参数详解,php开发公众号
随着微信小程序功能的开发, 已经可以跟公众号打通了, 主要有两种方式: 1) 在公众号文章中插入小程序 2) 在公众号菜单中添加小程序 第一种方式, 子恒老师在前面的课程已经详细介绍过, 今天来讲第二 ...
- new JSONObject(str)无法解析 报错:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject 解析服务器返回的Jso ...
- Java数据类型转换(自动转换和强制转换)
数据类型的转换,分为自动转换和强制转换.自动转换是程序在执行过程中“悄然”进行的转换,不需要用户提前声明,一般是从位数低的类型向位数高的类型转换;强制类型转换则必须在代码中声明,转换顺序不受限制. 自 ...
- 1月24日 ruby基础3部分 Numeric, Array已学。
<div style="background:lightblue"> 第12章 数值类 12.1 数值的构成 Numeric-> Integer-> Fix ...
- HttpServletResponse输出的中文乱码
HttpServletResponse输出有两种格式,一种是字符流,一种是字节流. 1.字符流 // 这句话的意思,是让浏览器用utf8来解析返回的数据,即设置客户端解析的编码 response.se ...
- 微信access_token全局缓存,处理过期
//PHP创建access_token.json文件,将access_token 和 生成时间expires 保存在其中, //{"access_token":"xxxx ...
- js实现个链表吧
存储多个元素,最常用的数据结构是数组.但是数组有个一缺点,从数组中添加或移除项的成本很高,因为需要移动元素.链表也可以存储有序的元素集合,但是和数组不同,链表中的元素在内存中不是连续放置的.每个元素存 ...