| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#pragma once |
| 19 |
|
| 20 |
|
| 21 |
#include <pj/assert.h> |
| 22 |
#include <pj/sock.h> |
| 23 |
#include <pj/string.h> |
| 24 |
#include <pj/unicode.h> |
| 25 |
|
| 26 |
|
| 27 |
enum { |
| 28 |
READ_TIMEOUT = 60 * 1000, |
| 29 |
WRITE_TIMEOUT = 60 * 1000, |
| 30 |
SEND_BUFFER_SIZE = 128 * 1024, |
| 31 |
}; |
| 32 |
|
| 33 |
enum PjUwpSocketType { |
| 34 |
SOCKTYPE_UNKNOWN, SOCKTYPE_LISTENER, |
| 35 |
SOCKTYPE_STREAM, SOCKTYPE_DATAGRAM |
| 36 |
}; |
| 37 |
|
| 38 |
enum PjUwpSocketState { |
| 39 |
SOCKSTATE_NULL, SOCKSTATE_INITIALIZED, SOCKSTATE_CONNECTING, |
| 40 |
SOCKSTATE_CONNECTED, SOCKSTATE_DISCONNECTED, SOCKSTATE_ERROR |
| 41 |
}; |
| 42 |
|
| 43 |
ref class PjUwpSocketDatagramRecvHelper; |
| 44 |
ref class PjUwpSocketListenerHelper; |
| 45 |
class PjUwpSocket; |
| 46 |
|
| 47 |
|
| 48 |
typedef struct PjUwpSocketCallback |
| 49 |
{ |
| 50 |
void (*on_read)(PjUwpSocket *s, int bytes_read); |
| 51 |
void (*on_write)(PjUwpSocket *s, int bytes_sent); |
| 52 |
void (*on_accept)(PjUwpSocket *s); |
| 53 |
void (*on_connect)(PjUwpSocket *s, pj_status_t status); |
| 54 |
} PjUwpSocketCallback; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
class PjUwpSocket |
| 61 |
{ |
| 62 |
public: |
| 63 |
PjUwpSocket(int af_, int type_, int proto_); |
| 64 |
virtual ~PjUwpSocket(); |
| 65 |
pj_status_t InitSocket(enum PjUwpSocketType sock_type_); |
| 66 |
void DeinitSocket(); |
| 67 |
|
| 68 |
void* GetUserData() { return user_data; } |
| 69 |
void SetNonBlocking(const PjUwpSocketCallback *cb_, void *user_data_) |
| 70 |
{ |
| 71 |
is_blocking = PJ_FALSE; |
| 72 |
cb=*cb_; |
| 73 |
user_data = user_data_; |
| 74 |
} |
| 75 |
|
| 76 |
enum PjUwpSocketType GetType() { return sock_type; } |
| 77 |
enum PjUwpSocketState GetState() { return sock_state; } |
| 78 |
|
| 79 |
pj_sockaddr* GetLocalAddr() { return &local_addr; } |
| 80 |
pj_sockaddr* GetRemoteAddr() { return &remote_addr; } |
| 81 |
|
| 82 |
|
| 83 |
pj_status_t Bind(const pj_sockaddr_t *addr = NULL); |
| 84 |
pj_status_t Send(const void *buf, pj_ssize_t *len); |
| 85 |
pj_status_t SendTo(const void *buf, pj_ssize_t *len, const pj_sockaddr_t *to); |
| 86 |
pj_status_t Recv(void *buf, pj_ssize_t *len); |
| 87 |
pj_status_t RecvFrom(void *buf, pj_ssize_t *len, pj_sockaddr_t *from); |
| 88 |
pj_status_t Connect(const pj_sockaddr_t *addr); |
| 89 |
pj_status_t Listen(); |
| 90 |
pj_status_t Accept(PjUwpSocket **new_sock); |
| 91 |
|
| 92 |
void (*on_read)(PjUwpSocket *s, int bytes_read); |
| 93 |
void (*on_write)(PjUwpSocket *s, int bytes_sent); |
| 94 |
void (*on_accept)(PjUwpSocket *s, pj_status_t status); |
| 95 |
void (*on_connect)(PjUwpSocket *s, pj_status_t status); |
| 96 |
|
| 97 |
private: |
| 98 |
PjUwpSocket* CreateAcceptSocket(Windows::Networking::Sockets::StreamSocket^ stream_sock_); |
| 99 |
pj_status_t SendImp(const void *buf, pj_ssize_t *len); |
| 100 |
int ConsumeReadBuffer(void *buf, int max_len); |
| 101 |
|
| 102 |
int af; |
| 103 |
int type; |
| 104 |
int proto; |
| 105 |
pj_sockaddr local_addr; |
| 106 |
pj_sockaddr remote_addr; |
| 107 |
pj_bool_t is_blocking; |
| 108 |
pj_bool_t has_pending_bind; |
| 109 |
pj_bool_t has_pending_send; |
| 110 |
pj_bool_t has_pending_recv; |
| 111 |
void *user_data; |
| 112 |
PjUwpSocketCallback cb; |
| 113 |
|
| 114 |
enum PjUwpSocketType sock_type; |
| 115 |
enum PjUwpSocketState sock_state; |
| 116 |
Windows::Networking::Sockets::DatagramSocket^ datagram_sock; |
| 117 |
Windows::Networking::Sockets::StreamSocket^ stream_sock; |
| 118 |
Windows::Networking::Sockets::StreamSocketListener^ listener_sock; |
| 119 |
|
| 120 |
|
| 121 |
PjUwpSocketDatagramRecvHelper^ dgram_recv_helper; |
| 122 |
PjUwpSocketListenerHelper^ listener_helper; |
| 123 |
|
| 124 |
Windows::Storage::Streams::DataReader^ socket_reader; |
| 125 |
Windows::Storage::Streams::DataWriter^ socket_writer; |
| 126 |
Windows::Storage::Streams::IBuffer^ send_buffer; |
| 127 |
|
| 128 |
friend PjUwpSocketDatagramRecvHelper; |
| 129 |
friend PjUwpSocketListenerHelper; |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
inline pj_status_t wstr_addr_to_sockaddr(const wchar_t *waddr, |
| 138 |
const wchar_t *wport, |
| 139 |
pj_sockaddr_t *sockaddr) |
| 140 |
{ |
| 141 |
#if 0 |
| 142 |
char tmp_str_buf[PJ_INET6_ADDRSTRLEN+1]; |
| 143 |
pj_assert(wcslen(waddr) < sizeof(tmp_str_buf)); |
| 144 |
pj_unicode_to_ansi(waddr, wcslen(waddr), tmp_str_buf, sizeof(tmp_str_buf)); |
| 145 |
pj_str_t remote_host; |
| 146 |
pj_strset(&remote_host, tmp_str_buf, pj_ansi_strlen(tmp_str_buf)); |
| 147 |
pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &remote_host, (pj_sockaddr*)sockaddr); |
| 148 |
pj_sockaddr_set_port((pj_sockaddr*)sockaddr, (pj_uint16_t)_wtoi(wport)); |
| 149 |
|
| 150 |
return PJ_SUCCESS; |
| 151 |
#endif |
| 152 |
char tmp_str_buf[PJ_INET6_ADDRSTRLEN+1]; |
| 153 |
pj_assert(wcslen(waddr) < sizeof(tmp_str_buf)); |
| 154 |
pj_unicode_to_ansi(waddr, wcslen(waddr), tmp_str_buf, sizeof(tmp_str_buf)); |
| 155 |
pj_str_t remote_host; |
| 156 |
pj_strset(&remote_host, tmp_str_buf, pj_ansi_strlen(tmp_str_buf)); |
| 157 |
pj_sockaddr *addr = (pj_sockaddr*)sockaddr; |
| 158 |
pj_bool_t got_addr = PJ_FALSE; |
| 159 |
|
| 160 |
if (pj_inet_pton(PJ_AF_INET, &remote_host, &addr->ipv4.sin_addr) |
| 161 |
== PJ_SUCCESS) |
| 162 |
{ |
| 163 |
addr->addr.sa_family = PJ_AF_INET; |
| 164 |
got_addr = PJ_TRUE; |
| 165 |
} else if (pj_inet_pton(PJ_AF_INET6, &remote_host, &addr->ipv6.sin6_addr) |
| 166 |
== PJ_SUCCESS) |
| 167 |
{ |
| 168 |
addr->addr.sa_family = PJ_AF_INET6; |
| 169 |
got_addr = PJ_TRUE; |
| 170 |
} |
| 171 |
if (!got_addr) |
| 172 |
return PJ_EINVAL; |
| 173 |
|
| 174 |
pj_sockaddr_set_port(addr, (pj_uint16_t)_wtoi(wport)); |
| 175 |
return PJ_SUCCESS; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
inline pj_status_t sockaddr_to_hostname_port(const pj_sockaddr_t *sockaddr, |
| 180 |
Windows::Networking::HostName ^&hostname, |
| 181 |
int *port) |
| 182 |
{ |
| 183 |
char tmp[PJ_INET6_ADDRSTRLEN]; |
| 184 |
wchar_t wtmp[PJ_INET6_ADDRSTRLEN]; |
| 185 |
pj_sockaddr_print(sockaddr, tmp, PJ_INET6_ADDRSTRLEN, 0); |
| 186 |
pj_ansi_to_unicode(tmp, pj_ansi_strlen(tmp), wtmp, |
| 187 |
PJ_INET6_ADDRSTRLEN); |
| 188 |
hostname = ref new Windows::Networking::HostName(ref new Platform::String(wtmp)); |
| 189 |
*port = pj_sockaddr_get_port(sockaddr); |
| 190 |
|
| 191 |
return PJ_SUCCESS; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
#include <Robuffer.h> |
| 198 |
#include <wrl/client.h> |
| 199 |
|
| 200 |
inline Microsoft::WRL::ComPtr<Windows::Storage::Streams::IBufferByteAccess> GetBufferByteAccess(Windows::Storage::Streams::IBuffer^ buffer) |
| 201 |
{ |
| 202 |
auto pUnk = reinterpret_cast<IUnknown*>(buffer); |
| 203 |
|
| 204 |
Microsoft::WRL::ComPtr<Windows::Storage::Streams::IBufferByteAccess> comBuff; |
| 205 |
pUnk->QueryInterface(__uuidof(Windows::Storage::Streams::IBufferByteAccess), (void**)comBuff.ReleaseAndGetAddressOf()); |
| 206 |
|
| 207 |
return comBuff; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
inline void GetRawBufferFromIBuffer(Windows::Storage::Streams::IBuffer^ buffer, unsigned char** pbuffer) |
| 212 |
{ |
| 213 |
Platform::Object^ obj = buffer; |
| 214 |
Microsoft::WRL::ComPtr<IInspectable> insp(reinterpret_cast<IInspectable*>(obj)); |
| 215 |
Microsoft::WRL::ComPtr<Windows::Storage::Streams::IBufferByteAccess> bufferByteAccess; |
| 216 |
insp.As(&bufferByteAccess); |
| 217 |
bufferByteAccess->Buffer(pbuffer); |
| 218 |
} |
| 219 |
|
| 220 |
inline void CopyToIBuffer(unsigned char* buffSource, unsigned int copyByteCount, Windows::Storage::Streams::IBuffer^ buffer, unsigned int writeStartPos = 0) |
| 221 |
{ |
| 222 |
auto bufferLen = buffer->Capacity; |
| 223 |
assert(copyByteCount <= bufferLen); |
| 224 |
|
| 225 |
unsigned char* pBuffer; |
| 226 |
|
| 227 |
GetRawBufferFromIBuffer(buffer, &pBuffer); |
| 228 |
|
| 229 |
memcpy(pBuffer + writeStartPos, buffSource, copyByteCount); |
| 230 |
} |
| 231 |
|
| 232 |
inline void CopyFromIBuffer(unsigned char* buffDestination, unsigned int copyByteCount, Windows::Storage::Streams::IBuffer^ buffer, unsigned int readStartPos = 0) |
| 233 |
{ |
| 234 |
assert(copyByteCount <= buffer->Capacity); |
| 235 |
|
| 236 |
unsigned char* pBuffer; |
| 237 |
|
| 238 |
GetRawBufferFromIBuffer(buffer, &pBuffer); |
| 239 |
|
| 240 |
memcpy(buffDestination, pBuffer + readStartPos, copyByteCount); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
#include <ppltasks.h> |
| 247 |
#include <agents.h> |
| 248 |
|
| 249 |
|
| 250 |
inline concurrency::task<void> complete_after(unsigned int timeout) |
| 251 |
{ |
| 252 |
|
| 253 |
concurrency::task_completion_event<void> tce; |
| 254 |
|
| 255 |
|
| 256 |
auto fire_once = new concurrency::timer<int>(timeout, 0, nullptr, false); |
| 257 |
|
| 258 |
auto callback = new concurrency::call<int>([tce](int) |
| 259 |
{ |
| 260 |
tce.set(); |
| 261 |
}); |
| 262 |
|
| 263 |
|
| 264 |
fire_once->link_target(callback); |
| 265 |
fire_once->start(); |
| 266 |
|
| 267 |
|
| 268 |
concurrency::task<void> event_set(tce); |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
return event_set.then([callback, fire_once]() |
| 273 |
{ |
| 274 |
delete callback; |
| 275 |
delete fire_once; |
| 276 |
}); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
template<typename T> |
| 282 |
inline concurrency::task<T> cancel_after_timeout(concurrency::task<T> t, concurrency::cancellation_token_source cts, unsigned int timeout) |
| 283 |
{ |
| 284 |
|
| 285 |
concurrency::task<bool> success_task = t.then([](T) |
| 286 |
{ |
| 287 |
return true; |
| 288 |
}); |
| 289 |
|
| 290 |
concurrency::task<bool> failure_task = complete_after(timeout).then([] |
| 291 |
{ |
| 292 |
return false; |
| 293 |
}); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
return (failure_task || success_task).then([t, cts](bool success) |
| 298 |
{ |
| 299 |
if (!success) |
| 300 |
{ |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
cts.cancel(); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
return t; |
| 309 |
}); |
| 310 |
} |