-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPStream.cpp
More file actions
137 lines (120 loc) · 3.01 KB
/
TCPStream.cpp
File metadata and controls
137 lines (120 loc) · 3.01 KB
1
2
3
4
5
6
7
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//i dont know how many of these are relevant
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <cstring>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include "TCPStream.h"
#include "Packet.h"
#include "PacketTypes.h"
#define maxPacketSize 1000
TCPStream::TCPStream(int sockfd)
{
fcntl(sockfd, F_SETFL, O_NONBLOCK);
this->sockfd = sockfd;
}
TCPStream::~TCPStream()
{
Packet_Closed* p = new Packet_Closed();
write((Packet*)p);
::close(sockfd);
}
TCPStream* TCPStream::connectTo(const char* ip, int port)
{
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
fprintf(stderr, "ERROR opening socket\n");
return nullptr;
}
server = gethostbyname(ip);
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
return nullptr;
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length);
serv_addr.sin_port = htons(port);
if (connect(sockfd, (sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) //idk where the adress goes when casting
{
fprintf(stderr, "ERROR connecting\n");
fprintf(stderr, "%d %s\n", errno, strerror(errno));
return nullptr;
}
fcntl(sockfd, F_SETFL, O_NONBLOCK);
return new TCPStream(sockfd);
}
int TCPStream::write(void* buf, int len)
{
return ::write(sockfd, buf, len);
}
int TCPStream::read(void* buf, int len)
{
return ::read(sockfd, buf, len);
// read(sockfd, buf, len);
}
void TCPStream::write(Packet* packet)
{
char* data = packet->toByteArray();
unsigned int length = packet->getDataSize();
unsigned int dataWritten = 0;
while (dataWritten < length)
{
int curWritten = write(data + dataWritten, length - dataWritten);
dataWritten += curWritten > 0 ? curWritten : 0;
}
}
Packet* TCPStream::tryReadPacket()
{
char buf[1]; //still need a char array type
if (read(buf, 1) == 1)
{
Packet* p;
//identify type
char type = buf[0];
if (type == 0)
{
fprintf(stderr, "ERROR invalid packet type\n");
return nullptr;
}
else if (type == 1)
{
p = new Packet_Closed(this);
}
else if (type == 2)
{
p = new Packet_WriteFile(this);
}
else if (type == 3)
{
p = new Packet_DeletePath(this);
}
else if (type == 4)
{
p = new Packet_DeleteDir(this);
}
else
{
fprintf(stderr, "ERROR invalid packet type\n");
return nullptr;
}
if (p->read())
{
return p;
}
else
{
fprintf(stderr, "ERROR bad packet read\n");
}
}
return nullptr;
}