-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPSocket.h
76 lines (75 loc) · 2.18 KB
/
UDPSocket.h
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
#ifndef UDPSOCKET_H
#define UDPSOCKET_H
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include "string.h"
#include <thread>
#include <mutex>
#include <queue>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include "Message.h"
#include <algorithm>
#include <math.h>
#include <thread>
#include <unordered_map>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <chrono>
#include <sys/time.h>
#include <ctime>
#include <unistd.h>
using namespace std::chrono;
using namespace std;
//#define RECEIVE_OUTPUT_FILE_LOG
//#define DEBUG
class UDPSocket
{
protected:
int sock;
struct sockaddr_in myAddr;
int myPort;
string myAddress_str;
thread * ReceiveThread;
thread* SendThread;
thread * FaultThread;
mutex ReceiveBufferMtx;
mutex SendBufferMtx;
mutex NonAckedMtx;
mutex sockMtx;
//<string(MSGID+fragc), <trialsLeft, Message *>>
unordered_map<string, pair<unsigned int, Message *>> NonAcked;
queue<Message *> ReceiveBuffer;
queue<Message *> SendBuffer;
bool enabled = true;
unsigned int FRAG_MSG_SIZE = 10000;
unsigned int SOCK_MAX_BUFFER_SIZE = 100000;
unsigned int faultTrials = 20;
ofstream outFile;
bool dest=false;
public:
UDPSocket ();
bool initializeSocket(char * _myAddr, unsigned int _myPort);
bool initializeSocket(unsigned int _myPort);
char * getMachineIP();
int getMyPort();
string getMyIP();
void setMyPort(unsigned int _myPort);
Message * receiveMsg();
bool sendMessage(Message * FullMessage);
void fragmentMsg(Message * FullMessage, vector<Message *> & frags);
string getMsgID(Message* message);
string getFragmentID(Message* message);
string getAckFragmentID(Message* message);
void sendingHandler(UDPSocket * myUDPSocket);
void receiveHandler(UDPSocket * myUDPSocket);
void faultToleranceHandler(UDPSocket * myUDPSockeT);
void setBroadcast(int s);
~UDPSocket ( );
};
#endif // UDPSOCKET_H