Return to client.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / mqtt / src |
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:
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
64: ioVERBOSE(3) printf("Received %d bytes\n", siz);
65:
66: return (u_char) mqtt_readCONNACK(args->msg);
67: }
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:
1.2.2.2 misho 80: siz = send(sock, args->msg->msg_base, siz, MSG_NOSIGNAL);
1.2 misho 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:
87: shutdown(sock, SHUT_RDWR);
88: close(sock);
89: return siz;
90: }
91:
92: void *
93: OpenFile(void)
94: {
95: int f, siz = 0;
96: void *mem;
97:
98: if (!args->isFile)
99: return NULL;
100:
101: f = open(AIT_GET_STR(&args->Value), O_RDONLY);
102: if (f == -1) {
103: printf("Error:: in open file #%d - %s\n", errno, strerror(errno));
104: return NULL;
105: }
106: mem = mmap(NULL, siz, PROT_READ, MAP_PRIVATE, f, 0);
107: if (mem == MAP_FAILED) {
108: printf("Error:: in map file #%d - %s\n", errno, strerror(errno));
109: close(f);
110: return NULL;
111: } else
112: close(f);
113:
114: AIT_SET_PTR(&args->Value, mem, siz);
115: return mem;
116: }
117:
118: void
119: CloseFile(void)
120: {
121: if (args->isFile) {
122: munmap(AIT_GET_PTR(&args->Value), AIT_LEN(&args->Value));
123: AIT_FREE_VAL(&args->Value);
124: }
125: }
126:
127: inline int
128: SendTo(int sock, int siz)
129: {
1.2.2.2 misho 130: siz = send(sock, args->msg->msg_base, siz, MSG_NOSIGNAL);
1.2 misho 131: if (siz == -1) {
132: printf("Error:: send() #%d - %s\n", errno, strerror(errno));
133: return -1;
134: } else
1.2.2.1 misho 135: ioVERBOSE(3) printf("Sended %d bytes\n", siz);
1.2 misho 136:
137: return siz;
138: }
139:
140: inline int
141: RecvFrom(int sock)
142: {
143: struct pollfd pfd;
144: int siz = 0;
145:
146: pfd.fd = sock;
147: pfd.events = POLLIN | POLLPRI;
148: do {
149: switch (poll(&pfd, 1, args->ka * 1000)) {
150: case -1:
151: printf("Error:: poll() #%d - %s\n", errno, strerror(errno));
152: return -1;
153: case 0:
154: ioVERBOSE(3) printf("Timeout reached (%d) ...\n", args->ka * 1000);
1.2.2.3 ! misho 155: if (mqtt_KeepAlive(sock, args->ka, 1) == -1)
1.2 misho 156: return -1;
157: continue;
158: }
159: if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))
160: return -1;
161: } while (0);
162:
163: siz = recv(sock, args->msg->msg_base, args->msg->msg_len, 0);
164: if (siz == -1) {
165: printf("Error:: recv() #%d - %s\n", errno, strerror(errno));
166: return -1;
167: } else
168: ioVERBOSE(3) printf("Received %d bytes\n", siz);
169:
170: return siz;
171: }