#include <common.h>
Inheritance diagram for ReceivingPort:
Public Member Functions | |
ReceivingPort () | |
virtual | ~ReceivingPort () |
void | init () |
Packet * | receivePacket () |
The main receive function of receiving port to receive a single packet. | |
Protected Attributes | |
Packet * | pkt_ |
char * | tmpBuffer_ |
temporary buffer for packets |
ReceivingPort is an abstract class for the interface to receive a packet. The main function for the receiving port:
Definition at line 301 of file common.h.
|
Constructor Definition at line 428 of file common.cpp. References pkt_.
|
|
Init Funciton to initialize a socket port. The port binds to its own address, generate a UDP socket. Bind to local address is one important task in init() Here source address of node itself (myaddr_) does not really be used by bind function of port. The program use INADDR_ANY as the address filled in address parameters of bind(). So, we need an empty hostname with the port number. When the UDP port is recieving, we need to create a default buffer to store received packet. The data in buffer will be copy to the corresponding flow once the packet's sender address is checked. Implements Port. Definition at line 447 of file common.cpp. References Address::isSet(), Port::myaddr_, Port::setHostname(), Port::setPort(), and Port::sockfd_. 00448 { 00449 if (sockfd_ != 0) { 00450 return; 00451 } 00452 if ( myaddr_.isSet() == false) { 00453 setHostname("localhost"); 00454 setPort(DEFAULT_RECV_PORT); 00455 } 00456 00457 if ((sockfd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 00458 throw "Error while opening UDP socket of a receiver"; 00459 } 00460 Address *emptyAddr = new Address("", myaddr_.getPort()); 00461 struct sockaddr* addr = setSockAddress(emptyAddr, &mySockAddress_); 00462 if ( bind(sockfd_, addr, sizeof(struct sockaddr_in)) < 0 ){ 00463 throw "Scoket Bind Error occured in an UDP receiver"; 00464 } 00465 //cout << "binding to port: " << myaddr_.getPort() << "......" << endl; 00466 // needs a dummy buffer for storing packets 00467 tmpBuffer_ = new char[MAXBUFLENGTH]; 00468 }
|
|
The main receive function of receiving port to receive a single packet. The main receive function of a receiving port. First,check addresses. Then call recvfrom() to get a packet. after this, pkt_ variable stores information of the packet and tmpSockAddr stores the sender information. then, recast tmpSockAddr to itsaddr_. As the socket is not set as non-blocking, the recvfrom() call blocks usually, but if the main program use select() to do synchronized I/O Multiplexing, this call will not block. The design structure allows a future implementation improvement. packet size is the maximum allowed packet above UDP or buffer size Reimplemented in LossyReceivingPort. Definition at line 483 of file common.cpp. References Port::decodeSockAddress(), Packet::extractHeader(), Packet::fillPayload(), Packet::getHeaderSize(), Port::itsaddr_, pkt_, Port::sockfd_, and tmpBuffer_. Referenced by LossyReceivingPort::receivePacket(). 00484 { 00485 struct sockaddr_in tmpSockAddr; 00486 int length = sizeof(struct sockaddr); 00487 int len = (int)recvfrom(sockfd_, tmpBuffer_, MAXBUFLENGTH, 0, (struct sockaddr*)&tmpSockAddr,(socklen_t *)&length); 00488 if (len == -1) 00489 { 00490 perror("recvfrom"); 00491 return false; 00492 } 00493 decodeSockAddress( &itsaddr_, &tmpSockAddr); 00494 pkt_->extractHeader(tmpBuffer_); 00495 //tmpBuffer_ point shall not be moved,it will reused by the receiving port. 00496 pkt_->fillPayload(len-1-pkt_->getHeaderSize(), tmpBuffer_+pkt_->getHeaderSize()+1 ); 00497 return pkt_; 00498 }
|
|
This pointer points to the packet. This packet is just received. Definition at line 314 of file common.h. Referenced by receivePacket(), and ReceivingPort(). |