| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // Written in NSPR style to also be suitable for adding to the NSS demo suite | 4 // Written in NSPR style to also be suitable for adding to the NSS demo suite |
| 5 | 5 |
| 6 /* memio is a simple NSPR I/O layer that lets you decouple NSS from | 6 /* memio is a simple NSPR I/O layer that lets you decouple NSS from |
| 7 * the real network. It's rather like openssl's memory bio, | 7 * the real network. It's rather like openssl's memory bio, |
| 8 * and is useful when your app absolutely, positively doesn't | 8 * and is useful when your app absolutely, positively doesn't |
| 9 * want to let NSS do its own networking. | 9 * want to let NSS do its own networking. |
| 10 */ | 10 */ |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 struct memio_buffer readbuf; | 48 struct memio_buffer readbuf; |
| 49 | 49 |
| 50 /* write requests are satisfied from this buffer */ | 50 /* write requests are satisfied from this buffer */ |
| 51 struct memio_buffer writebuf; | 51 struct memio_buffer writebuf; |
| 52 | 52 |
| 53 /* SSL needs to know socket peer's name */ | 53 /* SSL needs to know socket peer's name */ |
| 54 PRNetAddr peername; | 54 PRNetAddr peername; |
| 55 | 55 |
| 56 /* if set, empty I/O returns EOF instead of EWOULDBLOCK */ | 56 /* if set, empty I/O returns EOF instead of EWOULDBLOCK */ |
| 57 int eof; | 57 int eof; |
| 58 |
| 59 /* if set, the number of bytes requested from readbuf that were not |
| 60 * fulfilled (due to readbuf being empty) */ |
| 61 int read_requested; |
| 58 }; | 62 }; |
| 59 | 63 |
| 60 /*--------------- private memio_buffer functions ---------------------*/ | 64 /*--------------- private memio_buffer functions ---------------------*/ |
| 61 | 65 |
| 62 /* Forward declarations. */ | 66 /* Forward declarations. */ |
| 63 | 67 |
| 64 /* Allocate a memio_buffer of given size. */ | 68 /* Allocate a memio_buffer of given size. */ |
| 65 static void memio_buffer_new(struct memio_buffer *mb, int size); | 69 static void memio_buffer_new(struct memio_buffer *mb, int size); |
| 66 | 70 |
| 67 /* Deallocate a memio_buffer allocated by memio_buffer_new. */ | 71 /* Deallocate a memio_buffer allocated by memio_buffer_new. */ |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 if (flags) { | 220 if (flags) { |
| 217 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); | 221 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); |
| 218 return -1; | 222 return -1; |
| 219 } | 223 } |
| 220 | 224 |
| 221 secret = fd->secret; | 225 secret = fd->secret; |
| 222 mb = &secret->readbuf; | 226 mb = &secret->readbuf; |
| 223 PR_ASSERT(mb->bufsize); | 227 PR_ASSERT(mb->bufsize); |
| 224 rv = memio_buffer_get(mb, buf, len); | 228 rv = memio_buffer_get(mb, buf, len); |
| 225 if (rv == 0 && !secret->eof) { | 229 if (rv == 0 && !secret->eof) { |
| 230 secret->read_requested = len; |
| 226 if (mb->last_err) | 231 if (mb->last_err) |
| 227 PR_SetError(mb->last_err, 0); | 232 PR_SetError(mb->last_err, 0); |
| 228 else | 233 else |
| 229 PR_SetError(PR_WOULD_BLOCK_ERROR, 0); | 234 PR_SetError(PR_WOULD_BLOCK_ERROR, 0); |
| 230 return -1; | 235 return -1; |
| 231 } | 236 } |
| 232 | 237 |
| 238 secret->read_requested = 0; |
| 233 return rv; | 239 return rv; |
| 234 } | 240 } |
| 235 | 241 |
| 236 static int PR_CALLBACK memio_Read(PRFileDesc *fd, void *buf, PRInt32 len) | 242 static int PR_CALLBACK memio_Read(PRFileDesc *fd, void *buf, PRInt32 len) |
| 237 { | 243 { |
| 238 /* pull bytes from buffer */ | 244 /* pull bytes from buffer */ |
| 239 return memio_Recv(fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT); | 245 return memio_Recv(fd, buf, len, 0, PR_INTERVAL_NO_TIMEOUT); |
| 240 } | 246 } |
| 241 | 247 |
| 242 static int PR_CALLBACK memio_Send(PRFileDesc *fd, const void *buf, PRInt32 len, | 248 static int PR_CALLBACK memio_Send(PRFileDesc *fd, const void *buf, PRInt32 len, |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 secret->peername = *peername; | 381 secret->peername = *peername; |
| 376 } | 382 } |
| 377 | 383 |
| 378 memio_Private *memio_GetSecret(PRFileDesc *fd) | 384 memio_Private *memio_GetSecret(PRFileDesc *fd) |
| 379 { | 385 { |
| 380 PRFileDesc *memiofd = PR_GetIdentitiesLayer(fd, memio_identity); | 386 PRFileDesc *memiofd = PR_GetIdentitiesLayer(fd, memio_identity); |
| 381 struct PRFilePrivate *secret = memiofd->secret; | 387 struct PRFilePrivate *secret = memiofd->secret; |
| 382 return (memio_Private *)secret; | 388 return (memio_Private *)secret; |
| 383 } | 389 } |
| 384 | 390 |
| 391 int memio_GetReadRequest(memio_Private *secret) |
| 392 { |
| 393 return ((PRFilePrivate *)secret)->read_requested; |
| 394 } |
| 395 |
| 385 int memio_GetReadParams(memio_Private *secret, char **buf) | 396 int memio_GetReadParams(memio_Private *secret, char **buf) |
| 386 { | 397 { |
| 387 struct memio_buffer* mb = &((PRFilePrivate *)secret)->readbuf; | 398 struct memio_buffer* mb = &((PRFilePrivate *)secret)->readbuf; |
| 388 PR_ASSERT(mb->bufsize); | 399 PR_ASSERT(mb->bufsize); |
| 389 | 400 |
| 390 *buf = &mb->buf[mb->tail]; | 401 *buf = &mb->buf[mb->tail]; |
| 391 return memio_buffer_unused_contiguous(mb); | 402 return memio_buffer_unused_contiguous(mb); |
| 392 } | 403 } |
| 393 | 404 |
| 394 void memio_PutReadResult(memio_Private *secret, int bytes_read) | 405 void memio_PutReadResult(memio_Private *secret, int bytes_read) |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 CHECKEQ(memio_buffer_unused_contiguous(&mb), 0); | 502 CHECKEQ(memio_buffer_unused_contiguous(&mb), 0); |
| 492 CHECKEQ(memio_buffer_used_contiguous(&mb), 1); | 503 CHECKEQ(memio_buffer_used_contiguous(&mb), 1); |
| 493 | 504 |
| 494 /* TODO: add more cases */ | 505 /* TODO: add more cases */ |
| 495 | 506 |
| 496 printf("Test passed\n"); | 507 printf("Test passed\n"); |
| 497 exit(0); | 508 exit(0); |
| 498 } | 509 } |
| 499 | 510 |
| 500 #endif | 511 #endif |
| OLD | NEW |