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 ...
随机推荐
- BeautifulSoup中的select方法
在写css时,标签名不加任何修饰,类名前加点,id名前加 #,我们可以用类似的方法来筛选元素,用到的方法是soup.select(),返回类型是list. (1).通过标签名查找 print(soup ...
- Qt5.3.2(VS2010)_调试_进入Qt源码
1.必须是 Debug模式 2. http://blog.csdn.net/mayenjoy/article/details/42535789 http://blog.csdn.net/goforwa ...
- 《剑指offer》第三_二题(不修改数组找出重复的数字)
// 面试题3(二):不修改数组找出重复的数字 // 题目:在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至 // 少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改 ...
- resin中关于url rewrite来传递jsessionid的问题
最近两天在项目中碰到,一个很奇怪的问题.同一个账号多次切换登录时,会出现这个账号的信息在session中找不到,虽然可以登录成功,但是之后这个用户信息好像没有保存到session中一样,或者是被改变了 ...
- windows批处理命令
前言 批处理文件(batch file)包含一系列 DOS命令,通常用于自动执行重复性任务.用户只需双击批处理文件便可执行任务,而无需重复输入相同指令.编写批处理文件非常简单,但难点在于确保一切按顺序 ...
- [转]UTF-8网页中的头部部分多出一行空白
最近做php开发,我自己的习惯都是用utf-8编码,有时候却在网页顶部多了一个空白行,甚至引起了式样的错乱,后来google到这么一篇文 章,彻底解决问题UTF-8的BOM问题通常情况下,使用Wind ...
- vs2010打包安装
[WinForm] VS2010发布.打包安装程序(超全超详细) 2017年02月17日 21:47:09 y13156556538 阅读数:16487更多 个人分类: C#winform 1. ...
- win10 自己DIY的arp绑定小脚本
@echo off&mode con cols=80 lines=22&title ARP_bind Tools setlocal enabledelayedexpansion rem ...
- 在 Confluence 6 中连 Jira 的问题解决
下面是可能会发生的一些错误信息.如果你的系统中出现了下面的一些提示,你应该调整你的日志错误级别到 WARN,然后查看具体的错误原因.请参考:Configuring Logging. error.jir ...
- 『Networkx』常用方法
这是一个用于分析'图'结构的包,由于我只是用到了浅显的可视化功能,所以这个介绍会对其使用浅尝辄止. 解决matplotlib中文字体缺失问题, from pylab import mpl mpl.rc ...