The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // |
| 18 | // Socket class. Modeled after Java classes. |
| 19 | // |
| 20 | #ifndef _RUNTIME_SOCKET_H |
| 21 | #define _RUNTIME_SOCKET_H |
| 22 | |
| 23 | #include <utils/inet_address.h> |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | /* |
| 29 | * Basic socket class, needed to abstract away the differences between |
| 30 | * BSD sockets and WinSock. This establishes a streaming network |
| 31 | * connection (TCP/IP) to somebody. |
| 32 | */ |
| 33 | class Socket { |
| 34 | public: |
| 35 | Socket(void); |
| 36 | ~Socket(void); |
| 37 | |
| 38 | // Create a connection to somewhere. |
| 39 | // Return 0 on success. |
| 40 | int connect(const char* host, int port); |
| 41 | int connect(const InetAddress* addr, int port); |
| 42 | |
| 43 | |
| 44 | // Close the socket. Don't try to use this object again after |
| 45 | // calling this. Returns false on failure. |
| 46 | bool close(void); |
| 47 | |
| 48 | // If we created the socket without an address, we can use these |
| 49 | // to finish the connection. Returns 0 on success. |
| 50 | int bind(const SocketAddress& bindPoint); |
| 51 | int connect(const SocketAddress& endPoint); |
| 52 | |
| 53 | // Here we deviate from the traditional object-oriented fanciness |
| 54 | // and just provide read/write operators instead of getters for |
| 55 | // objects that abstract a stream. |
| 56 | // |
| 57 | // Standard read/write semantics. |
| 58 | int read(void* buf, ssize_t len) const; |
| 59 | int write(const void* buf, ssize_t len) const; |
| 60 | |
| 61 | // This must be called once, at program startup. |
| 62 | static bool bootInit(void); |
| 63 | static void finalShutdown(void); |
| 64 | |
| 65 | private: |
| 66 | // Internal function that establishes a connection. |
| 67 | int doConnect(const InetSocketAddress& addr); |
| 68 | |
| 69 | unsigned long mSock; // holds SOCKET or int |
| 70 | |
| 71 | static bool mBootInitialized; |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | // debug -- unit tests |
| 76 | void TestSockets(void); |
| 77 | |
| 78 | }; // namespace android |
| 79 | |
| 80 | #endif // _RUNTIME_SOCKET_H |