3 #include <sys/socket.h>
4 #include <netinet/in.h>
8 #include <sys/select.h>
10 #include "Session.hpp"
11 #include "Exceptions.hpp"
12 #include "DataBufferFile.hpp"
13 #include "DataBufferListing.hpp"
15 Data::Data( const SessionWPtr
& session
, FILE* file
, Mode mode
)
18 , m_session( session
)
19 , m_buf( new char[BufSize
] )
20 , m_data( new DataBufferFile( file
) )
24 Data::Data( const SessionWPtr
& session
, const std::list
<std::string
>& list
)
27 , m_session( session
)
28 , m_buf( new char[BufSize
] )
29 , m_data( new DataBufferListing( list
) )
43 bool Data::Connect( const std::string
& addr
, int port
)
45 if( ( m_sock
= socket( PF_INET
, SOCK_STREAM
, 0 ) ) == -1 )
47 throw SessionErrorException
;
52 sa
.sin_family
= AF_INET
;
53 sa
.sin_port
= htons( port
);
54 sa
.sin_addr
.s_addr
= inet_addr( addr
.c_str() );
55 memset( sa
.sin_zero
, 0, sizeof( sa
.sin_zero
) );
57 return connect( m_sock
, (sockaddr
*)&sa
, sizeof( sockaddr
) ) != -1;
60 bool Data::Accept( int sock
)
63 socklen_t size
= sizeof( addr
);
65 int incoming
= accept( sock
, (sockaddr
*)&addr
, &size
);
79 if( m_mode
== M_UPLOAD
)
91 int len
= m_data
->Read( m_buf
, BufSize
);
95 SessionPtr sptr
= m_session
.lock();
98 throw "Data lost its Session";
100 sptr
->DataConnectionFinished();
109 int size
= send( m_sock
, ptr
, len
- pos
, 0 );
113 throw SessionErrorException
;
117 SessionPtr sptr
= m_session
.lock();
120 throw "Data lost its Session";
122 sptr
->DataConnectionError();
135 int size
= recv( m_sock
, m_buf
, BufSize
, 0 );
139 throw SessionErrorException
;
143 SessionPtr sptr
= m_session
.lock();
146 throw "Data lost its Session";
148 sptr
->DataConnectionFinished();
152 int writeSize
= m_data
->Write( m_buf
, size
);
154 if( writeSize
!= size
)
156 SessionPtr sptr
= m_session
.lock();
159 throw "Data lost its Session";
170 FD_SET( m_sock
, &fd
);
174 select( m_sock
+ 1, NULL
, &fd
, NULL
, &tv
);
176 return FD_ISSET( m_sock
, &fd
);
179 bool Data::CanReceive()
183 FD_SET( m_sock
, &fd
);
187 select( m_sock
+ 1, &fd
, NULL
, NULL
, &tv
);
189 return FD_ISSET( m_sock
, &fd
);