-
Notifications
You must be signed in to change notification settings - Fork 1
/
udp.hpp
106 lines (90 loc) · 2.2 KB
/
udp.hpp
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
#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <cstring>
#include <memory>
#include <iostream>
#include <fstream>
#define DGRAMSIZE 512
#define DATASIZE (DGRAMSIZE-3*sizeof(int32_t))
#define CONNECT 1
#define ACK 2
#define MESSAGE 3
#define DATAGRAM 4
#define DATAFILE 5
#define ENDFILE 6
#define ACCEPT 30
#define REJECT 31
#define TIMEOUT -99
class UDPSocket;
class UDPConnection;
class UDPServer;
class UDPClient;
typedef std::shared_ptr<UDPServer> udpserver_ptr;
typedef std::shared_ptr<UDPConnection> udpconnection_ptr;
typedef std::shared_ptr<UDPClient> udpclient_ptr;
typedef struct datagram {
int32_t type;
int32_t seqNumber;
int32_t size;
char data[DATASIZE];
} Datagram;
void zerosDatagram (Datagram* dg);
class UDPSocket {
protected:
struct sockaddr_in socketAddrLocal;
int socketDesc;
public:
struct sockaddr_in* getAddr();
int getSocketDesc();
};
class UDPConnection: public UDPSocket {
friend class UDPServer;
protected:
bool connected;
int port;
public:
UDPConnection();
UDPConnection(int port, sockaddr_in socketAddrRemote, sockaddr_in socketAddrLocal);
struct sockaddr_in socketAddrRemote;
int sendDatagram(Datagram &dg);
int recDatagram();
int sendDatagramMaxTries(Datagram &dg, int maxTries);
int recDatagramTimeOut(int timeOut);
char sendbuffer[DGRAMSIZE];
char recvbuffer[DGRAMSIZE];
long recvMessageSize;
Datagram* getRecvbuffer();
int sendString(std::string str);
std::string receiveString();
int sendMessage(char* buffer, int length);
int sendFile(FILE* file);
char* receiveMessage();
long getRecvMessageSize();
int receiveFile(FILE* file);
struct sockaddr_in* getAddrRemote();
bool isConnected();
void close();
};
class UDPClient: public UDPConnection {
public:
UDPClient();
UDPClient(int port, std::string ip);
~UDPClient();
int connect();
int waitResponse();
void close();
};
class UDPServer: public UDPConnection {
public:
UDPServer();
UDPServer(int port);
~UDPServer();
udpconnection_ptr accept();
void _bind();
};