1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include "SocketUtils.h"
- #include "Stream.h"
- int sendAll(int socket, Stream* out)
- {
- char* data = out->data;
- int left = out->index;
- streamUpdateIndex(out);
- while(left > 0)
- {
- int bytes = send(socket, data, left, MSG_NOSIGNAL);
- if(bytes == -1)
- {
- perror("Cannot send data");
- return -1;
- }
- left -= bytes;
- data += bytes;
- }
- return out->index;
- }
- int receiveAll(int socket, Stream* in)
- {
- int size = recv(socket, in->data, in->size, 0);
- if(size > 0)
- {
- int dataLength = streamGetIndex(in);
- if(size == dataLength)
- {
- in->size = dataLength;
- return dataLength;
- }
- streamEnsureCapacity(in, dataLength);
- in->size = dataLength;
-
- char* data = in->data + size;
- int left = dataLength - size;
- while(left > 0)
- {
- size = recv(socket, data, left, 0);
- if(size > 0)
- {
- data += size;
- left -= size;
- }
- else if(size == 0)
- {
- printf("Remote Socket closed\n");
- return -1;
- }
- else
- {
- perror("recv error");
- return -1;
- }
- }
- return dataLength;
- }
- else if(size == 0)
- {
- printf("Remote Socket closed\n");
- return 0;
- }
- else
- {
- perror("recv error");
- return -1;
- }
- return -1;
- }
|