gloox  0.9.9.12
connectiontcpclient.cpp
1 /*
2  Copyright (c) 2004-2008 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #include "gloox.h"
16 
17 #include "connectiontcpclient.h"
18 #include "dns.h"
19 #include "logsink.h"
20 #include "mutexguard.h"
21 
22 #ifdef __MINGW32__
23 # include <winsock.h>
24 #endif
25 
26 #if !defined( _WIN32 ) && !defined( _WIN32_WCE )
27 # include <sys/types.h>
28 # include <sys/socket.h>
29 # include <sys/select.h>
30 # include <unistd.h>
31 #else
32 # include <winsock.h>
33 #endif
34 
35 #include <cstdlib>
36 #include <string>
37 
38 #ifndef _WIN32_WCE
39 # include <sstream>
40 #endif
41 
42 namespace gloox
43 {
44 
46  const std::string& server, int port )
47  : ConnectionTCPBase( logInstance, server, port )
48  {
49  }
50 
52  const std::string& server, int port )
53  : ConnectionTCPBase( cdh, logInstance, server, port )
54  {
55  }
56 
57 
59  {
60  }
61 
63  {
64  return new ConnectionTCPClient( m_handler, m_logInstance, m_server, m_port );
65  }
66 
68  {
69  m_sendMutex.lock();
70 
71  if( !m_handler || m_socket >= 0 )
72  {
73  m_sendMutex.unlock();
74  return ConnNotConnected;
75  }
76 
77  if( m_state > StateDisconnected )
78  {
79  m_sendMutex.unlock();
80  return ConnNoError;
81  }
82 
83  m_state = StateConnecting;
84 
85  if( m_socket < 0 )
86  {
87  if( m_port == -1 )
88  m_socket = DNS::connect( m_server, m_logInstance );
89  else
90  m_socket = DNS::connect( m_server, m_port, m_logInstance );
91  }
92 
93  m_sendMutex.unlock();
94 
95  if( m_socket < 0 )
96  {
97  switch( m_socket )
98  {
101  m_server + ": connection refused" );
102  break;
103  case -ConnDnsError:
105  m_server + ": host not found" );
106  break;
107  default:
109  "Unknown error condition" );
110  break;
111  }
112  m_handler->handleDisconnect( this, (ConnectionError)-m_socket );
113  return (ConnectionError)-m_socket;
114  }
115  else
116  {
117  m_state = StateConnected;
118  }
119 
120  m_cancel = false;
121  m_handler->handleConnect( this );
122  return ConnNoError;
123  }
124 
126  {
127  m_recvMutex.lock();
128 
129  if( m_cancel || m_socket < 0 )
130  {
131  m_recvMutex.unlock();
132  return ConnNotConnected;
133  }
134 
135  if( !dataAvailable( timeout ) )
136  {
137  m_recvMutex.unlock();
138  return ConnNoError;
139  }
140 
141 #ifdef SKYOS
142  int size = ::recv( m_socket, (unsigned char*)m_buf, m_bufsize, 0 );
143 #else
144  int size = ::recv( m_socket, m_buf, m_bufsize, 0 );
145 #endif
146 
147  if( size > 0 )
148  m_totalBytesIn += size;
149 
150  m_recvMutex.unlock();
151 
152  if( size <= 0 )
153  {
154  ConnectionError error = ( size ? ConnIoError : ConnStreamClosed );
155  if( m_handler )
156  m_handler->handleDisconnect( this, error );
157  return error;
158  }
159 
160  m_buf[size] = '\0';
161 
162  if( m_handler )
163  m_handler->handleReceivedData( this, std::string( m_buf, size ) );
164 
165  return ConnNoError;
166  }
167 
168 }