/*******************************************************************************
* 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的更多相关文章

  1. paho.mqtt.embedded-c MQTTPacket pub0sub1.c hacking

    /******************************************************************************* * paho.mqtt.embedde ...

  2. Paho - MQTT C Cient的实现

    来自我的CSDN博客   在前几天,我大致了解了一下Paho C项目,并对其的一些内容进行了翻译.俗话说,光说不练假把戏,今天就给大家讲一下使用Paho的客户端库文件实现MQTT C Client的过 ...

  3. [3] MQTT,mosquitto,Eclipse Paho---怎样使用 Eclipse Paho MQTT工具来发送订阅MQTT消息?

    在上两节,笔者主要介绍了 MQTT,mosquitto,Eclipse Paho的基本概念已经怎样安装mosquitto. 在这个章节我们就来看看怎样用 Eclipse Paho MQTT工具来发送接 ...

  4. 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 ...

  5. paho.mqtt.c打印日志

    mqtt中自身就带有日志系统Log.h和Log.c,这些日志文件是在客户端调用MQTTClient_create函数是初始化的,MQTTClient_create源码如下: int MQTTClien ...

  6. Eclipse Paho MQTT Utility

    下载地址: https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho. ...

  7. 3.MQTT paho

    一.概述 遥测传输 (MQTT) 是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放.简单.轻量.易于实现.这些特点使它适用于受限环境.例如,但不仅限于此: 网络代价昂贵,带宽低.不可靠. 在 ...

  8. Paho -物联网 MQTT C Cient的实现和详解

    概述   在文章Paho - MQTT C Cient的实现中,我介绍了如何使用Paho开源项目创建MQTTClient_pulish客户端.但只是简单的介绍了使用方法,而且客户端的结果与之前介绍的并 ...

  9. MQTT和paho(一)

    参考链接:http://blog.csdn.net/yangzl2008/article/details/8861069 一.mqtt 1.简单介绍 http://mqtt.org/software ...

随机推荐

  1. java基础深入解析基本类型

    一.基本类型的简介 基本类型的两条准则: Java中,如果对整数不指定类型,默认时int类型,对小数不指定类型,默认是double类型. 基本类型由小到大,可以自动转换,但是由大到小,则需要强制类型转 ...

  2. Codeforces 483B - Friends and Presents(二分+容斥)

    483B - Friends and Presents 思路:这个博客写的不错:http://www.cnblogs.com/windysai/p/4058235.html 代码: #include& ...

  3. CAS-自旋锁

    自旋锁 自旋锁(spinlock):是指当一个线程在获取锁的时候,如果锁已经被其它线程获取,那么该线程将循环等待,然后不断的判断锁是否能够被成功获取,直到获取到锁才会退出循环.  获取锁的线程一直处于 ...

  4. Python 爬虫-Scrapy框架基本使用

    2017-08-01  22:39:50 一.Scrapy爬虫的基本命令 Scrapy是为持续运行设计的专业爬虫框架,提供操作的Scrapy命令行. Scrapy命令行格式 Scrapy常用命令 采用 ...

  5. MyBatis中的@Mapper注解 @Mappe与@MapperScan关系

    从mybatis3.4.0开始加入了@Mapper注解,目的就是为了不再写mapper映射文件 现在项目中的配置 public interface DemoMapper{ int deleteByPr ...

  6. 负载均衡中使用 Redis 实现共享 Session

    最近在研究Web架构方面的知识,包括数据库读写分离,Redis缓存和队列,集群,以及负载均衡(LVS),今天就来先学习下我在负载均衡中遇到的问题,那就是session共享的问题. 一.负载均衡 负载均 ...

  7. SQL2005 安装问题

    1. 单击“开始”,依次指向“程序”.“Microsoft SQL Server 2005”和“配置工具”,然后单击“SQL Server 外围应用配置器”. 2. 在“SQL Server 2005 ...

  8. Is your JDeveloper Slow? - It shouldn't be!(转)

    我的Jdeveloper随便点一个AM,code的显示速度和手指的反应速度跟不上,真的是着急,忍了好久,找到以下教程. 经过考虑,仅仅只是在jdev.conf(jdevbin/jdev/bin/jde ...

  9. 小议常被忽略的a标签:visited属性的特殊用法

    CSS1/CSS2对于a定义了4个伪类, :link  a标签未访问时的样式 :active  a标签mousedown时的样式 :hover  a标签mouseover时的样式 :visited  ...

  10. ubuntu下没有Language Support

    sudo apt-get installlanguage-selector-gnome