Annotation of mqtt/src/client.c, revision 1.2.2.4
1.2 misho 1: #include "global.h"
2: #include "mqtt.h"
3: #include "client.h"
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: }
23:
24: int
25: ConnectClient(int sock)
26: {
27: int siz = 0;
28: struct pollfd pfd;
29:
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,
33: !args->notClear, args->QoS, args->Retain);
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
44: ioVERBOSE(3) printf("Sended CONNECT %d bytes\n", siz);
45:
46: pfd.fd = sock;
47: pfd.events = POLLIN | POLLPRI;
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:
53: ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
54: return -1;
55: }
56: if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
57: return -1;
58:
1.2.2.4 ! misho 59: memset(args->msg->msg_base, 0, args->msg->msg_len);
1.2 misho 60: siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
61: if (siz == -1) {
62: printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
63: return -1;
64: } else
65: ioVERBOSE(3) printf("Received %d bytes\n", siz);
66:
67: return (u_char) mqtt_readCONNACK(args->msg);
68: }
69:
70: int
71: CloseClient(int sock)
72: {
73: int siz = 0;
74:
75: siz = mqtt_msgDISCONNECT(args->msg);
76: if (siz == -1) {
77: printf("Error:: msgDISCONNECT #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
78: return -1;
79: }
80:
1.2.2.2 misho 81: siz = send(sock, args->msg->msg_base, siz, MSG_NOSIGNAL);
1.2 misho 82: if (siz == -1) {
83: printf("Error:: send() #%d - %s\n", errno, strerror(errno));
84: return -1;
85: } else
86: ioVERBOSE(3) printf("Sended DISCONNECT %d bytes\n", siz);
87:
88: shutdown(sock, SHUT_RDWR);
89: close(sock);
90: return siz;
91: }
92:
93: void *
94: OpenFile(void)
95: {
96: int f, siz = 0;
97: void *mem;
98:
99: if (!args->isFile)
100: return NULL;
101:
102: f = open(AIT_GET_STR(&args->Value), O_RDONLY);
103: if (f == -1) {
104: printf("Error:: in open file #%d - %s\n", errno, strerror(errno));
105: return NULL;
106: }
107: mem = mmap(NULL, siz, PROT_READ, MAP_PRIVATE, f, 0);
108: if (mem == MAP_FAILED) {
109: printf("Error:: in map file #%d - %s\n", errno, strerror(errno));
110: close(f);
111: return NULL;
112: } else
113: close(f);
114:
115: AIT_SET_PTR(&args->Value, mem, siz);
116: return mem;
117: }
118:
119: void
120: CloseFile(void)
121: {
122: if (args->isFile) {
123: munmap(AIT_GET_PTR(&args->Value), AIT_LEN(&args->Value));
124: AIT_FREE_VAL(&args->Value);
125: }
126: }
127:
128: inline int
129: SendTo(int sock, int siz)
130: {
1.2.2.2 misho 131: siz = send(sock, args->msg->msg_base, siz, MSG_NOSIGNAL);
1.2 misho 132: if (siz == -1) {
133: printf("Error:: send() #%d - %s\n", errno, strerror(errno));
134: return -1;
135: } else
1.2.2.1 misho 136: ioVERBOSE(3) printf("Sended %d bytes\n", siz);
1.2 misho 137:
138: return siz;
139: }
140:
141: inline int
142: RecvFrom(int sock)
143: {
144: struct pollfd pfd;
145: int siz = 0;
146:
1.2.2.4 ! misho 147: memset(args->msg->msg_base, 0, args->msg->msg_len);
! 148:
1.2 misho 149: pfd.fd = sock;
150: pfd.events = POLLIN | POLLPRI;
151: do {
152: switch (poll(&pfd, 1, args->ka * 1000)) {
153: case -1:
154: printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
155: return -1;
156: case 0:
157: ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
1.2.2.3 misho 158: if (mqtt_KeepAlive(sock, args->ka, 1) == -1)
1.2 misho 159: return -1;
160: continue;
161: }
162: if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
163: return -1;
164: } while (0);
165:
166: siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
167: if (siz == -1) {
168: printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
169: return -1;
170: } else
171: ioVERBOSE(3) printf("Received %d bytes\n", siz);
172:
173: return siz;
174: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>