Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

VrUDPSource.h

Go to the documentation of this file.
00001 /* -*- Mode: c++ -*- */ 00002 /* 00003 * Copyright 2001 Free Software Foundation, Inc. 00004 * 00005 * This file is part of GNU Radio 00006 * 00007 * GNU Radio is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2, or (at your option) 00010 * any later version. 00011 * 00012 * GNU Radio is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with GNU Radio; see the file COPYING. If not, write to 00019 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00020 * Boston, MA 02111-1307, USA. 00021 */ 00022 /* 00023 * Copyright 1999 Massachusetts Institute of Technology 00024 * 00025 * Permission to use, copy, modify, distribute, and sell this software and its 00026 * documentation for any purpose is hereby granted without fee, provided that 00027 * the above copyright notice appear in all copies and that both that 00028 * copyright notice and this permission notice appear in supporting 00029 * documentation, and that the name of M.I.T. not be used in advertising or 00030 * publicity pertaining to distribution of the software without specific, 00031 * written prior permission. M.I.T. makes no representations about the 00032 * suitability of this software for any purpose. It is provided "as is" 00033 * without express or implied warranty. 00034 * 00035 */ 00036 00037 #ifndef _VRUDPSOURCE_H_ 00038 #define _VRUDPSOURCE_H_ 00039 00040 #include <VrSource.h> 00041 00042 #include <string.h> 00043 #include <sys/types.h> 00044 #include <netinet/in.h> 00045 #include <sys/socket.h> 00046 #include <sys/wait.h> 00047 #include <arpa/inet.h> 00048 #include <netinet/in.h> 00049 00050 #define MAX_UDP_SIZE 3000 00051 00052 template<class oType> 00053 class VrUDPSource : public VrSource<oType> { 00054 protected: 00055 int sockfd; 00056 struct sockaddr_in my_addr; /* my address information */ 00057 struct sockaddr_in their_addr; /* connector's address information */ 00058 unsigned char tempbuf[MAX_UDP_SIZE]; 00059 int tempbuf_index, tempbuf_size; 00060 public: 00061 virtual const char *name() { return "VrUDPSource"; } 00062 00063 virtual int work2(VrSampleRange output, void *o[]); 00064 00065 VrUDPSource (double sampling_freq, int port); 00066 virtual ~VrUDPSource() {} 00067 }; 00068 00069 template<class oType> int 00070 VrUDPSource<oType>::work2(VrSampleRange output, void *ao[]) 00071 { 00072 oType **o= (oType **)ao; 00073 unsigned int addr_len = sizeof(struct sockaddr); 00074 int size = output.size * sizeof(oType); 00075 unsigned char *cp = (unsigned char *)o[0]; 00076 int temp; 00077 00078 //jca printf ("[%s] work %ld\n", __FILE__, output.size); 00079 printf ("[%s] work %ld\n", __FILE__, output.size); 00080 while (size > 0) { 00081 if (tempbuf_size > 0) { 00082 *cp++ = tempbuf[tempbuf_index++]; 00083 tempbuf_size--; 00084 size--; 00085 continue; 00086 } 00087 if (size < MAX_UDP_SIZE) { 00088 tempbuf_index = 0; 00089 tempbuf_size = recvfrom(sockfd, tempbuf, sizeof(tempbuf), 0, 00090 (struct sockaddr *)&their_addr, &addr_len); 00091 continue; 00092 } 00093 temp = recvfrom(sockfd, cp, size, 0, 00094 (struct sockaddr *)&their_addr, &addr_len); 00095 if (temp > 0){ 00096 // jca printf ("received %d bytes from %s\n", temp, inet_ntoa(their_addr.sin_addr) ); 00097 printf ("received %d bytes from %s\n", temp, inet_ntoa(their_addr.sin_addr) ); 00098 size -= temp; 00099 cp += temp; 00100 } 00101 else { 00102 printf ("[%s:%d] error in recvfrom %d\n", __FILE__, __LINE__, errno); 00103 break; 00104 } 00105 } 00106 return (int)output.size; 00107 } 00108 00109 template<class oType> 00110 VrUDPSource<oType>::VrUDPSource(double sampling_freq, int port) 00111 { 00112 setSamplingFrequency (sampling_freq); 00113 tempbuf_index = 0; 00114 tempbuf_size = 0; 00115 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 00116 perror("socket"); 00117 exit(1); 00118 } 00119 my_addr.sin_family = AF_INET; /* host byte order */ 00120 my_addr.sin_port = htons(port); /* short, network byte order */ 00121 my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ 00122 bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */ 00123 00124 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { 00125 perror("bind"); 00126 exit(1); 00127 } 00128 } 00129 #endif

Generated on Wed Aug 4 02:22:06 2004 for GNU Radio by doxygen 1.3.8