1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UDP_TRANSCEIVER_H
6 #define ICE_UDP_TRANSCEIVER_H
7 
8 #include <IceUtil/Mutex.h>
9 
10 #include <Ice/ProtocolInstanceF.h>
11 #include <Ice/Transceiver.h>
12 #include <Ice/Network.h>
13 
14 #ifdef ICE_OS_UWP
15 #   include <deque>
16 #endif
17 
18 namespace IceInternal
19 {
20 
21 class UdpEndpoint;
22 
23 class UdpTransceiver : public Transceiver, public NativeInfo
24 {
25     enum State
26     {
27         StateNeedConnect,
28         StateConnectPending,
29         StateConnected,
upCast(UdpTransceiver * p)30         StateNotConnected
31     };
32 
getNativeInfo()33 public:
34 
35     virtual NativeInfoPtr getNativeInfo();
36 #if defined(ICE_USE_IOCP) || defined(ICE_OS_UWP)
37     virtual AsyncInfo* getAsyncInfo(SocketOperation);
38 #endif
39 
getAsyncInfo(SocketOperation status)40     virtual SocketOperation initialize(Buffer&, Buffer&);
41     virtual SocketOperation closing(bool, const Ice::LocalException&);
42     virtual void close();
43     virtual EndpointIPtr bind();
44     virtual SocketOperation write(Buffer&);
45     virtual SocketOperation read(Buffer&);
46 #if defined(ICE_USE_IOCP) || defined(ICE_OS_UWP)
47     virtual bool startWrite(Buffer&);
48     virtual void finishWrite(Buffer&);
49     virtual void startRead(Buffer&);
50     virtual void finishRead(Buffer&);
51 #endif
52     virtual std::string protocol() const;
53     virtual std::string toString() const;
54     virtual std::string toDetailedString() const;
55     virtual Ice::ConnectionInfoPtr getInfo() const;
56     virtual void checkSendSize(const Buffer&);
57     virtual void setBufferSize(int rcvSize, int sndSize);
58 
59     int effectivePort() const;
60 
initialize(Buffer &,Buffer &)61 private:
62 
63     UdpTransceiver(const ProtocolInstancePtr&, const Address&, const Address&, const std::string&, int);
64     UdpTransceiver(const UdpEndpointIPtr&, const ProtocolInstancePtr&, const std::string&, int, const std::string&,
65                    bool);
66 
67     virtual ~UdpTransceiver();
68 
69     void setBufSize(int, int);
70 
71 #ifdef ICE_OS_UWP
72     void appendMessage(Windows::Networking::Sockets::DatagramSocketMessageReceivedEventArgs^);
73     Windows::Networking::Sockets::DatagramSocketMessageReceivedEventArgs^ readMessage();
74 
75     void connectCompleted(Windows::Foundation::IAsyncAction^, Windows::Foundation::AsyncStatus);
76     void getOutputStreamMcastCompleted(
77         Windows::Foundation::IAsyncOperation<Windows::Storage::Streams::IOutputStream^>^,
78         Windows::Foundation::AsyncStatus);
79     void getOutputStreamCompleted(concurrency::task<Windows::Storage::Streams::IOutputStream^>, Buffer&);
80 #endif
81 
82     friend class UdpEndpointI;
83     friend class UdpConnector;
84 
85     UdpEndpointIPtr _endpoint;
86     const ProtocolInstancePtr _instance;
87     const bool _incoming;
88     bool _bound;
89 
90     const Address _addr;
91     Address _mcastAddr;
92     const std::string _mcastInterface;
93     Address _peerAddr;
94     int _port;
95 
closing(bool,const Ice::LocalException &)96     State _state;
97     int _rcvSize;
98     int _sndSize;
99     static const int _udpOverhead;
100     static const int _maxPacketSize;
101 
102 #if defined(ICE_USE_IOCP)
close()103     AsyncInfo _read;
104     AsyncInfo _write;
105     Address _readAddr;
106     socklen_t _readAddrLen;
107 #elif defined(ICE_OS_UWP)
108     AsyncInfo _write;
109     Windows::Storage::Streams::DataWriter^ _writer;
110     IceUtil::Mutex _mutex;
111     bool _readPending;
112     std::deque<Windows::Networking::Sockets::DatagramSocketMessageReceivedEventArgs^> _received;
113 #endif
114 };
115 
116 }
117 #endif
118