diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:31:44 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:31:44 -0800 |
commit | 9066cfe9886ac131c34d59ed0e2d287b0e3c0087 (patch) | |
tree | d88beb88001f2482911e3d28e43833b50e4b4e97 /include/utils/Socket.h | |
parent | d83a98f4ce9cfa908f5c54bbd70f03eec07e7553 (diff) | |
download | frameworks_base-9066cfe9886ac131c34d59ed0e2d287b0e3c0087.zip frameworks_base-9066cfe9886ac131c34d59ed0e2d287b0e3c0087.tar.gz frameworks_base-9066cfe9886ac131c34d59ed0e2d287b0e3c0087.tar.bz2 |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'include/utils/Socket.h')
-rw-r--r-- | include/utils/Socket.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/include/utils/Socket.h b/include/utils/Socket.h new file mode 100644 index 0000000..8b7f406 --- /dev/null +++ b/include/utils/Socket.h @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2005 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// +// Socket class. Modeled after Java classes. +// +#ifndef _RUNTIME_SOCKET_H +#define _RUNTIME_SOCKET_H + +#include <utils/inet_address.h> +#include <sys/types.h> + +namespace android { + +/* + * Basic socket class, needed to abstract away the differences between + * BSD sockets and WinSock. This establishes a streaming network + * connection (TCP/IP) to somebody. + */ +class Socket { +public: + Socket(void); + ~Socket(void); + + // Create a connection to somewhere. + // Return 0 on success. + int connect(const char* host, int port); + int connect(const InetAddress* addr, int port); + + + // Close the socket. Don't try to use this object again after + // calling this. Returns false on failure. + bool close(void); + + // If we created the socket without an address, we can use these + // to finish the connection. Returns 0 on success. + int bind(const SocketAddress& bindPoint); + int connect(const SocketAddress& endPoint); + + // Here we deviate from the traditional object-oriented fanciness + // and just provide read/write operators instead of getters for + // objects that abstract a stream. + // + // Standard read/write semantics. + int read(void* buf, ssize_t len) const; + int write(const void* buf, ssize_t len) const; + + // This must be called once, at program startup. + static bool bootInit(void); + static void finalShutdown(void); + +private: + // Internal function that establishes a connection. + int doConnect(const InetSocketAddress& addr); + + unsigned long mSock; // holds SOCKET or int + + static bool mBootInitialized; +}; + + +// debug -- unit tests +void TestSockets(void); + +}; // namespace android + +#endif // _RUNTIME_SOCKET_H |