Added UDP getDestPort function
This commit is contained in:
parent
5bbb54848b
commit
0fe911899b
2 changed files with 28 additions and 8 deletions
|
@ -1023,8 +1023,12 @@ Socket::UDPConnection::UDPConnection(const UDPConnection & o) {
|
||||||
destAddr = 0;
|
destAddr = 0;
|
||||||
destAddr_size = 0;
|
destAddr_size = 0;
|
||||||
}
|
}
|
||||||
data = 0;
|
data = (char*)malloc(1024);
|
||||||
|
if (data){
|
||||||
|
data_size = 1024;
|
||||||
|
}else{
|
||||||
data_size = 0;
|
data_size = 0;
|
||||||
|
}
|
||||||
data_len = 0;
|
data_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1103,6 +1107,19 @@ void Socket::UDPConnection::GetDestination(std::string & destIp, uint32_t & port
|
||||||
DEBUG_MSG(DLVL_FAIL, "Could not get destination for UDP socket");
|
DEBUG_MSG(DLVL_FAIL, "Could not get destination for UDP socket");
|
||||||
}//Socket::UDPConnection GetDestination
|
}//Socket::UDPConnection GetDestination
|
||||||
|
|
||||||
|
/// Returns the port number of the receiving end of this socket.
|
||||||
|
/// Returns 0 on error.
|
||||||
|
uint32_t Socket::UDPConnection::getDestPort() const{
|
||||||
|
if (!destAddr || !destAddr_size){return 0;}
|
||||||
|
if (((struct sockaddr_in *)destAddr)->sin_family == AF_INET6) {
|
||||||
|
return ntohs(((struct sockaddr_in6 *)destAddr)->sin6_port);
|
||||||
|
}
|
||||||
|
if (((struct sockaddr_in *)destAddr)->sin_family == AF_INET) {
|
||||||
|
return ntohs(((struct sockaddr_in *)destAddr)->sin_port);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the socket to be blocking if the parameters is true.
|
/// Sets the socket to be blocking if the parameters is true.
|
||||||
/// Sets the socket to be non-blocking otherwise.
|
/// Sets the socket to be non-blocking otherwise.
|
||||||
void Socket::UDPConnection::setBlocking(bool blocking) {
|
void Socket::UDPConnection::setBlocking(bool blocking) {
|
||||||
|
@ -1214,20 +1231,22 @@ bool Socket::UDPConnection::Receive() {
|
||||||
data_size = SOCKETSIZE;
|
data_size = SOCKETSIZE;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
int r = recvfrom(sock, data, data_size, MSG_PEEK | MSG_TRUNC, 0, 0);
|
int r = recvfrom(sock, data, data_size, MSG_PEEK | MSG_TRUNC | MSG_DONTWAIT, 0, 0);
|
||||||
if (r == -1) {
|
if (r == -1) {
|
||||||
if (errno != EAGAIN) {
|
if (errno != EAGAIN) {
|
||||||
INFO_MSG("Found an error: %d (%s)", errno, strerror(errno));
|
INFO_MSG("UDP receive: %d (%s)", errno, strerror(errno));
|
||||||
}
|
}
|
||||||
data_len = 0;
|
data_len = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (data_size < (unsigned int)r) {
|
if (data_size < (unsigned int)r) {
|
||||||
data = (char *)realloc(data, r);
|
char* tmp = (char*)realloc(data, r);
|
||||||
if (data) {
|
if (tmp) {
|
||||||
|
data = tmp;
|
||||||
data_size = r;
|
data_size = r;
|
||||||
}else{
|
}else{
|
||||||
data_size = 0;
|
FAIL_MSG("Could not resize socket buffer to %d bytes!", r);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
socklen_t destsize = destAddr_size;
|
socklen_t destsize = destAddr_size;
|
||||||
|
|
|
@ -141,6 +141,7 @@ namespace Socket {
|
||||||
void setBlocking(bool blocking);
|
void setBlocking(bool blocking);
|
||||||
void SetDestination(std::string hostname, uint32_t port);
|
void SetDestination(std::string hostname, uint32_t port);
|
||||||
void GetDestination(std::string & hostname, uint32_t & port);
|
void GetDestination(std::string & hostname, uint32_t & port);
|
||||||
|
uint32_t getDestPort() const;
|
||||||
bool Receive();
|
bool Receive();
|
||||||
void SendNow(const std::string & data);
|
void SendNow(const std::string & data);
|
||||||
void SendNow(const char * data);
|
void SendNow(const char * data);
|
||||||
|
|
Loading…
Add table
Reference in a new issue