-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement closeRead/closeWrite using TcpStream::shutdown #903
Changes from 1 commit
46daa09
147a762
0ea6755
4f06636
160115b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
|
||
import * as deno from "deno"; | ||
import { testPerm, assert, assertEqual } from "./test_util.ts"; | ||
import { testPerm, assert, assertEqual, deferred } from "./test_util.ts"; | ||
|
||
testPerm({ net: true }, function netListenClose() { | ||
const listener = deno.listen("tcp", "127.0.0.1:4500"); | ||
|
@@ -35,3 +35,59 @@ testPerm({ net: true }, async function netDialListen() { | |
listener.close(); | ||
conn.close(); | ||
}); | ||
|
||
testPerm({ net: true }, async function netConnCloseRead() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
const addr = "127.0.0.1:4500"; | ||
const listener = deno.listen("tcp", addr); | ||
const closeDeferred = deferred(); | ||
listener.accept().then(async conn => { | ||
await conn.write(new Uint8Array([1, 2, 3])); | ||
const buf = new Uint8Array(1024); | ||
const readResult = await conn.read(buf); | ||
assertEqual(3, readResult.nread); | ||
assertEqual(4, buf[0]); | ||
assertEqual(5, buf[1]); | ||
assertEqual(6, buf[2]); | ||
conn.close(); | ||
closeDeferred.resolve(); | ||
}); | ||
const conn = await deno.dial("tcp", addr); | ||
conn.closeRead(); // closing read | ||
const buf = new Uint8Array(1024); | ||
const readResult = await conn.read(buf); | ||
assertEqual(0, readResult.nread); // TODO: no error? | ||
assertEqual(true, readResult.eof); | ||
await conn.write(new Uint8Array([4, 5, 6])); | ||
await closeDeferred.promise; | ||
listener.close(); | ||
conn.close(); | ||
}); | ||
|
||
testPerm({ net: true }, async function netConnCloseWrite() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
const addr = "127.0.0.1:4500"; | ||
const listener = deno.listen("tcp", addr); | ||
const closeDeferred = deferred(); | ||
listener.accept().then(async conn => { | ||
await conn.write(new Uint8Array([1, 2, 3])); | ||
await closeDeferred.promise; | ||
conn.close(); | ||
}); | ||
const conn = await deno.dial("tcp", addr); | ||
conn.closeWrite(); // closing read | ||
const buf = new Uint8Array(1024); | ||
const readResult = await conn.read(buf); | ||
assertEqual(3, readResult.nread); | ||
assertEqual(1, buf[0]); | ||
assertEqual(2, buf[1]); | ||
assertEqual(3, buf[2]); | ||
let err; | ||
try { | ||
await conn.write(new Uint8Array([1, 2, 3])); | ||
} catch (e) { | ||
err = e; | ||
} | ||
assert(!!err); // TODO: Broken Pipe? | ||
closeDeferred.resolve(); | ||
listener.close(); | ||
conn.close(); | ||
}); | ||
kevinkassimo marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ use std; | |
use std::collections::HashMap; | ||
use std::io::Error; | ||
use std::io::{Read, Write}; | ||
use std::net::SocketAddr; | ||
use std::net::{Shutdown, SocketAddr}; | ||
use std::sync::atomic::AtomicIsize; | ||
use std::sync::atomic::Ordering; | ||
use std::sync::Mutex; | ||
|
@@ -79,6 +79,19 @@ impl Resource { | |
let r = table.remove(&self.rid); | ||
assert!(r.is_some()); | ||
} | ||
|
||
// no collision with unimplemented shutdown | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this mean? |
||
pub fn shutdown_on(&mut self, how: Shutdown) { | ||
let mut table = RESOURCE_TABLE.lock().unwrap(); | ||
let maybe_repr = table.get_mut(&self.rid); | ||
match maybe_repr { | ||
None => panic!("bad rid"), | ||
Some(repr) => match repr { | ||
Repr::TcpStream(ref mut f) => TcpStream::shutdown(f, how).unwrap(), | ||
_ => panic!("Cannot shutdown"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good - nice. |
||
}, | ||
} | ||
} | ||
} | ||
|
||
impl Read for Resource { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please leave the Addr changes out of this PR? I'd like to discuss them separately from shutdown.