#include <common.h>
Inheritance diagram for SendingPort:

Public Member Functions | |
| SendingPort (char *hostname, short port) | |
| Another constructor with local address given. | |
| virtual | ~SendingPort () |
| Deconstructor. | |
| void | init () |
| void | sendPacket (Packet *pkt) |
| void | setBroadcast () |
| void | setBroadcastOff () |
| virtual void | timerHandler ()=0 |
Public Attributes | |
| TxTimer | timer_ |
Protected Attributes | |
| int | bcastflag_ |
| this flag indicates the port ought to broadcast or unicast a packet. | |
| char * | sendingbuf_ |
SendingPort is an subclass of Port for sending purpose
Definition at line 307 of file common.h.
|
|
Function to initialize the port Implements Port. Definition at line 365 of file common.cpp. References bcastflag_, Address::getPort(), Address::isSet(), Port::itsaddr_, Port::myaddr_, Port::mySockAddress_, sendingbuf_, Port::setHostname(), Port::setPort(), Port::setSockAddress(), and Port::sockfd_. 00366 {
00367 //"0" shows the sockfd is uninitialized, -1 means error
00368 if (sockfd_ != 0) {
00369 cout << "socket has not been properly initialized!" << endl;
00370 return;
00371 }
00372 if ( myaddr_.isSet() == false) {
00373 setHostname("localhost");
00374 setPort(DEFAULT_SEND_PORT);
00375 }
00376 //check if itsaddr_ is set
00377 if ( itsaddr_.isSet() == false)
00378 throw "Destination address of a sending port is not set!";
00379
00380 if ((sockfd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00381 perror("socket");
00382 throw "Error while opening a UDP socket";
00383 }
00384 Address *emptyAddr = new Address("", myaddr_.getPort());
00385 struct sockaddr* addr = setSockAddress(emptyAddr, &mySockAddress_);
00386 if ( bind(sockfd_, addr, sizeof(struct sockaddr_in)) < 0 ){
00387 perror("bind");
00388 throw "Scoket Bind Error";
00389 }
00390
00391 if (bcastflag_ == 1)
00392 if (setsockopt(sockfd_,SOL_SOCKET,SO_BROADCAST,&bcastflag_,sizeof(bcastflag_)) == -1 )
00393 {
00394 perror("setsockopt");
00395 throw "Set broadcast option failed.";
00396 };
00397
00398 //create sending buffer
00399 sendingbuf_ = new char[MTU_SIZE+1];
00400 return;
00401
00402 }
|
|
|
Function to send a packet. The default socket file descriptor will always be used for send() only a sockfd used by the port. Definition at line 409 of file common.cpp. References Port::dstSockAddress_, Port::getRemoteAddr(), Packet::makePacket(), sendingbuf_, Port::setSockAddress(), and Port::sockfd_. 00410 {
00411 int pktlen = pkt->makePacket(sendingbuf_);
00412 Address *dst = getRemoteAddr();
00413 int length = sizeof(struct sockaddr_in);
00414 struct sockaddr *dest = setSockAddress(dst, &dstSockAddress_);
00415 int len = sendto(sockfd_, sendingbuf_, pktlen, 0, dest, length);
00416 if (len == -1)
00417 {
00418 perror("send");
00419 throw "Sending Error.";
00420 }
00421 }
|
|
|
toggle broadcast option on Definition at line 328 of file common.h. References bcastflag_. 00328 {bcastflag_ = 1;}
|
|
|
toggle broadcast option off Definition at line 332 of file common.h. References bcastflag_. 00332 {bcastflag_ = 0;}
|
|
|
TimerHandler is called when the TxTimer expires. This function is virtual. So another child class has to be derived from this base class to use the timer. Referenced by TxTimer::timerProc(). |
|
|
this flag indicates the port ought to broadcast or unicast a packet. If broadcast, an broadcast IP address (192.168.255.255, etc) need to be supplied Definition at line 346 of file common.h. Referenced by init(), setBroadcast(), and setBroadcastOff(). |
|
|
Sending buffer Definition at line 350 of file common.h. Referenced by init(), and sendPacket(). |
|
|
The timer used to schedule future events in a sending port. When this timer expires, the timerHandler will be called. |
1.4.2