1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -43,7 +43,7 @@ extern mod rustrt {
43
43
/**
44
44
* Encapsulates an open TCP/IP connection through libuv
45
45
*
46
- * `tcp_socket ` is non-copyable/sendable and automagically handles closing the
46
+ * `TcpSocket ` is non-copyable/sendable and automagically handles closing the
47
47
* underlying libuv data structures when it goes out of scope. This is the
48
48
* data structure that is used for read/write operations over a TCP stream.
49
49
*/
@@ -66,10 +66,10 @@ pub fn TcpSocket(socket_data: @TcpSocketData) -> TcpSocket {
66
66
}
67
67
68
68
/**
69
- * A buffered wrapper for `net::tcp::tcp_socket `
69
+ * A buffered wrapper for `net::tcp::TcpSocket `
70
70
*
71
71
* It is created with a call to `net::tcp::socket_buf()` and has impls that
72
- * satisfy both the `io::reader ` and `io::writer ` traits.
72
+ * satisfy both the `io::Reader ` and `io::Writer ` traits.
73
73
*/
74
74
pub struct TcpSocketBuf {
75
75
data : @TcpBufferedSocketData ,
@@ -89,7 +89,7 @@ pub struct TcpErrData {
89
89
err_msg : ~str ,
90
90
}
91
91
92
- /// Details returned as part of a `result::err ` result from `tcp::listen`
92
+ /// Details returned as part of a `Result::Err ` result from `tcp::listen`
93
93
pub enum TcpListenErrData {
94
94
/**
95
95
* Some unplanned-for error. The first and second fields correspond
@@ -116,7 +116,7 @@ pub enum TcpListenErrData {
116
116
*/
117
117
AccessDenied
118
118
}
119
- /// Details returned as part of a `result::err ` result from `tcp::connect`
119
+ /// Details returned as part of a `Result::Err ` result from `tcp::connect`
120
120
pub enum TcpConnectErrData {
121
121
/**
122
122
* Some unplanned-for error. The first and second fields correspond
@@ -139,9 +139,9 @@ pub enum TcpConnectErrData {
139
139
* # Returns
140
140
*
141
141
* A `result` that, if the operation succeeds, contains a
142
- * `net::net::tcp_socket ` that can be used to send and receive data to/from
142
+ * `net::net::TcpSocket ` that can be used to send and receive data to/from
143
143
* the remote host. In the event of failure, a
144
- * `net::tcp::tcp_connect_err_data ` instance will be returned
144
+ * `net::tcp::TcpConnectErrData ` instance will be returned
145
145
*/
146
146
pub fn connect ( input_ip : ip:: IpAddr , port : uint ,
147
147
iotask : & IoTask )
@@ -288,14 +288,14 @@ pub fn connect(input_ip: ip::IpAddr, port: uint,
288
288
*
289
289
* # Arguments
290
290
*
291
- * * sock - a `tcp_socket ` to write to
291
+ * * sock - a `TcpSocket ` to write to
292
292
* * raw_write_data - a vector of `~[u8]` that will be written to the stream.
293
293
* This value must remain valid for the duration of the `write` call
294
294
*
295
295
* # Returns
296
296
*
297
- * A `result ` object with a `nil ` value as the `ok ` variant, or a
298
- * `tcp_err_data ` value as the `err ` variant
297
+ * A `Result ` object with a `() ` value as the `Ok ` variant, or a
298
+ * `TcpErrData ` value as the `Err ` variant
299
299
*/
300
300
pub fn write ( sock : & TcpSocket , raw_write_data : ~[ u8 ] )
301
301
-> result:: Result < ( ) , TcpErrData > {
@@ -306,35 +306,35 @@ pub fn write(sock: &TcpSocket, raw_write_data: ~[u8])
306
306
}
307
307
308
308
/**
309
- * Write binary data to tcp stream; Returns a `future::future ` value
309
+ * Write binary data to tcp stream; Returns a `future::Future ` value
310
310
* immediately
311
311
*
312
312
* # Safety
313
313
*
314
314
* This function can produce unsafe results if:
315
315
*
316
316
* 1. the call to `write_future` is made
317
- * 2. the `future::future ` value returned is never resolved via
318
- * `future ::get`
319
- * 3. and then the `tcp_socket ` passed in to `write_future` leaves
317
+ * 2. the `future::Future ` value returned is never resolved via
318
+ * `Future ::get`
319
+ * 3. and then the `TcpSocket ` passed in to `write_future` leaves
320
320
* scope and is destructed before the task that runs the libuv write
321
321
* operation completes.
322
322
*
323
323
* As such: If using `write_future`, always be sure to resolve the returned
324
- * `future ` so as to ensure libuv doesn't try to access a released write
324
+ * `Future ` so as to ensure libuv doesn't try to access a released write
325
325
* handle. Otherwise, use the blocking `tcp::write` function instead.
326
326
*
327
327
* # Arguments
328
328
*
329
- * * sock - a `tcp_socket ` to write to
329
+ * * sock - a `TcpSocket ` to write to
330
330
* * raw_write_data - a vector of `~[u8]` that will be written to the stream.
331
331
* This value must remain valid for the duration of the `write` call
332
332
*
333
333
* # Returns
334
334
*
335
- * A `future ` value that, once the `write` operation completes, resolves to a
336
- * `result ` object with a `nil` value as the `ok ` variant, or a `tcp_err_data `
337
- * value as the `err ` variant
335
+ * A `Future ` value that, once the `write` operation completes, resolves to a
336
+ * `Result ` object with a `nil` value as the `Ok ` variant, or a `TcpErrData `
337
+ * value as the `Err ` variant
338
338
*/
339
339
pub fn write_future ( sock : & TcpSocket , raw_write_data : ~[ u8 ] )
340
340
-> future:: Future < result:: Result < ( ) , TcpErrData > > {
@@ -353,14 +353,14 @@ pub fn write_future(sock: &TcpSocket, raw_write_data: ~[u8])
353
353
*
354
354
* # Arguments
355
355
*
356
- * * sock -- a `net::tcp::tcp_socket ` for the connection to read from
356
+ * * sock -- a `net::tcp::TcpSocket ` for the connection to read from
357
357
*
358
358
* # Returns
359
359
*
360
- * * A `result ` instance that will either contain a
361
- * `core::comm::port<tcp_read_result> ` that the user can read (and
362
- * optionally, loop on) from until `read_stop` is called, or a
363
- * `tcp_err_data ` record
360
+ * * A `Result ` instance that will either contain a
361
+ * `core::comm::Port<Result<~[u8], TcpErrData>> ` that the user can read
362
+ * (and * optionally, loop on) from until `read_stop` is called, or a
363
+ * `TcpErrData ` record
364
364
*/
365
365
pub fn read_start ( sock : & TcpSocket )
366
366
-> result:: Result < @Port <
@@ -376,7 +376,7 @@ pub fn read_start(sock: &TcpSocket)
376
376
*
377
377
* # Arguments
378
378
*
379
- * * `sock` - a `net::tcp::tcp_socket ` that you wish to stop reading on
379
+ * * `sock` - a `net::tcp::TcpSocket ` that you wish to stop reading on
380
380
*/
381
381
pub fn read_stop ( sock : & TcpSocket ) ->
382
382
result:: Result < ( ) , TcpErrData > {
@@ -387,17 +387,17 @@ pub fn read_stop(sock: &TcpSocket) ->
387
387
}
388
388
389
389
/**
390
- * Reads a single chunk of data from `tcp_socket `; block until data/error
390
+ * Reads a single chunk of data from `TcpSocket `; block until data/error
391
391
* recv'd
392
392
*
393
393
* Does a blocking read operation for a single chunk of data from a
394
- * `tcp_socket ` until a data arrives or an error is received. The provided
394
+ * `TcpSocket ` until a data arrives or an error is received. The provided
395
395
* `timeout_msecs` value is used to raise an error if the timeout period
396
396
* passes without any data received.
397
397
*
398
398
* # Arguments
399
399
*
400
- * * `sock` - a `net::tcp::tcp_socket ` that you wish to read from
400
+ * * `sock` - a `net::tcp::TcpSocket ` that you wish to read from
401
401
* * `timeout_msecs` - a `uint` value, in msecs, to wait before dropping the
402
402
* read attempt. Pass `0u` to wait indefinitely
403
403
*/
@@ -408,31 +408,31 @@ pub fn read(sock: &TcpSocket, timeout_msecs: uint)
408
408
}
409
409
410
410
/**
411
- * Reads a single chunk of data; returns a `future::future <~[u8]>`
411
+ * Reads a single chunk of data; returns a `future::Future <~[u8]>`
412
412
* immediately
413
413
*
414
414
* Does a non-blocking read operation for a single chunk of data from a
415
- * `tcp_socket ` and immediately returns a `future ` value representing the
416
- * result. When resolving the returned `future `, it will block until data
415
+ * `TcpSocket ` and immediately returns a `Future ` value representing the
416
+ * result. When resolving the returned `Future `, it will block until data
417
417
* arrives or an error is received. The provided `timeout_msecs`
418
418
* value is used to raise an error if the timeout period passes without any
419
419
* data received.
420
420
*
421
421
* # Safety
422
422
*
423
423
* This function can produce unsafe results if the call to `read_future` is
424
- * made, the `future::future ` value returned is never resolved via
425
- * `future ::get`, and then the `tcp_socket ` passed in to `read_future` leaves
424
+ * made, the `future::Future ` value returned is never resolved via
425
+ * `Future ::get`, and then the `TcpSocket ` passed in to `read_future` leaves
426
426
* scope and is destructed before the task that runs the libuv read
427
427
* operation completes.
428
428
*
429
429
* As such: If using `read_future`, always be sure to resolve the returned
430
- * `future ` so as to ensure libuv doesn't try to access a released read
430
+ * `Future ` so as to ensure libuv doesn't try to access a released read
431
431
* handle. Otherwise, use the blocking `tcp::read` function instead.
432
432
*
433
433
* # Arguments
434
434
*
435
- * * `sock` - a `net::tcp::tcp_socket ` that you wish to read from
435
+ * * `sock` - a `net::tcp::TcpSocket ` that you wish to read from
436
436
* * `timeout_msecs` - a `uint` value, in msecs, to wait before dropping the
437
437
* read attempt. Pass `0u` to wait indefinitely
438
438
*/
@@ -445,7 +445,7 @@ fn read_future(sock: &TcpSocket, timeout_msecs: uint)
445
445
}
446
446
447
447
/**
448
- * Bind an incoming client connection to a `net::tcp::tcp_socket `
448
+ * Bind an incoming client connection to a `net::tcp::TcpSocket `
449
449
*
450
450
* # Notes
451
451
*
@@ -461,57 +461,57 @@ fn read_future(sock: &TcpSocket, timeout_msecs: uint)
461
461
*
462
462
* This implies that a port/chan pair must be used to make sure that the
463
463
* `new_connect_cb` call blocks until an attempt to create a
464
- * `net::tcp::tcp_socket ` is completed.
464
+ * `net::tcp::TcpSocket ` is completed.
465
465
*
466
466
* # Example
467
467
*
468
468
* Here, the `new_conn` is used in conjunction with `accept` from within
469
469
* a task spawned by the `new_connect_cb` passed into `listen`
470
470
*
471
471
* ~~~~~~~~~~~
472
- * net::tcp::listen(remote_ip, remote_port, backlog)
472
+ * do net::tcp::listen(remote_ip, remote_port, backlog, iotask,
473
473
* // this callback is ran once after the connection is successfully
474
474
* // set up
475
- * { |kill_ch|
475
+ * |kill_ch| {
476
476
* // pass the kill_ch to your main loop or wherever you want
477
477
* // to be able to externally kill the server from
478
- * }
478
+ * })
479
479
* // this callback is ran when a new connection arrives
480
- * {|new_conn, kill_ch|
481
- * let cont_po = core::comm::port::<Option<tcp_err_data>>();
482
- * let cont_ch = core::comm::chan(cont_po);
483
- * task::spawn {||
480
+ * |new_conn, kill_ch| {
481
+ * let (cont_po, cont_ch) = comm::stream::<option::Option<TcpErrData>>();
482
+ * do task::spawn {
484
483
* let accept_result = net::tcp::accept(new_conn);
485
- * if accept_result.is_err() {
486
- * core::comm::send(cont_ch, result::get_err(accept_result));
487
- * // fail?
488
- * }
489
- * else {
490
- * let sock = result::get(accept_result);
491
- * core::comm::send(cont_ch, true);
492
- * // do work here
484
+ * match accept_result {
485
+ * Err(accept_error) => {
486
+ * cont_ch.send(Some(accept_error));
487
+ * // fail?
488
+ * },
489
+ * Ok(sock) => {
490
+ * cont_ch.send(None);
491
+ * // do work here
492
+ * }
493
493
* }
494
494
* };
495
- * match core::comm:: recv(cont_po ) {
495
+ * match cont_po. recv() {
496
496
* // shut down listen()
497
- * Some(err_data) { core::comm:: send(kill_chan, Some(err_data)) }
497
+ * Some(err_data) => kill_ch. send(Some(err_data)),
498
498
* // wait for next connection
499
- * None {}
499
+ * None => ()
500
500
* }
501
501
* };
502
502
* ~~~~~~~~~~~
503
503
*
504
504
* # Arguments
505
505
*
506
- * * `new_conn` - an opaque value used to create a new `tcp_socket `
506
+ * * `new_conn` - an opaque value used to create a new `TcpSocket `
507
507
*
508
508
* # Returns
509
509
*
510
- * On success, this function will return a `net::tcp::tcp_socket ` as the
511
- * `ok ` variant of a `result `. The `net::tcp::tcp_socket ` is anchored within
510
+ * On success, this function will return a `net::tcp::TcpSocket ` as the
511
+ * `Ok ` variant of a `Result `. The `net::tcp::TcpSocket ` is anchored within
512
512
* the task that `accept` was called within for its lifetime. On failure,
513
- * this function will return a `net::tcp::tcp_err_data ` record
514
- * as the `err ` variant of a `result `.
513
+ * this function will return a `net::tcp::TcpErrData ` record
514
+ * as the `Err ` variant of a `Result `.
515
515
*/
516
516
pub fn accept ( new_conn : TcpNewConnection )
517
517
-> result:: Result < TcpSocket , TcpErrData > {
@@ -600,27 +600,27 @@ pub fn accept(new_conn: TcpNewConnection)
600
600
*
601
601
* # Arguments
602
602
*
603
- * * `host_ip` - a `net::ip::ip_addr ` representing a unique IP
603
+ * * `host_ip` - a `net::ip::IpAddr ` representing a unique IP
604
604
* (versions 4 or 6)
605
605
* * `port` - a uint representing the port to listen on
606
606
* * `backlog` - a uint representing the number of incoming connections
607
607
* to cache in memory
608
- * * `hl_loop` - a `uv::hl::high_level_loop ` that the tcp request will run on
608
+ * * `hl_loop` - a `uv_iotask::IoTask ` that the tcp request will run on
609
609
* * `on_establish_cb` - a callback that is evaluated if/when the listener
610
610
* is successfully established. it takes no parameters
611
611
* * `new_connect_cb` - a callback to be evaluated, on the libuv thread,
612
612
* whenever a client attempts to conect on the provided ip/port. the
613
613
* callback's arguments are:
614
614
* * `new_conn` - an opaque type that can be passed to
615
- * `net::tcp::accept` in order to be converted to a `tcp_socket `.
616
- * * `kill_ch` - channel of type `core::comm::chan <Option<tcp_err_data>>`.
615
+ * `net::tcp::accept` in order to be converted to a `TcpSocket `.
616
+ * * `kill_ch` - channel of type `core::comm::Chan <Option<tcp_err_data>>`.
617
617
* this channel can be used to send a message to cause `listen` to begin
618
618
* closing the underlying libuv data structures.
619
619
*
620
620
* # returns
621
621
*
622
- * a `result ` instance containing empty data of type `()` on a
623
- * successful/normal shutdown, and a `tcp_listen_err_data ` enum in the event
622
+ * a `Result ` instance containing empty data of type `()` on a
623
+ * successful/normal shutdown, and a `TcpListenErrData ` enum in the event
624
624
* of listen exiting because of an error
625
625
*/
626
626
pub fn listen ( host_ip : ip:: IpAddr , port : uint , backlog : uint ,
@@ -799,27 +799,27 @@ fn listen_common(host_ip: ip::IpAddr, port: uint, backlog: uint,
799
799
800
800
801
801
/**
802
- * Convert a `net::tcp::tcp_socket ` to a `net::tcp::tcp_socket_buf `.
802
+ * Convert a `net::tcp::TcpSocket ` to a `net::tcp::TcpSocketBuf `.
803
803
*
804
- * This function takes ownership of a `net::tcp::tcp_socket `, returning it
805
- * stored within a buffered wrapper, which can be converted to a `io::reader `
806
- * or `io::writer `
804
+ * This function takes ownership of a `net::tcp::TcpSocket `, returning it
805
+ * stored within a buffered wrapper, which can be converted to a `io::Reader `
806
+ * or `io::Writer `
807
807
*
808
808
* # Arguments
809
809
*
810
- * * `sock` -- a `net::tcp::tcp_socket ` that you want to buffer
810
+ * * `sock` -- a `net::tcp::TcpSocket ` that you want to buffer
811
811
*
812
812
* # Returns
813
813
*
814
- * A buffered wrapper that you can cast as an `io::reader ` or `io::writer `
814
+ * A buffered wrapper that you can cast as an `io::Reader ` or `io::Writer `
815
815
*/
816
816
pub fn socket_buf ( sock : TcpSocket ) -> TcpSocketBuf {
817
817
TcpSocketBuf ( @TcpBufferedSocketData {
818
818
sock : sock, mut buf : ~[ ] , buf_off : 0
819
819
} )
820
820
}
821
821
822
- /// Convenience methods extending `net::tcp::tcp_socket `
822
+ /// Convenience methods extending `net::tcp::TcpSocket `
823
823
pub impl TcpSocket {
824
824
pub fn read_start ( ) -> result:: Result < @Port <
825
825
result:: Result < ~[ u8 ] , TcpErrData > > , TcpErrData > {
@@ -862,7 +862,7 @@ pub impl TcpSocket {
862
862
}
863
863
}
864
864
865
- /// Implementation of `io::reader ` trait for a buffered `net::tcp::tcp_socket `
865
+ /// Implementation of `io::Reader ` trait for a buffered `net::tcp::TcpSocket `
866
866
impl io:: Reader for TcpSocketBuf {
867
867
fn read ( & self , buf : & mut [ u8 ] , len : uint ) -> uint {
868
868
if len == 0 { return 0 }
@@ -962,7 +962,7 @@ impl io::Reader for TcpSocketBuf {
962
962
}
963
963
}
964
964
965
- /// Implementation of `io::reader ` trait for a buffered `net::tcp::tcp_socket `
965
+ /// Implementation of `io::Reader ` trait for a buffered `net::tcp::TcpSocket `
966
966
impl io:: Writer for TcpSocketBuf {
967
967
pub fn write ( & self , data : & [ const u8 ] ) {
968
968
unsafe {
0 commit comments