-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUDPGSocket.cpp
81 lines (68 loc) · 2.78 KB
/
UDPGSocket.cpp
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: UDPGSocket.cpp
* Author: gedas
*
* Created on Šeštadienis, 2016, balandis 23, 00.31
*/
#include "UDPGSocket.h"
#include "GSocket.h"
#include "GLogger.h"
#include "UDPClientGSocket.h"
#include <arpa/inet.h>
GServer::UDPGSocket::UDPGSocket(GServer::GConfig* conf, GLogger* logger,
GCommandExecution* command) : GSocket(conf, logger, command) {
// Nustatau pavadinima
this->className = this->className + ":UDPGSocket";
this->commands = command;
this->logger->logDebug(this->className, "Objektas sukurtas");
}
GServer::UDPGSocket::~UDPGSocket() {
this->logger->logDebug(this->className, "Objektas sunaikintas");
}
void GServer::UDPGSocket::listenSocket() {
// Si funkcija padeda uzblokuoti listen funkcijos kvietima UDP protokokui
}
int GServer::UDPGSocket::reciveData(char* buffer, int size) {
int returnValue = -1;
// Siusti duomenis
char IP[NI_MAXHOST]; //The clienthost will hold the IP address.
char PORT[NI_MAXSERV];
returnValue = recvfrom(this->socket_descriptor, buffer, size, 0,
(struct sockaddr *) &serverStorage, &addr_size);
int theErrorCode = getnameinfo((struct sockaddr *) &serverStorage,
sizeof (sockaddr), IP, sizeof (IP), PORT, sizeof (PORT),
NI_NUMERICHOST | NI_NUMERICSERV);
this->logger->logDebug(this->className, string(IP) + ":" + string(PORT));
// Pranesimas gautus duomenis
this->logger->logDebug(this->className,
std::to_string(this->socket_descriptor) + ":" +
std::to_string(this->buffer.size()) + " <---" +
std::to_string(returnValue));
return returnValue;
}
sockaddr_storage GServer::UDPGSocket::returnClientAddressInfo() {
return this->serverStorage;
}
int GServer::UDPGSocket::sendData(char* data, int size) {
char IP[NI_MAXHOST]; //The clienthost will hold the IP address.
char PORT[NI_MAXSERV];
int theErrorCode = getnameinfo((struct sockaddr *) &serverStorage,
sizeof (sockaddr), IP, sizeof (IP), PORT, sizeof (PORT),
NI_NUMERICHOST | NI_NUMERICSERV);
int send = 0;
while (send != size && send != -1) {
this->logger->logDebug(this->className, string(IP) + ":" + string(PORT));
send = send + sendto(this->socket_descriptor, data, size, 0,
(struct sockaddr *) &serverStorage, sizeof(sockaddr));
this->logger->logDebug(this->className,
std::to_string(this->socket_descriptor) + ":" +
std::to_string(size) + " ---> " + std::to_string(send));
}
//this->logger->logDebug(this->className,string(data,size));
return send;
}