Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
port_forwarder.cpp
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <sys/types.h>
4 #include <sys/wait.h>
5 #include <string>
6 
7 #include <vector>
8 
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include "inet.h"
12 #include "inet_file.h"
13 #include <sys/types.h>
14 #include <pwd.h>
15 using std::string;
16 //assumes null termination;
17 float fadein = 0;
18 float fadeout = 0;
19 float volume = 1;
20 bool scanChar( string s, char c, float &val )
21 {
22  char cc[2] = {c, 0};
23  string::size_type loc;
24  bool anyvalid = false;
25  while ( ( loc = s.find( cc ) ) != string::npos ) {
26  bool valid = (loc == 0);
27  if (!valid && loc > 0)
28  valid = ( (s[loc-1] == '\n') || (s[loc-1] == '\r') );
29  s = s.substr( loc+1 );
30  if (valid) {
31  sscanf( s.c_str(), "%f", &val );
32  anyvalid = true;
33  }
34  }
35  return anyvalid;
36 }
37 bool quit = false;
38 bool ForwardBytes( int fd, int mysocket_write, char buf[65536] )
39 {
40  if ( INET_BytesToRead( fd ) ) {
41  int ammt = INET_Recv( fd, buf, 65535 );
42  if (ammt == 0)
43  return false;
44  if (ammt == -1)
45  return true;
46  buf[ammt] = 0;
47  while ( -1 == INET_Write( mysocket_write, ammt, buf ) ) {}
48  }
49  return true;
50 }
51 
52 int main( int argc, char **argv )
53 {
54  char buf[65536];
55  INET_startup();
56  int fd = -1;
57  int ofd = -1;
58  int outport = 6000;
59  char *outaddress = "192.168.1.6";
60  if (argc > 2)
61  outaddress = (char*) argv[2];
62  if (argc > 3)
63  outport = atoi( argv[3] );
64  ofd = INET_ConnectTo( outaddress, outport );
65  if (ofd != -1) {
66  for (int i = 0; i < 10 && fd == -1; i++) {
67  int port = 4364;
68  if (argc > 1) port = atoi( argv[1] );
69  fd = INET_AcceptFrom( port, "localhost" );
70  }
71  }
72  if (fd == -1)
73  return 1;
74  timeval t;
75  t.tv_sec = 5;
76  t.tv_usec = 0;
77  fd_set fdset;
78  FD_ZERO( &fdset );
79  FD_SET( fd, &fdset );
80  FD_SET( ofd, &fdset );
81  fcntl( fd, F_SETFL, O_NONBLOCK );
82  fcntl( ofd, F_SETFL, O_NONBLOCK );
83  while (1) {
84  //select(fd>ofd?(fd+1):(ofd+1),&fdset,NULL,NULL,&t);
85  if ( !ForwardBytes( fd, ofd, buf ) )
86  break;
87  if ( !ForwardBytes( ofd, fd, buf ) )
88  break;
89  }
90  INET_cleanup();
91  return 0;
92 }
93