1 /* 2 * Copyright (C) 2010 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 #ifndef NET_FD_H_included 18 #define NET_FD_H_included 19 20 /** 21 * Wraps access to the int inside a java.io.FileDescriptor, taking care of throwing exceptions. 22 */ 23 class NetFd { 24 public: NetFd(JNIEnv * env,jobject fileDescriptor)25 NetFd(JNIEnv* env, jobject fileDescriptor) 26 : mEnv(env), mFileDescriptor(fileDescriptor), mFd(-1) 27 { 28 } 29 isClosed()30 bool isClosed() { 31 mFd = jniGetFDFromFileDescriptor(mEnv, mFileDescriptor); 32 bool closed = (mFd == -1); 33 if (closed) { 34 jniThrowException(mEnv, "java/net/SocketException", "Socket closed"); 35 } 36 return closed; 37 } 38 get()39 int get() const { 40 return mFd; 41 } 42 43 private: 44 JNIEnv* mEnv; 45 jobject mFileDescriptor; 46 int mFd; 47 48 // Disallow copy and assignment. 49 NetFd(const NetFd&); 50 void operator=(const NetFd&); 51 }; 52 53 /** 54 * Used to retry syscalls that can return EINTR. This differs from TEMP_FAILURE_RETRY in that 55 * it also considers the case where the reason for failure is that another thread called 56 * Socket.close. 57 */ 58 #define NET_FAILURE_RETRY(fd, exp) ({ \ 59 typeof (exp) _rc; \ 60 do { \ 61 _rc = (exp); \ 62 if (_rc == -1) { \ 63 if (fd.isClosed() || errno != EINTR) { \ 64 break; \ 65 } \ 66 } \ 67 } while (_rc == -1); \ 68 _rc; }) 69 70 #endif // NET_FD_H_included 71