Annotation of mqtt/src/client.c, revision 1.1.2.11
1.1.2.1 misho 1: #include "global.h"
2: #include "mqtt.h"
3:
4:
5: int
6: InitClient(void)
7: {
8: int sock;
9:
10: sock = socket(args->addr.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
11: if (sock == -1) {
12: printf("Error:: socket() #%d - %s\n", errno, strerror(errno));
13: return -1;
14: }
15: if (connect(sock, &args->addr.sa, args->addr.sa.sa_len) == -1) {
16: printf("Error:: connect() #%d - %s\n", errno, strerror(errno));
17: return -1;
18: }
19:
20: return sock;
21: }
1.1.2.2 misho 22:
23: int
1.1.2.9 misho 24: ConnectClient(int sock)
1.1.2.2 misho 25: {
1.1.2.4 misho 26: int siz = 0;
1.1.2.6 misho 27: struct pollfd pfd;
1.1.2.4 misho 28:
1.1.2.7 misho 29: siz = mqtt_msgCONNECT(args->msg, (char*) AIT_GET_STR(&args->ConnID), args->ka,
30: (char*) AIT_GET_STR(&args->User), (char*) AIT_GET_STR(&args->Pass),
31: (char*) args->Will.Topic.val.string, (char*) args->Will.Msg.val.string,
1.1.2.5 misho 32: !args->notClear, args->QoS, args->Retain);
1.1.2.4 misho 33: if (siz == -1) {
34: printf("Error:: msgCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
35: return -1;
36: }
37:
38: siz = send(sock, args->msg->msg_base, siz, 0);
39: if (siz == -1) {
40: printf("Error:: send() #%d - %s\n", errno, strerror(errno));
41: return -1;
42: } else
1.1.2.7 misho 43: ioVERBOSE(3) printf("Sended CONNECT %d bytes\n", siz);
1.1.2.4 misho 44:
1.1.2.6 misho 45: pfd.fd = sock;
1.1.2.9 misho 46: pfd.events = POLLIN | POLLPRI;
1.1.2.6 misho 47: switch (poll(&pfd, 1, args->ka * 1000)) {
48: case -1:
49: printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
50: return -1;
51: case 0:
1.1.2.7 misho 52: ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
1.1.2.6 misho 53: return -1;
54: }
55: if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
56: return -1;
57:
58: siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
59: if (siz == -1) {
60: printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
61: return -1;
62: } else
1.1.2.7 misho 63: ioVERBOSE(3) printf("Received %d bytes\n", siz);
1.1.2.6 misho 64:
65: return (u_char) mqtt_readCONNACK(args->msg);
1.1.2.2 misho 66: }
1.1.2.9 misho 67:
68: int
69: CloseClient(int sock)
70: {
71: int siz = 0;
72:
73: siz = mqtt_msgDISCONNECT(args->msg);
74: if (siz == -1) {
75: printf("Error:: msgDISCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
76: return -1;
77: }
78:
79: siz = send(sock, args->msg->msg_base, siz, 0);
80: if (siz == -1) {
81: printf("Error:: send() #%d - %s\n", errno, strerror(errno));
82: return -1;
83: } else
84: ioVERBOSE(3) printf("Sended DISCONNECT %d bytes\n", siz);
85:
1.1.2.11! misho 86: shutdown(sock, SHUT_RDWR);
1.1.2.9 misho 87: close(sock);
88: return siz;
89: }
90:
91: int
92: KAClient(int sock)
93: {
94: int siz = 0;
95: struct pollfd pfd;
96:
97: siz = mqtt_msgPINGREQ(args->msg);
98: if (siz == -1) {
99: printf("Error:: msgPINGREQ #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
100: return -1;
101: }
102:
103: siz = send(sock, args->msg->msg_base, siz, 0);
104: if (siz == -1) {
105: printf("Error:: send() #%d - %s\n", errno, strerror(errno));
106: return -1;
107: } else
108: ioVERBOSE(3) printf("Sended PINGREQ %d bytes\n", siz);
109:
110: pfd.fd = sock;
111: pfd.events = POLLIN | POLLPRI;
112: switch (poll(&pfd, 1, args->ka * 1000)) {
113: case -1:
114: printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
115: return -1;
116: case 0:
117: ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
118: return -1;
119: }
120: if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
121: return -1;
122:
123: siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
124: if (siz == -1) {
125: printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
126: return -1;
127: } else
128: ioVERBOSE(3) printf("Received %d bytes\n", siz);
129:
130: return (u_char) mqtt_readPINGRESP(args->msg);
131: }
1.1.2.10 misho 132:
1.1.2.11! misho 133: void *
1.1.2.10 misho 134: OpenFile(void)
135: {
1.1.2.11! misho 136: int f, siz = 0;
! 137: void *mem;
! 138:
1.1.2.10 misho 139: if (!args->isFile)
140: return NULL;
141:
1.1.2.11! misho 142: f = open(AIT_GET_STR(&args->Value), O_RDONLY);
! 143: if (f == -1) {
1.1.2.10 misho 144: printf("Error:: in open file #%d - %s\n", errno, strerror(errno));
145: return NULL;
146: }
1.1.2.11! misho 147: mem = mmap(NULL, siz, PROT_READ, MAP_PRIVATE, f, 0);
! 148: if (mem == MAP_FAILED) {
! 149: printf("Error:: in map file #%d - %s\n", errno, strerror(errno));
! 150: close(f);
! 151: return NULL;
! 152: } else
! 153: close(f);
1.1.2.10 misho 154:
1.1.2.11! misho 155: AIT_SET_PTR(&args->Value, mem, siz);
! 156: return mem;
1.1.2.10 misho 157: }
158:
159: void
160: CloseFile(void)
161: {
1.1.2.11! misho 162: if (args->isFile) {
! 163: munmap(AIT_GET_PTR(&args->Value), AIT_LEN(&args->Value));
! 164: AIT_FREE_VAL(&args->Value);
! 165: }
1.1.2.10 misho 166: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>