Mercurial > hg > beaglert
comparison core/UdpServer.cpp @ 126:719119fb2905 scope-refactoring
Closing socket in the destructor
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Mon, 24 Aug 2015 15:25:34 +0100 |
parents | 23137a333c93 |
children | ff28e56e5b7e |
comparison
equal
deleted
inserted
replaced
125:850a4a9bd832 | 126:719119fb2905 |
---|---|
11 }; | 11 }; |
12 UdpServer::UdpServer(){ | 12 UdpServer::UdpServer(){ |
13 init(0); | 13 init(0); |
14 } | 14 } |
15 UdpServer::~UdpServer(){ | 15 UdpServer::~UdpServer(){ |
16 printf("Close the socket\n"); | 16 close(); |
17 shutdown(inSocket, 0); //Stop receiving data for this socket. If further data arrives, reject it. | |
18 //TODO: unbind from port. AFAIK, this involves closing the socket, therefore creating the socket should become part of bindToPort | |
19 }; | 17 }; |
20 bool UdpServer::init(int aPort){ | 18 bool UdpServer::init(int aPort){ |
21 enabled=true; | 19 enabled=true; |
22 stZeroTimeOut.tv_sec = 0; //set timeout to 0 | 20 stZeroTimeOut.tv_sec = 0; //set timeout to 0 |
23 stZeroTimeOut.tv_usec = 0; | 21 stZeroTimeOut.tv_usec = 0; |
45 enabled=false; | 43 enabled=false; |
46 return false; | 44 return false; |
47 } | 45 } |
48 enabled=true; | 46 enabled=true; |
49 return true; | 47 return true; |
50 }; | 48 } |
49 | |
50 void UdpServer::close(){ | |
51 int ret=::close(inSocket); | |
52 if(ret != 0) | |
53 printf("Error while closing socket, errno: %d\n", errno);//Stop receiving data for this socket. If further data arrives, reject it. | |
54 inSocket=0; | |
55 } | |
56 | |
51 int UdpServer::waitUntilReady(bool readyForReading, int timeoutMsecs){ | 57 int UdpServer::waitUntilReady(bool readyForReading, int timeoutMsecs){ |
52 // If the socket is ready on return, this returns 1. If it times-out before the socket becomes ready, it returns 0. If an error occurs, it returns -1. | 58 // If the socket is ready on return, this returns 1. If it times-out before the socket becomes ready, it returns 0. If an error occurs, it returns -1. |
53 if(enabled==false) | 59 if(enabled==false) |
54 return -1; | 60 return -1; |
55 if(timeoutMsecs<0) | 61 if(timeoutMsecs<0) |