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