Mercurial > pidgin
comparison libfaim/aim_rxqueue.c @ 2:68b230f8da5f
[gaim-migrate @ 11]
A few more commits :)
committer: Tailor Script <tailor@pidgin.im>
| author | Rob Flynn <gaim@robflynn.com> |
|---|---|
| date | Thu, 23 Mar 2000 03:16:06 +0000 |
| parents | |
| children | 6ced2f1c8b24 |
comparison
equal
deleted
inserted
replaced
| 1:2846a03bda67 | 2:68b230f8da5f |
|---|---|
| 1 /* | |
| 2 aim_rxqueue.c | |
| 3 | |
| 4 This file contains the management routines for the receive | |
| 5 (incoming packet) queue. The actual packet handlers are in | |
| 6 aim_rxhandlers.c. | |
| 7 | |
| 8 */ | |
| 9 | |
| 10 #include "aim.h" | |
| 11 | |
| 12 /* | |
| 13 This is a modified read() to make SURE we get the number | |
| 14 of bytes we are told to, otherwise block. | |
| 15 */ | |
| 16 int Read(int fd, u_char *buf, int len) | |
| 17 { | |
| 18 int i = 0; | |
| 19 int j = 0; | |
| 20 | |
| 21 while ((i < len) && (!(i < 0))) | |
| 22 { | |
| 23 j = read(fd, &(buf[i]), len-i); | |
| 24 if ( (j < 0) && (errno != EAGAIN)) | |
| 25 return -errno; /* fail */ | |
| 26 else | |
| 27 i += j; /* success, continue */ | |
| 28 } | |
| 29 #if 0 | |
| 30 printf("\nRead Block: (%d/%04x)\n", len, len); | |
| 31 printf("\t"); | |
| 32 for (j = 0; j < len; j++) | |
| 33 { | |
| 34 if (j % 8 == 0) | |
| 35 printf("\n\t"); | |
| 36 if (buf[j] >= ' ' && buf[j] < 127) | |
| 37 printf("%c=%02x ",buf[j], buf[j]); | |
| 38 else | |
| 39 printf("0x%02x ", buf[j]); | |
| 40 } | |
| 41 printf("\n\n"); | |
| 42 #endif | |
| 43 return i; | |
| 44 } | |
| 45 | |
| 46 /* | |
| 47 struct command_struct * | |
| 48 get_generic( | |
| 49 struct connection_info struct *, | |
| 50 struct command_struct * | |
| 51 ) | |
| 52 | |
| 53 Grab as many command sequences as we can off the socket, and enqueue | |
| 54 each command in the incoming event queue in a seperate struct. | |
| 55 | |
| 56 */ | |
| 57 int aim_get_command(void) | |
| 58 { | |
| 59 int i, readgood, j, isav, err; | |
| 60 int s; | |
| 61 fd_set fds; | |
| 62 struct timeval tv; | |
| 63 char generic[6]; | |
| 64 struct command_rx_struct *workingStruct = NULL; | |
| 65 struct command_rx_struct *workingPtr = NULL; | |
| 66 struct aim_conn_t *conn = NULL; | |
| 67 #if debug > 0 | |
| 68 printf("Reading generic/unknown response..."); | |
| 69 #endif | |
| 70 | |
| 71 | |
| 72 /* dont wait at all (ie, never call this unless something is there) */ | |
| 73 tv.tv_sec = 0; | |
| 74 tv.tv_usec = 0; | |
| 75 conn = aim_select(&tv); | |
| 76 | |
| 77 if (conn==NULL) | |
| 78 return 0; /* nothing waiting */ | |
| 79 | |
| 80 s = conn->fd; | |
| 81 | |
| 82 FD_ZERO(&fds); | |
| 83 FD_SET(s, &fds); | |
| 84 tv.tv_sec = 0; /* wait, but only for 10us */ | |
| 85 tv.tv_usec = 10; | |
| 86 | |
| 87 generic[0] = 0x00; | |
| 88 | |
| 89 readgood = 0; | |
| 90 i = 0; | |
| 91 j = 0; | |
| 92 /* read first 6 bytes (the FLAP header only) off the socket */ | |
| 93 while ( (select(s+1, &fds, NULL, NULL, &tv) == 1) && (i < 6)) | |
| 94 { | |
| 95 if ((err = Read(s, &(generic[i]), 1)) < 0) | |
| 96 { | |
| 97 /* error is probably not recoverable...(must be a pessimistic day) */ | |
| 98 aim_conn_close(conn); | |
| 99 return err; | |
| 100 } | |
| 101 | |
| 102 if (readgood == 0) | |
| 103 { | |
| 104 if (generic[i] == 0x2a) | |
| 105 { | |
| 106 readgood = 1; | |
| 107 #if debug > 1 | |
| 108 printf("%x ", generic[i]); | |
| 109 fflush(stdout); | |
| 110 #endif | |
| 111 i++; | |
| 112 } | |
| 113 else | |
| 114 { | |
| 115 #if debug > 1 | |
| 116 printf("skipping 0x%d ", generic[i]); | |
| 117 fflush(stdout); | |
| 118 #endif | |
| 119 j++; | |
| 120 } | |
| 121 } | |
| 122 else | |
| 123 { | |
| 124 #if debug > 1 | |
| 125 printf("%x ", generic[i]); | |
| 126 #endif | |
| 127 i++; | |
| 128 } | |
| 129 FD_ZERO(&fds); | |
| 130 FD_SET(s, &fds); | |
| 131 tv.tv_sec= 2; | |
| 132 tv.tv_usec= 2; | |
| 133 } | |
| 134 | |
| 135 if (generic[0] != 0x2a) | |
| 136 { | |
| 137 /* this really shouldn't happen, since the main loop | |
| 138 select() should protect us from entering this function | |
| 139 without data waiting */ | |
| 140 printf("Bad incoming data!"); | |
| 141 return -1; | |
| 142 } | |
| 143 | |
| 144 isav = i; | |
| 145 | |
| 146 /* allocate a new struct */ | |
| 147 workingStruct = (struct command_rx_struct *) malloc(sizeof(struct command_rx_struct)); | |
| 148 workingStruct->lock = 1; /* lock the struct */ | |
| 149 | |
| 150 /* store type -- byte 2 */ | |
| 151 workingStruct->type = (char) generic[1]; | |
| 152 | |
| 153 /* store seqnum -- bytes 3 and 4 */ | |
| 154 workingStruct->seqnum = ( (( (unsigned int) generic[2]) & 0xFF) << 8); | |
| 155 workingStruct->seqnum += ( (unsigned int) generic[3]) & 0xFF; | |
| 156 | |
| 157 /* store commandlen -- bytes 5 and 6 */ | |
| 158 workingStruct->commandlen = ( (( (unsigned int) generic[4]) & 0xFF ) << 8); | |
| 159 workingStruct->commandlen += ( (unsigned int) generic[5]) & 0xFF; | |
| 160 | |
| 161 printf("%d\n", workingStruct->commandlen); | |
| 162 | |
| 163 /* malloc for data portion */ | |
| 164 workingStruct->data = (char *) malloc(workingStruct->commandlen); | |
| 165 | |
| 166 /* read the data portion of the packet */ | |
| 167 i = Read(s, workingStruct->data, workingStruct->commandlen); | |
| 168 if (i < 0) | |
| 169 { | |
| 170 aim_conn_close(conn); | |
| 171 return i; | |
| 172 } | |
| 173 | |
| 174 #if debug > 0 | |
| 175 printf(" done. (%db+%db read, %db skipped)\n", isav, i, j); | |
| 176 #endif | |
| 177 | |
| 178 workingStruct->conn = conn; | |
| 179 | |
| 180 workingStruct->next = NULL; /* this will always be at the bottom */ | |
| 181 workingStruct->lock = 0; /* unlock */ | |
| 182 | |
| 183 /* enqueue this packet */ | |
| 184 if (aim_queue_incoming == NULL) | |
| 185 aim_queue_incoming = workingStruct; | |
| 186 else | |
| 187 { | |
| 188 workingPtr = aim_queue_incoming; | |
| 189 while (workingPtr->next != NULL) | |
| 190 workingPtr = workingPtr->next; | |
| 191 workingPtr->next = workingStruct; | |
| 192 } | |
| 193 | |
| 194 return 0; | |
| 195 } | |
| 196 | |
| 197 /* | |
| 198 purge_rxqueue() | |
| 199 | |
| 200 This is just what it sounds. It purges the receive (rx) queue of | |
| 201 all handled commands. This is normally called from inside | |
| 202 aim_rxdispatch() after it's processed all the commands in the queue. | |
| 203 | |
| 204 */ | |
| 205 struct command_rx_struct *aim_purge_rxqueue(struct command_rx_struct *queue) | |
| 206 { | |
| 207 int i = 0; | |
| 208 struct command_rx_struct *workingPtr = NULL; | |
| 209 struct command_rx_struct *workingPtr2 = NULL; | |
| 210 | |
| 211 workingPtr = queue; | |
| 212 if (queue == NULL) | |
| 213 { | |
| 214 return queue; | |
| 215 } | |
| 216 else if (queue->next == NULL) | |
| 217 { | |
| 218 if (queue->handled == 1) | |
| 219 { | |
| 220 workingPtr2 = queue; | |
| 221 queue = NULL; | |
| 222 free(workingPtr2->data); | |
| 223 free(workingPtr2); | |
| 224 } | |
| 225 return queue; | |
| 226 } | |
| 227 else | |
| 228 { | |
| 229 for (i = 0; workingPtr != NULL; i++) | |
| 230 { | |
| 231 if (workingPtr->next->handled == 1) | |
| 232 { | |
| 233 /* save struct */ | |
| 234 workingPtr2 = workingPtr->next; | |
| 235 /* dequeue */ | |
| 236 workingPtr->next = workingPtr2->next; | |
| 237 /* free */ | |
| 238 free(workingPtr2->data); | |
| 239 free(workingPtr2); | |
| 240 } | |
| 241 | |
| 242 workingPtr = workingPtr->next; | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 return queue; | |
| 247 } |
