Newer
Older
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fstream>
#include <map>
#include <TCP_Protocol.h>
using namespace inaf::oasbo::ConnectionProtocols;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
TCPProtocol::TCPProtocol(std::string hs, int prt){
this->host = hs;
this->port = prt;
fd_sock = 0;
}
/*
* Implementation of the BaseSocket connect for the TCP protocol
*
* @return file_descriptor [-1 error]
*/
int TCPProtocol::connectToClient() {
int sockfd, newSocket;
struct sockaddr_in servaddr;
int addrlen = sizeof(servaddr);
// Creating socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == 0){
perror("socket failed");
return -1;
}
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons( port );
if (bind(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr))<0){
perror("bind failed");
return -1;
}
std::cout << "Waiting for connection" << std::endl;
if (listen(sockfd, 1) < 0){
perror("listen error");
return -1;
}
if ((newSocket = accept(sockfd, (struct sockaddr *)&servaddr,(socklen_t*)&addrlen))<0){
perror("accept error");
return -1;
}
std::cout << "Connected" << std::endl;
this->fd_sock = newSocket;
return newSocket;
}
int TCPProtocol::connectToServer(){
int sock = 0;
struct sockaddr_in cli_addr;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("\n Socket creation error \n");
return -1;
}
cli_addr.sin_family = AF_INET;
cli_addr.sin_port = htons(port);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, host.c_str(), &cli_addr.sin_addr)<=0){
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&cli_addr, sizeof(cli_addr)) < 0){
printf("\nConnection Failed \n");
return -1;
}
std::cout << "Connected" << std::endl;
this->fd_sock = sock;
return sock;
}
/*
* Implementation of the BaseSocket readPacket for the TCP protocol
*
* @param pack: address of the packet object
* @param buff: buffer to write the read bytes
*
* @return number of bytes received [-1 error, 0 done]
*/
template<typename Value, template<typename> typename Container>
int TCPProtocol::rcvPacketFromCli(PacketLib::BasePacket<Container,Value> &pack){
size_t headerSize = pack.getHeaderSize();
uint8_t *buff = new uint8_t[pack.getMaxPacketSize()];
int rec = recv(this->fd_sock, &buff[0], headerSize, MSG_WAITALL);
pack.copyToBinaryPointer(buff, headerSize);
if(rec == headerSize){
rec+= recv(this->fd_sock, &buff[headerSize], pack.getPayloadSize() + pack.getTailSize(), MSG_WAITALL);
pack.copyToBinaryPointer(&buff[headerSize], pack.getPayloadSize() + pack.getTailSize(), headerSize);
template<typename Value, template<typename> typename Container>
int TCPProtocol::sendPacketToSrv(PacketLib::BasePacket<Container,Value> &pack){
int val = send(fd_sock, pack.getBinaryPointer(), pack.getHeaderSize()+pack.getPayloadSize()+pack.getTailSize(), MSG_CONFIRM);