1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_LIBJINGLE_XMPP_XMPPSOCKET_H_
12 #define WEBRTC_LIBJINGLE_XMPP_XMPPSOCKET_H_
13 
14 #include "webrtc/libjingle/xmpp/asyncsocket.h"
15 #include "webrtc/libjingle/xmpp/xmppengine.h"
16 #include "webrtc/base/asyncsocket.h"
17 #include "webrtc/base/bytebuffer.h"
18 #include "webrtc/base/sigslot.h"
19 
20 // The below define selects the SSLStreamAdapter implementation for
21 // SSL, as opposed to the SSLAdapter socket adapter.
22 // #define USE_SSLSTREAM
23 
24 namespace rtc {
25   class StreamInterface;
26   class SocketAddress;
27 };
28 extern rtc::AsyncSocket* cricket_socket_;
29 
30 namespace buzz {
31 
32 class XmppSocket : public buzz::AsyncSocket, public sigslot::has_slots<> {
33 public:
34   XmppSocket(buzz::TlsOptions tls);
35   ~XmppSocket();
36 
37   virtual buzz::AsyncSocket::State state();
38   virtual buzz::AsyncSocket::Error error();
39   virtual int GetError();
40 
41   virtual bool Connect(const rtc::SocketAddress& addr);
42   virtual bool Read(char * data, size_t len, size_t* len_read);
43   virtual bool Write(const char * data, size_t len);
44   virtual bool Close();
45   virtual bool StartTls(const std::string & domainname);
46 
47   sigslot::signal1<int> SignalCloseEvent;
48 
49 private:
50   void CreateCricketSocket(int family);
51 #ifndef USE_SSLSTREAM
52   void OnReadEvent(rtc::AsyncSocket * socket);
53   void OnWriteEvent(rtc::AsyncSocket * socket);
54   void OnConnectEvent(rtc::AsyncSocket * socket);
55   void OnCloseEvent(rtc::AsyncSocket * socket, int error);
56 #else  // USE_SSLSTREAM
57   void OnEvent(rtc::StreamInterface* stream, int events, int err);
58 #endif  // USE_SSLSTREAM
59 
60   rtc::AsyncSocket * cricket_socket_;
61 #ifdef USE_SSLSTREAM
62   rtc::StreamInterface *stream_;
63 #endif  // USE_SSLSTREAM
64   buzz::AsyncSocket::State state_;
65   rtc::ByteBuffer buffer_;
66   buzz::TlsOptions tls_;
67 };
68 
69 }  // namespace buzz
70 
71 #endif // WEBRTC_LIBJINGLE_XMPP_XMPPSOCKET_H_
72 
73