Commit f670e8a7 authored by Andreas Schmidt's avatar Andreas Schmidt
Browse files

Receiving is now done in socket.c. Clean-up.

parent 624e56aa
#include <string.h>
#include <stdio.h>
#include "block.h"
#include "../defines.h"
#include "vdmcode/block_code.h"
#include "../util/common.h"
void clear_list(gf *const *src, uint8_t k) {
int j = 0;
......
//
// Created by andreas on 09.02.16.
//
#ifndef PRRT_CODING_PARAMS_H
#define PRRT_CODING_PARAMS_H
......@@ -17,6 +13,7 @@ typedef struct {
uint8_t n_p;
} PrrtCodingParams;
void PrrtCodingParams_init(PrrtCodingParams *cpar);
#endif //PRRT_CODING_PARAMS_H
void PrrtCodingParams_init(PrrtCodingParams *cpar);
......@@ -5,7 +5,6 @@
#include <netinet/in.h>
#include <assert.h>
#include "packet.h"
#include "coding_params.h"
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet);
......
......@@ -100,46 +100,59 @@ int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, size_t data_len) {
return 0;
}
PrrtPacket *PrrtSocket_recv(PrrtSocket *sock_ptr) {
uint32_t PrrtSocket_recv(PrrtSocket *sock_ptr, void* buf_ptr) {
unsigned char buffer[MAX_PAYLOAD_LENGTH];
// RECEIVE DATA
ssize_t n;
struct sockaddr_in remote;
socklen_t addrlen = sizeof(remote);
n = recvfrom(sock_ptr->fd_data, buffer, MAX_PAYLOAD_LENGTH, 0, (struct sockaddr *) &remote, &addrlen);
uint16_t remote_port = ntohs(remote.sin_port);
char *remote_host = inet_ntoa(remote.sin_addr);
PrrtPacket *packet = malloc(sizeof(PrrtPacket));
assert(packet != NULL);
PrrtPacket_decode(buffer, (uint16_t) n, packet);
// REPLY
struct sockaddr_in targetaddr;
memset((char*) &targetaddr, 0, sizeof(targetaddr));
targetaddr.sin_family = AF_INET;
targetaddr.sin_port = htons((uint16_t) (remote_port + 1));
struct hostent *hp;
hp = gethostbyname(remote_host);
memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
PrrtPacket * feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 7, 3, 50, 4, 6, 8, 9, 5, 1);
uint8_t buf[MAX_PAYLOAD_LENGTH];
uint32_t length = PrrtPacket_size(feedback_pkt_ptr);
while(1)
{
// RECEIVE DATA
ssize_t n;
struct sockaddr_in remote;
socklen_t addrlen = sizeof(remote);
n = recvfrom(sock_ptr->fd_data, buffer, MAX_PAYLOAD_LENGTH, 0, (struct sockaddr *) &remote, &addrlen);
uint16_t remote_port = ntohs(remote.sin_port);
char *remote_host = inet_ntoa(remote.sin_addr);
PrrtPacket *packet = malloc(sizeof(PrrtPacket));
assert(packet != NULL);
PrrtPacket_decode(buffer, (uint16_t) n, packet);
// REPLY
struct sockaddr_in targetaddr;
memset((char*) &targetaddr, 0, sizeof(targetaddr));
targetaddr.sin_family = AF_INET;
targetaddr.sin_port = htons((uint16_t) (remote_port + 1));
struct hostent *hp;
hp = gethostbyname(remote_host);
memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
PrrtPacket * feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 7, 3, 50, 4, 6, 8, 9, 5, 1);
uint8_t buf[MAX_PAYLOAD_LENGTH];
uint32_t length = PrrtPacket_size(feedback_pkt_ptr);
if(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) < 0) {
perror("BUF too small.");
} else {
if((sendto(sock_ptr->fd_feedback, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
perror("sendto failed");
return NULL;
}
}
if(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) < 0) {
perror("BUF too small.");
} else {
if((sendto(sock_ptr->fd_feedback, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
perror("sendto failed");
return NULL;
switch(PrrtPacket_type(packet)) {
case PACKET_TYPE_DATA:
memcpy(buf_ptr, packet->payload + PRRT_PACKET_DATA_HEADER_SIZE, packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
return packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE;
default:
PrrtPacket_print(packet);
break;
}
// TODO: enough packets received?
}
return packet;
return 0;
}
int PrrtSocket_close(PrrtSocket *sock_ptr) {
......
......@@ -38,7 +38,7 @@ int PrrtSocket_create(PrrtSocket *sock_ptr, uint16_t port, uint8_t is_sender);
int PrrtSocket_close(PrrtSocket *sock_ptr);
int PrrtSocket_connect(PrrtSocket *sock_ptr, char *host, uint16_t port);
int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, size_t data_len);
PrrtPacket *PrrtSocket_recv(PrrtSocket *sock_ptr);
uint32_t PrrtSocket_recv(PrrtSocket *sock_ptr, void* buf_ptr);
PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *sock_ptr, void *bufin, size_t length);
#endif // PRRT_SOCKET_H
......@@ -22,23 +22,10 @@ int main(int argc, char* const argv[]) {
}
while(1) {
pkt = PrrtSocket_recv(&sock);
if(pkt == NULL) {
perror("Error receiving data");
break;
} else {
char buffer[pkt->payload_len + 1 - PRRT_PACKET_DATA_HEADER_SIZE];
switch(PrrtPacket_type(pkt)) {
case PACKET_TYPE_DATA:
memcpy(buffer, pkt->payload + PRRT_PACKET_DATA_HEADER_SIZE, pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
buffer[pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE] = '\0';
printf("[B] %s\n", buffer);
break;
default:
PrrtPacket_print(pkt);
break;
}
}
unsigned char buffer[MAX_PAYLOAD_LENGTH];
int n = PrrtSocket_recv(&sock, buffer);
buffer[n] = '\0';
printf("[B (n: %d)] %s\n", n, buffer);
usleep(1);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment