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
vsnet_pipe.cpp
Go to the documentation of this file.
1 #include <config.h>
2 #include <sstream>
3 
4 #include "vsnet_pipe.h"
5 #include "vsnet_oss.h"
6 #include "vsnet_socket.h"
7 
8 #if defined (_WIN32) && !defined (__CYGWIN__)
9 
11  _failed( false )
12 {
13  int retval;
14  struct sockaddr_in addr;
15 
16  _pipe[0] = ::socket( PF_INET, SOCK_DGRAM, 0 );
17  if (_pipe[0] == INVALID_SOCKET) {
18  _failed = true;
19  return;
20  }
21  memset( &addr, 0, sizeof (struct sockaddr_in) );
22  addr.sin_addr.s_addr = htonl( INADDR_ANY );
23  addr.sin_port = 0;
24  addr.sin_family = AF_INET;
25  retval = ::bind( _pipe[0], (sockaddr*) &addr, sizeof (addr) );
26  if (retval == SOCKET_ERROR) {
27  VsnetOSS::close_socket( _pipe[0] );
28  _failed = true;
29  return;
30  }
31  socklen_t addrlen = sizeof (addr);
32  retval = ::getsockname( _pipe[0], (sockaddr*) &addr, &addrlen );
33  if (retval == SOCKET_ERROR) {
34  VsnetOSS::close_socket( _pipe[0] );
35  _failed = true;
36  return;
37  }
38  _pipe[1] = ::socket( PF_INET, SOCK_DGRAM, 0 );
39  if (_pipe[1] == INVALID_SOCKET) {
40  VsnetOSS::close_socket( _pipe[1] );
41  _failed = true;
42  return;
43  }
44  retval = ::connect( _pipe[1], (sockaddr*) &addr, addrlen );
45  if (retval == SOCKET_ERROR) {
46  VsnetOSS::close_socket( _pipe[1] );
47  VsnetOSS::close_socket( _pipe[0] );
48  _failed = true;
49  return;
50  }
51 }
52 
53 int VSPipe::write( const char *buf, int size )
54 {
55  return ::send( _pipe[1], buf, size, 0 );
56 }
57 
58 int VSPipe::read( char *buf, int size )
59 {
60  return VsnetOSS::recv( _pipe[0], buf, size, 0 );
61 }
62 
63 #else
64 #include <stdio.h>
65 #include <unistd.h>
66 
68 {
69  int ret = pipe( _pipe );
70  if (ret != 0)
71  _failed = true;
72  else
73  _failed = false;
74 }
75 
76 int VSPipe::write( const char *buf, int size )
77 {
78  return ::write( _pipe[1], buf, size );
79 }
80 
81 int VSPipe::read( char *buf, int size )
82 {
83  return ::read( _pipe[0], buf, size );
84 }
85 
86 #endif
87 
89 {
90  return VsnetOSS::close_socket( _pipe[1] );
91 }
92 
94 {
95  return VsnetOSS::close_socket( _pipe[0] );
96 }
97 
98 int VSPipe::getread() const
99 {
100  return _pipe[0];
101 }
102 
103 bool VSPipe::ok() const
104 {
105  return !_failed;
106 }
107