6 #include <sys/socket.h>
7 #include <netinet/in.h>
9 #include <sys/select.h>
16 #include "Session.hpp"
17 #include "Exceptions.hpp"
18 #include "DataBufferFile.hpp"
19 #include "DataBufferListing.hpp"
23 #include "DataBufferFileSymbian.hpp"
25 Data::Data( const SessionWPtr
& session
, RFile
* file
, Mode mode
)
28 , m_session( session
)
29 , m_buf( new char[BufSize
] )
31 if( mode
== M_UPLOAD
)
33 m_data
.reset( new DataBufferFileSymbian( file
, BufSize
) );
37 m_data
.reset( new DataBufferFileSymbian( file
, BufSize
) );
42 Data::Data( const SessionWPtr
& session
, FILE* file
, Mode mode
)
45 , m_session( session
)
46 , m_buf( new char[BufSize
] )
48 if( mode
== M_UPLOAD
)
50 m_data
.reset( new DataBufferFile( file
, BufSize
) );
54 m_data
.reset( new DataBufferFile( file
, BufSize
) );
58 Data::Data( const SessionWPtr
& session
, const std::list
<std::string
>& list
)
61 , m_session( session
)
62 , m_buf( new char[BufSize
] )
63 , m_data( new DataBufferListing( list
) )
74 closesocket( m_sock
);
81 bool Data::Connect( const std::string
& addr
, int port
)
83 if( ( m_sock
= socket( PF_INET
, SOCK_STREAM
, 0 ) ) == -1 )
85 throw SessionErrorException
;
90 sa
.sin_family
= AF_INET
;
91 sa
.sin_port
= htons( port
);
92 sa
.sin_addr
.s_addr
= inet_addr( addr
.c_str() );
93 memset( sa
.sin_zero
, 0, sizeof( sa
.sin_zero
) );
95 return connect( m_sock
, (sockaddr
*)&sa
, sizeof( sockaddr
) ) != -1;
98 bool Data::Accept( int sock
)
101 socklen_t size
= sizeof( addr
);
103 int incoming
= accept( sock
, (sockaddr
*)&addr
, &size
);
117 if( m_mode
== M_UPLOAD
)
129 int len
= m_data
->Read( m_buf
, BufSize
);
133 m_session
.lock()->DataConnectionFinished();
140 while( pos
!= len
&& CanSend() )
142 int size
= send( m_sock
, ptr
, len
- pos
, 0 );
146 throw SessionErrorException
;
150 m_session
.lock()->DataConnectionError();
160 m_data
->Store( ptr
, len
- pos
);
169 while( total
< BufSize
&& CanReceive() )
171 int size
= recv( m_sock
, m_buf
, BufSize
- total
, 0 );
174 throw SessionErrorException
;
178 m_session
.lock()->DataConnectionFinished();
183 int writeSize
= m_data
->Write( m_buf
, size
);
185 if( writeSize
!= size
)
187 m_session
.lock()->OutOfSpace();
200 FD_SET( m_sock
, &fd
);
204 select( m_sock
+ 1, NULL
, &fd
, NULL
, &tv
);
206 return FD_ISSET( m_sock
, &fd
);
209 bool Data::CanReceive()
213 FD_SET( m_sock
, &fd
);
217 select( m_sock
+ 1, &fd
, NULL
, NULL
, &tv
);
219 return FD_ISSET( m_sock
, &fd
);