📄 udplocal.cc
字号:
//// UDPlocal.cc : UDP Local Support for Diffusion// Authors : Chalermek Intanagonwiwat and Fabio Silva//// Copyright (C) 2000-2002 by the University of Southern California// $Id: UDPlocal.cc,v 1.7 2002/06/27 22:04:31 fabio Exp $//// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License,// version 2, as published by the Free Software Foundation.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License along// with this program; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.////#include <stdio.h>#include <stdlib.h>#include "UDPlocal.hh"UDPLocal::UDPLocal(u_int16_t *port) : DiffusionIO(){ int fd; struct sockaddr_in skt_addr_inet; struct sockaddr_in my_addr_inet; socklen_t my_addr_inet_size = sizeof(my_addr_inet); fd = socket(AF_INET, SOCK_DGRAM, 0); bzero((char *) &skt_addr_inet, sizeof(skt_addr_inet)); skt_addr_inet.sin_family = AF_INET; skt_addr_inet.sin_port = htons(*port); skt_addr_inet.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(fd, (struct sockaddr *) &skt_addr_inet, sizeof(skt_addr_inet)) < 0){ DiffPrint(DEBUG_ALWAYS, "Can't bind to incoming socket\n"); exit(-1); } in_fds_.push_back(fd); max_in_descriptor_ = fd; // Return the actual UDP port if port is zero if (*port == 0){ getsockname(fd, (struct sockaddr *) &(my_addr_inet), &my_addr_inet_size); *port = ntohs(my_addr_inet.sin_port); }}DiffPacket UDPLocal::recvPacket(int fd){ DiffPacket incoming_packet = NULL; int packet_size; int header[(sizeof(struct hdr_diff) / sizeof(int)) + 1]; recvfrom(fd, &header[0], sizeof(struct hdr_diff), MSG_PEEK, NULL, NULL); packet_size = sizeof(struct hdr_diff) + ntohs(DATA_LEN(HDR_DIFF(&header[0]))); incoming_packet = new int [(packet_size / sizeof(int)) + 1]; if (incoming_packet == NULL){ DiffPrint(DEBUG_ALWAYS, "Cannot allocate memory for incoming packet !\n"); exit(-1); } recvfrom(fd, incoming_packet, packet_size, 0, NULL, NULL); return incoming_packet;}void UDPLocal::sendPacket(DiffPacket pkt, int len, int dst){ u_int16_t port = (u_int16_t) dst; LocalAgentsList::iterator itr; LocalAgentEntry *agent; if (len > MAX_UDP_MESSAGE_SIZE){ DiffPrint(DEBUG_ALWAYS, "Warning: Packet larger than maximum UDP datagram size !\n"); exit(-1); } for (itr = local_agents_.begin(); itr != local_agents_.end(); ++itr){ if ((*itr)->agent_port_ == port){ agent = *itr; sendto(agent->fd_, pkt, len, 0, (struct sockaddr *)&(agent->skt_addr_inet_), sizeof(agent->skt_addr_inet_)); return; } } // We do not have a socket to this agent agent = new LocalAgentEntry; addLocalAgent(agent, port); // Now send the Packet sendto(agent->fd_, pkt, len, 0, (struct sockaddr *)&(agent->skt_addr_inet_), sizeof(agent->skt_addr_inet_));}void UDPLocal::addLocalAgent(LocalAgentEntry *agent, u_int16_t port){ struct hostent *host_ent; int fd; host_ent = gethostbyname(LOCALHOST); if (host_ent == NULL){ DiffPrint(DEBUG_ALWAYS, "Cannot get local host entry !\n"); exit(-1); } fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0){ DiffPrint(DEBUG_ALWAYS, "Cannot create socket !\n"); exit(-1); } agent->fd_ = fd; agent->agent_port_ = port; bzero((char *) &(agent->skt_addr_inet_), sizeof(agent->skt_addr_inet_)); agent->skt_addr_inet_.sin_family = AF_INET; agent->skt_addr_inet_.sin_port = htons(port); bcopy(host_ent->h_addr, (char *) &(agent->skt_addr_inet_.sin_addr), host_ent->h_length); // Insert agent in the list local_agents_.push_back(agent);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -