| 1 | /* |
| 2 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 3 | * Copyright (C) 2003-2008 Benny Prijono <[email protected]> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include <pj/ioqueue.h> |
| 20 | #include <pj/assert.h> |
| 21 | #include <pj/errno.h> |
| 22 | #include <pj/list.h> |
| 23 | #include <pj/lock.h> |
| 24 | #include <pj/pool.h> |
| 25 | #include <pj/string.h> |
| 26 | |
| 27 | |
| 28 | /* Only build when the backend is using Symbian. */ |
| 29 | #if PJ_IOQUEUE_IMP == PJ_IOQUEUE_IMP_SYMBIAN |
| 30 | |
| 31 | |
| 32 | #include "os_symbian.h" |
| 33 | |
| 34 | class CIoqueueCallback; |
| 35 | |
| 36 | /* |
| 37 | * IO Queue structure. |
| 38 | */ |
| 39 | struct pj_ioqueue_t |
| 40 | { |
| 41 | int eventCount; |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | ///////////////////////////////////////////////////////////////////////////// |
| 46 | // Class to encapsulate asynchronous socket operation. |
| 47 | // |
| 48 | class CIoqueueCallback : public CActive |
| 49 | { |
| 50 | public: |
| 51 | static CIoqueueCallback* NewL(pj_ioqueue_t *ioqueue, |
| 52 | pj_ioqueue_key_t *key, |
| 53 | pj_sock_t sock, |
| 54 | const pj_ioqueue_callback *cb, |
| 55 | void *user_data); |
| 56 | |
| 57 | // |
| 58 | // Start asynchronous recv() operation |
| 59 | // |
| 60 | pj_status_t StartRead(pj_ioqueue_op_key_t *op_key, |
| 61 | void *buf, pj_ssize_t *size, unsigned flags, |
| 62 | pj_sockaddr_t *addr, int *addrlen); |
| 63 | |
| 64 | // |
| 65 | // Start asynchronous accept() operation. |
| 66 | // |
| 67 | pj_status_t StartAccept(pj_ioqueue_op_key_t *op_key, |
| 68 | pj_sock_t *new_sock, |
| 69 | pj_sockaddr_t *local, |
| 70 | pj_sockaddr_t *remote, |
| 71 | int *addrlen ); |
| 72 | |
| 73 | // |
| 74 | // Completion callback. |
| 75 | // |
| 76 | void RunL(); |
| 77 | |
| 78 | // |
| 79 | // CActive's DoCancel() |
| 80 | // |
| 81 | void DoCancel(); |
| 82 | |
| 83 | // |
| 84 | // Cancel operation and call callback. |
| 85 | // |
| 86 | void CancelOperation(pj_ioqueue_op_key_t *op_key, |
| 87 | pj_ssize_t bytes_status); |
| 88 | |
| 89 | // |
| 90 | // Accessors |
| 91 | // |
| 92 | void* get_user_data() const |
| 93 | { |
| 94 | return user_data_; |
| 95 | } |
| 96 | void set_user_data(void *user_data) |
| 97 | { |
| 98 | user_data_ = user_data; |
| 99 | } |
| 100 | pj_ioqueue_op_key_t *get_op_key() const |
| 101 | { |
| 102 | return pending_data_.common_.op_key_; |
| 103 | } |
| 104 | CPjSocket* get_pj_socket() |
| 105 | { |
| 106 | return sock_; |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | // Type of pending operation. |
| 111 | enum Type { |
| 112 | TYPE_NONE, |
| 113 | TYPE_READ, |
| 114 | TYPE_ACCEPT, |
| 115 | }; |
| 116 | |
| 117 | // Static data. |
| 118 | pj_ioqueue_t *ioqueue_; |
| 119 | pj_ioqueue_key_t *key_; |
| 120 | CPjSocket *sock_; |
| 121 | pj_ioqueue_callback cb_; |
| 122 | void *user_data_; |
| 123 | |
| 124 | // Symbian data. |
| 125 | TPtr8 aBufferPtr_; |
| 126 | TInetAddr aAddress_; |
| 127 | |
| 128 | // Application data. |
| 129 | Type type_; |
| 130 | |
| 131 | union Pending_Data |
| 132 | { |
| 133 | struct Common |
| 134 | { |
| 135 | pj_ioqueue_op_key_t *op_key_; |
| 136 | } common_; |
| 137 | |
| 138 | struct Pending_Read |
| 139 | { |
| 140 | pj_ioqueue_op_key_t *op_key_; |
| 141 | pj_sockaddr_t *addr_; |
| 142 | int *addrlen_; |
| 143 | } read_; |
| 144 | |
| 145 | struct Pending_Accept |
| 146 | { |
| 147 | pj_ioqueue_op_key_t *op_key_; |
| 148 | pj_sock_t *new_sock_; |
| 149 | pj_sockaddr_t *local_; |
| 150 | pj_sockaddr_t *remote_; |
| 151 | int *addrlen_; |
| 152 | } accept_; |
| 153 | }; |
| 154 | |
| 155 | union Pending_Data pending_data_; |
| 156 | RSocket blank_sock_; |
| 157 | |
| 158 | CIoqueueCallback(pj_ioqueue_t *ioqueue, |
| 159 | pj_ioqueue_key_t *key, pj_sock_t sock, |
| 160 | const pj_ioqueue_callback *cb, void *user_data) |
| 161 | : CActive(CActive::EPriorityStandard), |
| 162 | ioqueue_(ioqueue), key_(key), sock_((CPjSocket*)sock), |
| 163 | user_data_(user_data), aBufferPtr_(NULL, 0), type_(TYPE_NONE) |
| 164 | { |
| 165 | pj_memcpy(&cb_, cb, sizeof(*cb)); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void ConstructL() |
| 170 | { |
| 171 | CActiveScheduler::Add(this); |
| 172 | } |
| 173 | |
| 174 | void HandleReadCompletion(); |
| 175 | CPjSocket *HandleAcceptCompletion(); |
| 176 | }; |
| 177 | |
| 178 | |
| 179 | CIoqueueCallback* CIoqueueCallback::NewL(pj_ioqueue_t *ioqueue, |
| 180 | pj_ioqueue_key_t *key, |
| 181 | pj_sock_t sock, |
| 182 | const pj_ioqueue_callback *cb, |
| 183 | void *user_data) |
| 184 | { |
| 185 | CIoqueueCallback *self = new CIoqueueCallback(ioqueue, key, sock, |
| 186 | cb, user_data); |
| 187 | CleanupStack::PushL(self); |
| 188 | self->ConstructL(); |
| 189 | CleanupStack::Pop(self); |
| 190 | |
| 191 | return self; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | // |
| 196 | // Start asynchronous recv() operation |
| 197 | // |
| 198 | pj_status_t CIoqueueCallback::StartRead(pj_ioqueue_op_key_t *op_key, |
| 199 | void *buf, pj_ssize_t *size, |
| 200 | unsigned flags, |
| 201 | pj_sockaddr_t *addr, int *addrlen) |
| 202 | { |
| 203 | PJ_ASSERT_RETURN(IsActive()==false, PJ_EBUSY); |
| 204 | PJ_ASSERT_RETURN(pending_data_.common_.op_key_==NULL, PJ_EBUSY); |
| 205 | |
| 206 | flags &= ~PJ_IOQUEUE_ALWAYS_ASYNC; |
| 207 | |
| 208 | pending_data_.read_.op_key_ = op_key; |
| 209 | pending_data_.read_.addr_ = addr; |
| 210 | pending_data_.read_.addrlen_ = addrlen; |
| 211 | |
| 212 | aBufferPtr_.Set((TUint8*)buf, 0, (TInt)*size); |
| 213 | |
| 214 | type_ = TYPE_READ; |
| 215 | if (addr && addrlen) { |
| 216 | sock_->Socket().RecvFrom(aBufferPtr_, aAddress_, flags, iStatus); |
| 217 | } else { |
| 218 | aAddress_.SetAddress(0); |
| 219 | aAddress_.SetPort(0); |
| 220 | |
| 221 | if (sock_->IsDatagram()) { |
| 222 | sock_->Socket().Recv(aBufferPtr_, flags, iStatus); |
| 223 | } else { |
| 224 | // Using static like this is not pretty, but we don't need to use |
| 225 | // the value anyway, hence doing it like this is probably most |
| 226 | // optimal. |
| 227 | static TSockXfrLength len; |
| 228 | sock_->Socket().RecvOneOrMore(aBufferPtr_, flags, iStatus, len); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | SetActive(); |
| 233 | return PJ_EPENDING; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | // |
| 238 | // Start asynchronous accept() operation. |
| 239 | // |
| 240 | pj_status_t CIoqueueCallback::StartAccept(pj_ioqueue_op_key_t *op_key, |
| 241 | pj_sock_t *new_sock, |
| 242 | pj_sockaddr_t *local, |
| 243 | pj_sockaddr_t *remote, |
| 244 | int *addrlen ) |
| 245 | { |
| 246 | PJ_ASSERT_RETURN(IsActive()==false, PJ_EBUSY); |
| 247 | PJ_ASSERT_RETURN(pending_data_.common_.op_key_==NULL, PJ_EBUSY); |
| 248 | |
| 249 | // addrlen must be specified if local or remote is specified |
| 250 | PJ_ASSERT_RETURN((!local && !remote) || |
| 251 | (addrlen && *addrlen), PJ_EINVAL); |
| 252 | |
| 253 | pending_data_.accept_.op_key_ = op_key; |
| 254 | pending_data_.accept_.new_sock_ = new_sock; |
| 255 | pending_data_.accept_.local_ = local; |
| 256 | pending_data_.accept_.remote_ = remote; |
| 257 | pending_data_.accept_.addrlen_ = addrlen; |
| 258 | |
| 259 | // Create blank socket |
| 260 | blank_sock_.Open(PjSymbianOS::Instance()->SocketServ()); |
| 261 | |
| 262 | type_ = TYPE_ACCEPT; |
| 263 | sock_->Socket().Accept(blank_sock_, iStatus); |
| 264 | |
| 265 | SetActive(); |
| 266 | return PJ_EPENDING; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | // |
| 271 | // Handle asynchronous RecvFrom() completion |
| 272 | // |
| 273 | void CIoqueueCallback::HandleReadCompletion() |
| 274 | { |
| 275 | if (pending_data_.read_.addr_ && pending_data_.read_.addrlen_) { |
| 276 | PjSymbianOS::Addr2pj(aAddress_, |
| 277 | *(pj_sockaddr*)pending_data_.read_.addr_, |
| 278 | pending_data_.read_.addrlen_); |
| 279 | pending_data_.read_.addr_ = NULL; |
| 280 | pending_data_.read_.addrlen_ = NULL; |
| 281 | } |
| 282 | |
| 283 | pending_data_.read_.op_key_ = NULL; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | // |
| 288 | // Handle asynchronous Accept() completion. |
| 289 | // |
| 290 | CPjSocket *CIoqueueCallback::HandleAcceptCompletion() |
| 291 | { |
| 292 | CPjSocket *pjNewSock = new CPjSocket(get_pj_socket()->GetAf(), |
| 293 | get_pj_socket()->GetSockType(), |
| 294 | blank_sock_); |
| 295 | int addrlen = 0; |
| 296 | |
| 297 | if (pending_data_.accept_.new_sock_) { |
| 298 | *pending_data_.accept_.new_sock_ = (pj_sock_t)pjNewSock; |
| 299 | pending_data_.accept_.new_sock_ = NULL; |
| 300 | } |
| 301 | |
| 302 | if (pending_data_.accept_.local_) { |
| 303 | TInetAddr aAddr; |
| 304 | pj_sockaddr *ptr_sockaddr; |
| 305 | |
| 306 | blank_sock_.LocalName(aAddr); |
| 307 | ptr_sockaddr = (pj_sockaddr*)pending_data_.accept_.local_; |
| 308 | addrlen = *pending_data_.accept_.addrlen_; |
| 309 | PjSymbianOS::Addr2pj(aAddr, *ptr_sockaddr, &addrlen); |
| 310 | pending_data_.accept_.local_ = NULL; |
| 311 | } |
| 312 | |
| 313 | if (pending_data_.accept_.remote_) { |
| 314 | TInetAddr aAddr; |
| 315 | pj_sockaddr *ptr_sockaddr; |
| 316 | |
| 317 | blank_sock_.RemoteName(aAddr); |
| 318 | ptr_sockaddr = (pj_sockaddr*)pending_data_.accept_.remote_; |
| 319 | addrlen = *pending_data_.accept_.addrlen_; |
| 320 | PjSymbianOS::Addr2pj(aAddr, *ptr_sockaddr, &addrlen); |
| 321 | pending_data_.accept_.remote_ = NULL; |
| 322 | } |
| 323 | |
| 324 | if (pending_data_.accept_.addrlen_) { |
| 325 | if (addrlen == 0) { |
| 326 | if (pjNewSock->GetAf() == PJ_AF_INET) |
| 327 | addrlen = sizeof(pj_sockaddr_in); |
| 328 | else if (pjNewSock->GetAf() == PJ_AF_INET6) |
| 329 | addrlen = sizeof(pj_sockaddr_in6); |
| 330 | else { |
| 331 | pj_assert(!"Unsupported address family"); |
| 332 | } |
| 333 | } |
| 334 | *pending_data_.accept_.addrlen_ = addrlen; |
| 335 | pending_data_.accept_.addrlen_ = NULL; |
| 336 | } |
| 337 | |
| 338 | return pjNewSock; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | // |
| 343 | // Completion callback. |
| 344 | // |
| 345 | void CIoqueueCallback::RunL() |
| 346 | { |
| 347 | pj_ioqueue_t *ioq = ioqueue_; |
| 348 | Type cur_type = type_; |
| 349 | |
| 350 | type_ = TYPE_NONE; |
| 351 | |
| 352 | if (cur_type == TYPE_READ) { |
| 353 | // |
| 354 | // Completion of asynchronous RecvFrom() |
| 355 | // |
| 356 | |
| 357 | /* Clear op_key (save it to temp variable first!) */ |
| 358 | pj_ioqueue_op_key_t *op_key = pending_data_.read_.op_key_; |
| 359 | pending_data_.read_.op_key_ = NULL; |
| 360 | |
| 361 | // Handle failure condition |
| 362 | if (iStatus != KErrNone) { |
| 363 | if (cb_.on_read_complete) { |
| 364 | cb_.on_read_complete( key_, op_key, |
| 365 | -PJ_RETURN_OS_ERROR(iStatus.Int())); |
| 366 | } |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | HandleReadCompletion(); |
| 371 | |
| 372 | /* Call callback */ |
| 373 | if (cb_.on_read_complete) { |
| 374 | cb_.on_read_complete(key_, op_key, aBufferPtr_.Length()); |
| 375 | } |
| 376 | |
| 377 | } else if (cur_type == TYPE_ACCEPT) { |
| 378 | // |
| 379 | // Completion of asynchronous Accept() |
| 380 | // |
| 381 | |
| 382 | /* Clear op_key (save it to temp variable first!) */ |
| 383 | pj_ioqueue_op_key_t *op_key = pending_data_.read_.op_key_; |
| 384 | pending_data_.read_.op_key_ = NULL; |
| 385 | |
| 386 | // Handle failure condition |
| 387 | if (iStatus != KErrNone) { |
| 388 | if (pending_data_.accept_.new_sock_) |
| 389 | *pending_data_.accept_.new_sock_ = PJ_INVALID_SOCKET; |
| 390 | |
| 391 | if (cb_.on_accept_complete) { |
| 392 | cb_.on_accept_complete( key_, op_key, PJ_INVALID_SOCKET, |
| 393 | -PJ_RETURN_OS_ERROR(iStatus.Int())); |
| 394 | } |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | CPjSocket *pjNewSock = HandleAcceptCompletion(); |
| 399 | |
| 400 | // Call callback. |
| 401 | if (cb_.on_accept_complete) { |
| 402 | cb_.on_accept_complete( key_, op_key, (pj_sock_t)pjNewSock, |
| 403 | PJ_SUCCESS); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | ioq->eventCount++; |
| 408 | } |
| 409 | |
| 410 | // |
| 411 | // CActive's DoCancel() |
| 412 | // |
| 413 | void CIoqueueCallback::DoCancel() |
| 414 | { |
| 415 | if (type_ == TYPE_READ) |
| 416 | sock_->Socket().CancelRecv(); |
| 417 | else if (type_ == TYPE_ACCEPT) |
| 418 | sock_->Socket().CancelAccept(); |
| 419 | |
| 420 | type_ = TYPE_NONE; |
| 421 | pending_data_.common_.op_key_ = NULL; |
| 422 | } |
| 423 | |
| 424 | // |
| 425 | // Cancel operation and call callback. |
| 426 | // |
| 427 | void CIoqueueCallback::CancelOperation(pj_ioqueue_op_key_t *op_key, |
| 428 | pj_ssize_t bytes_status) |
| 429 | { |
| 430 | Type cur_type = type_; |
| 431 | |
| 432 | pj_assert(op_key == pending_data_.common_.op_key_); |
| 433 | |
| 434 | Cancel(); |
| 435 | |
| 436 | if (cur_type == TYPE_READ) { |
| 437 | if (cb_.on_read_complete) |
| 438 | cb_.on_read_complete(key_, op_key, bytes_status); |
| 439 | } else if (cur_type == TYPE_ACCEPT) |
| 440 | ; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | ///////////////////////////////////////////////////////////////////////////// |
| 445 | /* |
| 446 | * IO Queue key structure. |
| 447 | */ |
| 448 | struct pj_ioqueue_key_t |
| 449 | { |
| 450 | CIoqueueCallback *cbObj; |
| 451 | }; |
| 452 | |
| 453 | |
| 454 | /* |
| 455 | * Return the name of the ioqueue implementation. |
| 456 | */ |
| 457 | PJ_DEF(const char*) pj_ioqueue_name(void) |
| 458 | { |
| 459 | return "ioqueue-symbian"; |
| 460 | } |
| 461 | |
| 462 | |
| 463 | /* |
| 464 | * Create a new I/O Queue framework. |
| 465 | */ |
| 466 | PJ_DEF(pj_status_t) pj_ioqueue_create( pj_pool_t *pool, |
| 467 | pj_size_t max_fd, |
| 468 | pj_ioqueue_t **p_ioqueue) |
| 469 | { |
| 470 | pj_ioqueue_t *ioq; |
| 471 | |
| 472 | PJ_UNUSED_ARG(max_fd); |
| 473 | |
| 474 | ioq = PJ_POOL_ZALLOC_T(pool, pj_ioqueue_t); |
| 475 | *p_ioqueue = ioq; |
| 476 | return PJ_SUCCESS; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /* |
| 481 | * Destroy the I/O queue. |
| 482 | */ |
| 483 | PJ_DEF(pj_status_t) pj_ioqueue_destroy( pj_ioqueue_t *ioq ) |
| 484 | { |
| 485 | PJ_UNUSED_ARG(ioq); |
| 486 | return PJ_SUCCESS; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | /* |
| 491 | * Set the lock object to be used by the I/O Queue. |
| 492 | */ |
| 493 | PJ_DEF(pj_status_t) pj_ioqueue_set_lock( pj_ioqueue_t *ioq, |
| 494 | pj_lock_t *lock, |
| 495 | pj_bool_t auto_delete ) |
| 496 | { |
| 497 | /* Don't really need lock for now */ |
| 498 | PJ_UNUSED_ARG(ioq); |
| 499 | |
| 500 | if (auto_delete) { |
| 501 | pj_lock_destroy(lock); |
| 502 | } |
| 503 | |
| 504 | return PJ_SUCCESS; |
| 505 | } |
| 506 | |
| 507 | PJ_DEF(pj_status_t) pj_ioqueue_set_default_concurrency(pj_ioqueue_t *ioqueue, |
| 508 | pj_bool_t allow) |
| 509 | { |
| 510 | /* Not supported, just return PJ_SUCCESS silently */ |
| 511 | PJ_UNUSED_ARG(ioqueue); |
| 512 | PJ_UNUSED_ARG(allow); |
| 513 | return PJ_SUCCESS; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Register a socket to the I/O queue framework. |
| 518 | */ |
| 519 | PJ_DEF(pj_status_t) pj_ioqueue_register_sock( pj_pool_t *pool, |
| 520 | pj_ioqueue_t *ioq, |
| 521 | pj_sock_t sock, |
| 522 | void *user_data, |
| 523 | const pj_ioqueue_callback *cb, |
| 524 | pj_ioqueue_key_t **p_key ) |
| 525 | { |
| 526 | pj_ioqueue_key_t *key; |
| 527 | |
| 528 | key = PJ_POOL_ZALLOC_T(pool, pj_ioqueue_key_t); |
| 529 | key->cbObj = CIoqueueCallback::NewL(ioq, key, sock, cb, user_data); |
| 530 | |
| 531 | *p_key = key; |
| 532 | return PJ_SUCCESS; |
| 533 | } |
| 534 | |
| 535 | PJ_DEF(pj_status_t) pj_ioqueue_register_sock2(pj_pool_t *pool, |
| 536 | pj_ioqueue_t *ioqueue, |
| 537 | pj_sock_t sock, |
| 538 | pj_grp_lock_t *grp_lock, |
| 539 | void *user_data, |
| 540 | const pj_ioqueue_callback *cb, |
| 541 | pj_ioqueue_key_t **p_key) |
| 542 | { |
| 543 | PJ_UNUSED_ARG(grp_lock); |
| 544 | |
| 545 | return pj_ioqueue_register_sock(pool, ioqueue, sock, user_data, cb, p_key); |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Unregister from the I/O Queue framework. |
| 550 | */ |
| 551 | PJ_DEF(pj_status_t) pj_ioqueue_unregister( pj_ioqueue_key_t *key ) |
| 552 | { |
| 553 | if (key == NULL || key->cbObj == NULL) |
| 554 | return PJ_SUCCESS; |
| 555 | |
| 556 | // Cancel pending async object |
| 557 | if (key->cbObj) { |
| 558 | key->cbObj->Cancel(); |
| 559 | } |
| 560 | |
| 561 | // Close socket. |
| 562 | key->cbObj->get_pj_socket()->Socket().Close(); |
| 563 | delete key->cbObj->get_pj_socket(); |
| 564 | |
| 565 | // Delete async object. |
| 566 | if (key->cbObj) { |
| 567 | delete key->cbObj; |
| 568 | key->cbObj = NULL; |
| 569 | } |
| 570 | |
| 571 | return PJ_SUCCESS; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | /* |
| 576 | * Get user data associated with an ioqueue key. |
| 577 | */ |
| 578 | PJ_DEF(void*) pj_ioqueue_get_user_data( pj_ioqueue_key_t *key ) |
| 579 | { |
| 580 | return key->cbObj->get_user_data(); |
| 581 | } |
| 582 | |
| 583 | |
| 584 | /* |
| 585 | * Set or change the user data to be associated with the file descriptor or |
| 586 | * handle or socket descriptor. |
| 587 | */ |
| 588 | PJ_DEF(pj_status_t) pj_ioqueue_set_user_data( pj_ioqueue_key_t *key, |
| 589 | void *user_data, |
| 590 | void **old_data) |
| 591 | { |
| 592 | if (old_data) |
| 593 | *old_data = key->cbObj->get_user_data(); |
| 594 | key->cbObj->set_user_data(user_data); |
| 595 | |
| 596 | return PJ_SUCCESS; |
| 597 | } |
| 598 | |
| 599 | |
| 600 | /* |
| 601 | * Initialize operation key. |
| 602 | */ |
| 603 | PJ_DEF(void) pj_ioqueue_op_key_init( pj_ioqueue_op_key_t *op_key, |
| 604 | pj_size_t size ) |
| 605 | { |
| 606 | pj_bzero(op_key, size); |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /* |
| 611 | * Check if operation is pending on the specified operation key. |
| 612 | */ |
| 613 | PJ_DEF(pj_bool_t) pj_ioqueue_is_pending( pj_ioqueue_key_t *key, |
| 614 | pj_ioqueue_op_key_t *op_key ) |
| 615 | { |
| 616 | return key->cbObj->get_op_key()==op_key && |
| 617 | key->cbObj->IsActive(); |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /* |
| 622 | * Post completion status to the specified operation key and call the |
| 623 | * appropriate callback. |
| 624 | */ |
| 625 | PJ_DEF(pj_status_t) pj_ioqueue_post_completion( pj_ioqueue_key_t *key, |
| 626 | pj_ioqueue_op_key_t *op_key, |
| 627 | pj_ssize_t bytes_status ) |
| 628 | { |
| 629 | if (pj_ioqueue_is_pending(key, op_key)) { |
| 630 | key->cbObj->CancelOperation(op_key, bytes_status); |
| 631 | } |
| 632 | return PJ_SUCCESS; |
| 633 | } |
| 634 | |
| 635 | |
| 636 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0 |
| 637 | /** |
| 638 | * Instruct I/O Queue to accept incoming connection on the specified |
| 639 | * listening socket. |
| 640 | */ |
| 641 | PJ_DEF(pj_status_t) pj_ioqueue_accept( pj_ioqueue_key_t *key, |
| 642 | pj_ioqueue_op_key_t *op_key, |
| 643 | pj_sock_t *new_sock, |
| 644 | pj_sockaddr_t *local, |
| 645 | pj_sockaddr_t *remote, |
| 646 | int *addrlen ) |
| 647 | { |
| 648 | |
| 649 | return key->cbObj->StartAccept(op_key, new_sock, local, remote, addrlen); |
| 650 | } |
| 651 | |
| 652 | |
| 653 | /* |
| 654 | * Initiate non-blocking socket connect. |
| 655 | */ |
| 656 | PJ_DEF(pj_status_t) pj_ioqueue_connect( pj_ioqueue_key_t *key, |
| 657 | const pj_sockaddr_t *addr, |
| 658 | int addrlen ) |
| 659 | { |
| 660 | pj_status_t status; |
| 661 | |
| 662 | RSocket &rSock = key->cbObj->get_pj_socket()->Socket(); |
| 663 | TInetAddr inetAddr; |
| 664 | TRequestStatus reqStatus; |
| 665 | |
| 666 | // Return failure if access point is marked as down by app. |
| 667 | PJ_SYMBIAN_CHECK_CONNECTION(); |
| 668 | |
| 669 | // Convert address |
| 670 | status = PjSymbianOS::pj2Addr(*(const pj_sockaddr*)addr, addrlen, |
| 671 | inetAddr); |
| 672 | if (status != PJ_SUCCESS) |
| 673 | return status; |
| 674 | |
| 675 | // We don't support async connect for now. |
| 676 | PJ_TODO(IOQUEUE_SUPPORT_ASYNC_CONNECT); |
| 677 | |
| 678 | rSock.Connect(inetAddr, reqStatus); |
| 679 | User::WaitForRequest(reqStatus); |
| 680 | |
| 681 | if (reqStatus == KErrNone) |
| 682 | return PJ_SUCCESS; |
| 683 | |
| 684 | return PJ_RETURN_OS_ERROR(reqStatus.Int()); |
| 685 | } |
| 686 | |
| 687 | |
| 688 | #endif /* PJ_HAS_TCP */ |
| 689 | |
| 690 | /* |
| 691 | * Poll the I/O Queue for completed events. |
| 692 | */ |
| 693 | PJ_DEF(int) pj_ioqueue_poll( pj_ioqueue_t *ioq, |
| 694 | const pj_time_val *timeout) |
| 695 | { |
| 696 | /* Polling is not necessary on Symbian, since all async activities |
| 697 | * are registered to active scheduler. |
| 698 | */ |
| 699 | PJ_UNUSED_ARG(ioq); |
| 700 | PJ_UNUSED_ARG(timeout); |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | |
| 705 | /* |
| 706 | * Instruct the I/O Queue to read from the specified handle. |
| 707 | */ |
| 708 | PJ_DEF(pj_status_t) pj_ioqueue_recv( pj_ioqueue_key_t *key, |
| 709 | pj_ioqueue_op_key_t *op_key, |
| 710 | void *buffer, |
| 711 | pj_ssize_t *length, |
| 712 | pj_uint32_t flags ) |
| 713 | { |
| 714 | // If socket has reader, delete it. |
| 715 | if (key->cbObj->get_pj_socket()->Reader()) |
| 716 | key->cbObj->get_pj_socket()->DestroyReader(); |
| 717 | |
| 718 | // Clear flag |
| 719 | flags &= ~PJ_IOQUEUE_ALWAYS_ASYNC; |
| 720 | return key->cbObj->StartRead(op_key, buffer, length, flags, NULL, NULL); |
| 721 | } |
| 722 | |
| 723 | |
| 724 | /* |
| 725 | * This function behaves similarly as #pj_ioqueue_recv(), except that it is |
| 726 | * normally called for socket, and the remote address will also be returned |
| 727 | * along with the data. |
| 728 | */ |
| 729 | PJ_DEF(pj_status_t) pj_ioqueue_recvfrom( pj_ioqueue_key_t *key, |
| 730 | pj_ioqueue_op_key_t *op_key, |
| 731 | void *buffer, |
| 732 | pj_ssize_t *length, |
| 733 | pj_uint32_t flags, |
| 734 | pj_sockaddr_t *addr, |
| 735 | int *addrlen) |
| 736 | { |
| 737 | CPjSocket *sock = key->cbObj->get_pj_socket(); |
| 738 | |
| 739 | // If address is specified, check that the length match the |
| 740 | // address family |
| 741 | if (addr || addrlen) { |
| 742 | PJ_ASSERT_RETURN(addr && addrlen && *addrlen, PJ_EINVAL); |
| 743 | if (sock->GetAf() == PJ_AF_INET) { |
| 744 | PJ_ASSERT_RETURN(*addrlen>=(int)sizeof(pj_sockaddr_in), PJ_EINVAL); |
| 745 | } else if (sock->GetAf() == PJ_AF_INET6) { |
| 746 | PJ_ASSERT_RETURN(*addrlen>=(int)sizeof(pj_sockaddr_in6), PJ_EINVAL); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | // If socket has reader, delete it. |
| 751 | if (sock->Reader()) |
| 752 | sock->DestroyReader(); |
| 753 | |
| 754 | if (key->cbObj->IsActive()) |
| 755 | return PJ_EBUSY; |
| 756 | |
| 757 | // Clear flag |
| 758 | flags &= ~PJ_IOQUEUE_ALWAYS_ASYNC; |
| 759 | return key->cbObj->StartRead(op_key, buffer, length, flags, addr, addrlen); |
| 760 | } |
| 761 | |
| 762 | |
| 763 | /* |
| 764 | * Instruct the I/O Queue to write to the handle. |
| 765 | */ |
| 766 | PJ_DEF(pj_status_t) pj_ioqueue_send( pj_ioqueue_key_t *key, |
| 767 | pj_ioqueue_op_key_t *op_key, |
| 768 | const void *data, |
| 769 | pj_ssize_t *length, |
| 770 | pj_uint32_t flags ) |
| 771 | { |
| 772 | TRequestStatus reqStatus; |
| 773 | TPtrC8 aBuffer((const TUint8*)data, (TInt)*length); |
| 774 | TSockXfrLength aLen; |
| 775 | |
| 776 | PJ_UNUSED_ARG(op_key); |
| 777 | |
| 778 | // Forcing pending operation is not supported. |
| 779 | PJ_ASSERT_RETURN((flags & PJ_IOQUEUE_ALWAYS_ASYNC)==0, PJ_EINVAL); |
| 780 | |
| 781 | // Return failure if access point is marked as down by app. |
| 782 | PJ_SYMBIAN_CHECK_CONNECTION(); |
| 783 | |
| 784 | // Clear flag |
| 785 | flags &= ~PJ_IOQUEUE_ALWAYS_ASYNC; |
| 786 | |
| 787 | key->cbObj->get_pj_socket()->Socket().Send(aBuffer, flags, reqStatus, aLen); |
| 788 | User::WaitForRequest(reqStatus); |
| 789 | |
| 790 | if (reqStatus.Int() != KErrNone) |
| 791 | return PJ_RETURN_OS_ERROR(reqStatus.Int()); |
| 792 | |
| 793 | //At least in UIQ Emulator, aLen.Length() reports incorrect length |
| 794 | //for UDP (some newlc.com users seem to have reported this too). |
| 795 | //*length = aLen.Length(); |
| 796 | return PJ_SUCCESS; |
| 797 | } |
| 798 | |
| 799 | |
| 800 | /* |
| 801 | * Instruct the I/O Queue to write to the handle. |
| 802 | */ |
| 803 | PJ_DEF(pj_status_t) pj_ioqueue_sendto( pj_ioqueue_key_t *key, |
| 804 | pj_ioqueue_op_key_t *op_key, |
| 805 | const void *data, |
| 806 | pj_ssize_t *length, |
| 807 | pj_uint32_t flags, |
| 808 | const pj_sockaddr_t *addr, |
| 809 | int addrlen) |
| 810 | { |
| 811 | TRequestStatus reqStatus; |
| 812 | TPtrC8 aBuffer; |
| 813 | TInetAddr inetAddr; |
| 814 | TSockXfrLength aLen; |
| 815 | pj_status_t status; |
| 816 | |
| 817 | PJ_UNUSED_ARG(op_key); |
| 818 | |
| 819 | // Forcing pending operation is not supported. |
| 820 | PJ_ASSERT_RETURN((flags & PJ_IOQUEUE_ALWAYS_ASYNC)==0, PJ_EINVAL); |
| 821 | |
| 822 | // Return failure if access point is marked as down by app. |
| 823 | PJ_SYMBIAN_CHECK_CONNECTION(); |
| 824 | |
| 825 | // Convert address |
| 826 | status = PjSymbianOS::pj2Addr(*(const pj_sockaddr*)addr, addrlen, |
| 827 | inetAddr); |
| 828 | if (status != PJ_SUCCESS) |
| 829 | return status; |
| 830 | |
| 831 | // Clear flag |
| 832 | flags &= ~PJ_IOQUEUE_ALWAYS_ASYNC; |
| 833 | |
| 834 | aBuffer.Set((const TUint8*)data, (TInt)*length); |
| 835 | CPjSocket *pjSock = key->cbObj->get_pj_socket(); |
| 836 | |
| 837 | pjSock->Socket().SendTo(aBuffer, inetAddr, flags, reqStatus, aLen); |
| 838 | User::WaitForRequest(reqStatus); |
| 839 | |
| 840 | if (reqStatus.Int() != KErrNone) |
| 841 | return PJ_RETURN_OS_ERROR(reqStatus.Int()); |
| 842 | |
| 843 | //At least in UIQ Emulator, aLen.Length() reports incorrect length |
| 844 | //for UDP (some newlc.com users seem to have reported this too). |
| 845 | //*length = aLen.Length(); |
| 846 | return PJ_SUCCESS; |
| 847 | } |
| 848 | |
| 849 | PJ_DEF(pj_status_t) pj_ioqueue_set_concurrency(pj_ioqueue_key_t *key, |
| 850 | pj_bool_t allow) |
| 851 | { |
| 852 | /* Not supported, just return PJ_SUCCESS silently */ |
| 853 | PJ_UNUSED_ARG(key); |
| 854 | PJ_UNUSED_ARG(allow); |
| 855 | return PJ_SUCCESS; |
| 856 | } |
| 857 | |
| 858 | PJ_DEF(pj_status_t) pj_ioqueue_lock_key(pj_ioqueue_key_t *key) |
| 859 | { |
| 860 | /* Not supported, just return PJ_SUCCESS silently */ |
| 861 | PJ_UNUSED_ARG(key); |
| 862 | return PJ_SUCCESS; |
| 863 | } |
| 864 | |
| 865 | PJ_DEF(pj_status_t) pj_ioqueue_unlock_key(pj_ioqueue_key_t *key) |
| 866 | { |
| 867 | /* Not supported, just return PJ_SUCCESS silently */ |
| 868 | PJ_UNUSED_ARG(key); |
| 869 | return PJ_SUCCESS; |
| 870 | } |
| 871 | |
| 872 | PJ_DEF(pj_oshandle_t) pj_ioqueue_get_os_handle( pj_ioqueue_t *ioqueue ) |
| 873 | { |
| 874 | PJ_UNUSED_ARG(ioqueue); |
| 875 | return NULL; |
| 876 | } |
| 877 | |
| 878 | #endif /* PJ_IOQUEUE_IMP == PJ_IOQUEUE_IMP_SYMBIAN */ |