• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /* The CFStream handle acts as an event synchronization entity for
20  * read/write/open/error/eos events happening on CFStream streams. */
21 
22 #ifndef GRPC_CORE_LIB_IOMGR_CFSTREAM_HANDLE_H
23 #define GRPC_CORE_LIB_IOMGR_CFSTREAM_HANDLE_H
24 
25 #include <grpc/support/port_platform.h>
26 
27 #include "src/core/lib/iomgr/port.h"
28 
29 #ifdef GRPC_CFSTREAM
30 #import <CoreFoundation/CoreFoundation.h>
31 
32 #include "src/core/lib/iomgr/closure.h"
33 #include "src/core/lib/iomgr/lockfree_event.h"
34 
35 class CFStreamHandle final {
36  public:
37   static CFStreamHandle* CreateStreamHandle(CFReadStreamRef read_stream,
38                                             CFWriteStreamRef write_stream);
39   ~CFStreamHandle();
40   CFStreamHandle(const CFReadStreamRef& ref) = delete;
41   CFStreamHandle(CFReadStreamRef&& ref) = delete;
42   CFStreamHandle& operator=(const CFStreamHandle& rhs) = delete;
43 
44   void NotifyOnOpen(grpc_closure* closure);
45   void NotifyOnRead(grpc_closure* closure);
46   void NotifyOnWrite(grpc_closure* closure);
47   void Shutdown(grpc_error* error);
48 
49   void Ref(const char* file = "", int line = 0, const char* reason = nullptr);
50   void Unref(const char* file = "", int line = 0, const char* reason = nullptr);
51 
52  private:
53   CFStreamHandle(CFReadStreamRef read_stream, CFWriteStreamRef write_stream);
54   static void ReadCallback(CFReadStreamRef stream, CFStreamEventType type,
55                            void* client_callback_info);
56   static void WriteCallback(CFWriteStreamRef stream, CFStreamEventType type,
57                             void* client_callback_info);
58   static void* Retain(void* info);
59   static void Release(void* info);
60 
61   grpc_core::LockfreeEvent open_event_;
62   grpc_core::LockfreeEvent read_event_;
63   grpc_core::LockfreeEvent write_event_;
64 
65   gpr_refcount refcount_;
66 };
67 
68 #ifdef DEBUG
69 #define CFSTREAM_HANDLE_REF(handle, reason) \
70   (handle)->Ref(__FILE__, __LINE__, (reason))
71 #define CFSTREAM_HANDLE_UNREF(handle, reason) \
72   (handle)->Unref(__FILE__, __LINE__, (reason))
73 #else
74 #define CFSTREAM_HANDLE_REF(handle, reason) (handle)->Ref()
75 #define CFSTREAM_HANDLE_UNREF(handle, reason) (handle)->Unref()
76 #endif
77 
78 #endif
79 
80 #endif /* GRPC_CORE_LIB_IOMGR_CFSTREAM_HANDLE_H */
81