1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! This file has been automatically generated by `objc2`'s `header-translator`.
//! DO NOT EDIT
use objc2::__framework_prelude::*;
use objc2_foundation::*;

use crate::*;

// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MCSessionSendDataMode(pub NSInteger);
impl MCSessionSendDataMode {
    pub const MCSessionSendDataReliable: Self = Self(0);
    pub const MCSessionSendDataUnreliable: Self = Self(1);
}

unsafe impl Encode for MCSessionSendDataMode {
    const ENCODING: Encoding = NSInteger::ENCODING;
}

unsafe impl RefEncode for MCSessionSendDataMode {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MCSessionState(pub NSInteger);
impl MCSessionState {
    #[doc(alias = "MCSessionStateNotConnected")]
    pub const NotConnected: Self = Self(0);
    #[doc(alias = "MCSessionStateConnecting")]
    pub const Connecting: Self = Self(1);
    #[doc(alias = "MCSessionStateConnected")]
    pub const Connected: Self = Self(2);
}

unsafe impl Encode for MCSessionState {
    const ENCODING: Encoding = NSInteger::ENCODING;
}

unsafe impl RefEncode for MCSessionState {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MCEncryptionPreference(pub NSInteger);
impl MCEncryptionPreference {
    pub const MCEncryptionOptional: Self = Self(0);
    pub const MCEncryptionRequired: Self = Self(1);
    pub const MCEncryptionNone: Self = Self(2);
}

unsafe impl Encode for MCEncryptionPreference {
    const ENCODING: Encoding = NSInteger::ENCODING;
}

unsafe impl RefEncode for MCEncryptionPreference {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

extern "C" {
    pub static kMCSessionMinimumNumberOfPeers: NSUInteger;
}

extern "C" {
    pub static kMCSessionMaximumNumberOfPeers: NSUInteger;
}

extern_class!(
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct MCSession;

    unsafe impl ClassType for MCSession {
        type Super = NSObject;
        type Mutability = InteriorMutable;
    }
);

unsafe impl NSObjectProtocol for MCSession {}

extern_methods!(
    unsafe impl MCSession {
        #[cfg(feature = "MCPeerID")]
        #[method_id(@__retain_semantics Init initWithPeer:)]
        pub unsafe fn initWithPeer(this: Allocated<Self>, my_peer_id: &MCPeerID) -> Retained<Self>;

        #[cfg(feature = "MCPeerID")]
        #[method_id(@__retain_semantics Init initWithPeer:securityIdentity:encryptionPreference:)]
        pub unsafe fn initWithPeer_securityIdentity_encryptionPreference(
            this: Allocated<Self>,
            my_peer_id: &MCPeerID,
            identity: Option<&NSArray>,
            encryption_preference: MCEncryptionPreference,
        ) -> Retained<Self>;

        #[cfg(feature = "MCPeerID")]
        #[method(sendData:toPeers:withMode:error:_)]
        pub unsafe fn sendData_toPeers_withMode_error(
            &self,
            data: &NSData,
            peer_i_ds: &NSArray<MCPeerID>,
            mode: MCSessionSendDataMode,
        ) -> Result<(), Retained<NSError>>;

        #[method(disconnect)]
        pub unsafe fn disconnect(&self);

        #[cfg(all(feature = "MCPeerID", feature = "block2"))]
        #[method_id(@__retain_semantics Other sendResourceAtURL:withName:toPeer:withCompletionHandler:)]
        pub unsafe fn sendResourceAtURL_withName_toPeer_withCompletionHandler(
            &self,
            resource_url: &NSURL,
            resource_name: &NSString,
            peer_id: &MCPeerID,
            completion_handler: Option<&block2::Block<dyn Fn(*mut NSError)>>,
        ) -> Option<Retained<NSProgress>>;

        #[cfg(feature = "MCPeerID")]
        #[method_id(@__retain_semantics Other startStreamWithName:toPeer:error:_)]
        pub unsafe fn startStreamWithName_toPeer_error(
            &self,
            stream_name: &NSString,
            peer_id: &MCPeerID,
        ) -> Result<Retained<NSOutputStream>, Retained<NSError>>;

        #[method_id(@__retain_semantics Other delegate)]
        pub unsafe fn delegate(&self) -> Option<Retained<ProtocolObject<dyn MCSessionDelegate>>>;

        #[method(setDelegate:)]
        pub unsafe fn setDelegate(&self, delegate: Option<&ProtocolObject<dyn MCSessionDelegate>>);

        #[cfg(feature = "MCPeerID")]
        #[method_id(@__retain_semantics Other myPeerID)]
        pub unsafe fn myPeerID(&self) -> Retained<MCPeerID>;

        #[method_id(@__retain_semantics Other securityIdentity)]
        pub unsafe fn securityIdentity(&self) -> Option<Retained<NSArray>>;

        #[method(encryptionPreference)]
        pub unsafe fn encryptionPreference(&self) -> MCEncryptionPreference;

        #[cfg(feature = "MCPeerID")]
        #[method_id(@__retain_semantics Other connectedPeers)]
        pub unsafe fn connectedPeers(&self) -> Retained<NSArray<MCPeerID>>;
    }
);

extern_methods!(
    /// Methods declared on superclass `NSObject`
    unsafe impl MCSession {
        #[method_id(@__retain_semantics Init init)]
        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;

        #[method_id(@__retain_semantics New new)]
        pub unsafe fn new() -> Retained<Self>;
    }
);

extern_protocol!(
    pub unsafe trait MCSessionDelegate: NSObjectProtocol {
        #[cfg(feature = "MCPeerID")]
        #[method(session:peer:didChangeState:)]
        unsafe fn session_peer_didChangeState(
            &self,
            session: &MCSession,
            peer_id: &MCPeerID,
            state: MCSessionState,
        );

        #[cfg(feature = "MCPeerID")]
        #[method(session:didReceiveData:fromPeer:)]
        unsafe fn session_didReceiveData_fromPeer(
            &self,
            session: &MCSession,
            data: &NSData,
            peer_id: &MCPeerID,
        );

        #[cfg(feature = "MCPeerID")]
        #[method(session:didReceiveStream:withName:fromPeer:)]
        unsafe fn session_didReceiveStream_withName_fromPeer(
            &self,
            session: &MCSession,
            stream: &NSInputStream,
            stream_name: &NSString,
            peer_id: &MCPeerID,
        );

        #[cfg(feature = "MCPeerID")]
        #[method(session:didStartReceivingResourceWithName:fromPeer:withProgress:)]
        unsafe fn session_didStartReceivingResourceWithName_fromPeer_withProgress(
            &self,
            session: &MCSession,
            resource_name: &NSString,
            peer_id: &MCPeerID,
            progress: &NSProgress,
        );

        #[cfg(feature = "MCPeerID")]
        #[method(session:didFinishReceivingResourceWithName:fromPeer:atURL:withError:)]
        unsafe fn session_didFinishReceivingResourceWithName_fromPeer_atURL_withError(
            &self,
            session: &MCSession,
            resource_name: &NSString,
            peer_id: &MCPeerID,
            local_url: Option<&NSURL>,
            error: Option<&NSError>,
        );

        #[cfg(all(feature = "MCPeerID", feature = "block2"))]
        #[optional]
        #[method(session:didReceiveCertificate:fromPeer:certificateHandler:)]
        unsafe fn session_didReceiveCertificate_fromPeer_certificateHandler(
            &self,
            session: &MCSession,
            certificate: Option<&NSArray>,
            peer_id: &MCPeerID,
            certificate_handler: &block2::Block<dyn Fn(Bool)>,
        );
    }

    unsafe impl ProtocolType for dyn MCSessionDelegate {}
);

extern_methods!(
    /// MCSessionCustomDiscovery
    unsafe impl MCSession {
        #[cfg(all(feature = "MCPeerID", feature = "block2"))]
        #[method(nearbyConnectionDataForPeer:withCompletionHandler:)]
        pub unsafe fn nearbyConnectionDataForPeer_withCompletionHandler(
            &self,
            peer_id: &MCPeerID,
            completion_handler: &block2::Block<dyn Fn(*mut NSData, *mut NSError)>,
        );

        #[cfg(feature = "MCPeerID")]
        #[method(connectPeer:withNearbyConnectionData:)]
        pub unsafe fn connectPeer_withNearbyConnectionData(
            &self,
            peer_id: &MCPeerID,
            data: &NSData,
        );

        #[cfg(feature = "MCPeerID")]
        #[method(cancelConnectPeer:)]
        pub unsafe fn cancelConnectPeer(&self, peer_id: &MCPeerID);
    }
);