| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_ |
| 6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | |
| 11 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 12 | 11 |
| 13 namespace net { | 12 namespace net { |
| 14 class IOBuffer; | 13 |
| 14 class DrainableIOBuffer; |
| 15 class GrowableIOBuffer; |
| 16 |
| 15 } // namespace net | 17 } // namespace net |
| 16 | 18 |
| 17 namespace device { | 19 namespace device { |
| 18 | 20 |
| 19 // BluetoothSocket represents a socket to a specific service on a | 21 // BluetoothSocket represents a socket to a specific service on a |
| 20 // BluetoothDevice. BluetoothSocket objects are ref counted and may outlive | 22 // BluetoothDevice. BluetoothSocket objects are ref counted and may outlive |
| 21 // both the BluetoothDevice and BluetoothAdapter that were involved in their | 23 // both the BluetoothDevice and BluetoothAdapter that were involved in their |
| 22 // creation. In terms of threading, platform specific implementations may | 24 // creation. |
| 23 // differ slightly, but platform independent consumers must guarantee calling | 25 class BluetoothSocket : public base::RefCounted<BluetoothSocket> { |
| 24 // various instances methods on the same thread as the thread used at | |
| 25 // construction time -- platform specific implementation are resonsible for | |
| 26 // marshalling calls to a different thread if required. | |
| 27 class BluetoothSocket : public base::RefCountedThreadSafe<BluetoothSocket> { | |
| 28 public: | 26 public: |
| 29 enum ErrorReason { kSystemError, kIOPending, kDisconnected }; | 27 // Receives data from the socket and stores it in |buffer|. It returns whether |
| 28 // the reception has been successful. If it fails, the caller can get the |
| 29 // error message through |GetLastErrorMessage()|. |
| 30 virtual bool Receive(net::GrowableIOBuffer* buffer) = 0; |
| 30 | 31 |
| 31 typedef base::Callback<void(int)> SendCompletionCallback; | 32 // Sends |buffer| to the socket. It returns whether the sending has been |
| 32 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> | 33 // successful. If it fails, the caller can get the error message through |
| 33 ReceiveCompletionCallback; | 34 // |GetLastErrorMessage()|. |
| 34 typedef base::Callback<void(const std::string& error_message)> | 35 virtual bool Send(net::DrainableIOBuffer* buffer) = 0; |
| 35 ErrorCompletionCallback; | |
| 36 typedef base::Callback<void(ErrorReason, const std::string& error_message)> | |
| 37 ReceiveErrorCompletionCallback; | |
| 38 | 36 |
| 39 // Destroys resources associated with the socket. After calling this method, | 37 virtual std::string GetLastErrorMessage() const = 0; |
| 40 // it is illegal to call any method on this socket instance (except for the | |
| 41 // desctrutor via Release). | |
| 42 virtual void Close() = 0; | |
| 43 | |
| 44 // Gracefully disconnects the socket and calls |callback| upon completion. | |
| 45 // There is no failure case, as this is a best effort operation. | |
| 46 virtual void Disconnect(const base::Closure& callback) = 0; | |
| 47 | |
| 48 // Receives data from the socket and calls |success_callback| when data is | |
| 49 // available. |count| is maximum amount of bytes received. If an error occurs, | |
| 50 // calls |error_callback| with a reason and an error message. | |
| 51 virtual void Receive( | |
| 52 int count, | |
| 53 const ReceiveCompletionCallback& success_callback, | |
| 54 const ReceiveErrorCompletionCallback& error_callback) = 0; | |
| 55 | |
| 56 // Sends |buffer| to the socket and calls |success_callback| when data has | |
| 57 // been successfully sent. |buffer_size| is the number of bytes contained in | |
| 58 // |buffer|. If an error occurs, calls |error_callback| with an error message. | |
| 59 virtual void Send(scoped_refptr<net::IOBuffer> buffer, | |
| 60 int buffer_size, | |
| 61 const SendCompletionCallback& success_callback, | |
| 62 const ErrorCompletionCallback& error_callback) = 0; | |
| 63 | 38 |
| 64 protected: | 39 protected: |
| 65 friend class base::RefCountedThreadSafe<BluetoothSocket>; | 40 friend class base::RefCounted<BluetoothSocket>; |
| 66 virtual ~BluetoothSocket(); | 41 virtual ~BluetoothSocket() {} |
| 67 }; | 42 }; |
| 68 | 43 |
| 69 } // namespace device | 44 } // namespace device |
| 70 | 45 |
| 71 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_ | 46 #endif // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_H_ |
| OLD | NEW |