Skip to content

Commit 94b2d9d

Browse files
committed
Move unix pipes implementation to pipe_unix.rs
The windows named pipes implementation will have almost nothing to do with unix pipes, so I think it's best if they live in separate files.
1 parent a226f56 commit 94b2d9d

File tree

3 files changed

+312
-307
lines changed

3 files changed

+312
-307
lines changed

src/libnative/io/mod.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ pub mod timer;
6060
#[path = "timer_win32.rs"]
6161
pub mod timer;
6262

63+
#[cfg(unix)]
64+
#[path = "path_unix.rs"]
65+
pub mod pipe;
66+
67+
#[cfg(windows)]
68+
#[path = "path_win32.rs"]
69+
pub mod pipe;
70+
6371
mod timer_helper;
6472

6573
pub type IoResult<T> = Result<T, IoError>;
@@ -196,11 +204,11 @@ impl rtio::IoFactory for IoFactory {
196204
fn udp_bind(&mut self, addr: SocketAddr) -> IoResult<~RtioUdpSocket> {
197205
net::UdpSocket::bind(addr).map(|u| ~u as ~RtioUdpSocket)
198206
}
199-
fn unix_bind(&mut self, _path: &CString) -> IoResult<~RtioUnixListener> {
200-
net::UnixListener::bind(_path).map(|s| ~s as ~RtioUnixListener)
207+
fn unix_bind(&mut self, path: &CString) -> IoResult<~RtioUnixListener> {
208+
pipe::UnixListener::bind(path).map(|s| ~s as ~RtioUnixListener)
201209
}
202-
fn unix_connect(&mut self, _path: &CString) -> IoResult<~RtioPipe> {
203-
net::UnixStream::connect(_path, libc::SOCK_STREAM).map(|s| ~s as ~RtioPipe)
210+
fn unix_connect(&mut self, path: &CString) -> IoResult<~RtioPipe> {
211+
pipe::UnixStream::connect(path).map(|s| ~s as ~RtioPipe)
204212
}
205213
fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
206214
hint: Option<ai::Hint>) -> IoResult<~[ai::Info]> {

src/libnative/io/net.rs

-303
Original file line numberDiff line numberDiff line change
@@ -685,306 +685,3 @@ impl rtio::RtioUdpSocket for UdpSocket {
685685
~UdpSocket { inner: self.inner.clone() } as ~rtio::RtioUdpSocket
686686
}
687687
}
688-
689-
690-
#[cfg(not(windows))]
691-
////////////////////////////////////////////////////////////////////////////////
692-
// Unix
693-
////////////////////////////////////////////////////////////////////////////////
694-
695-
696-
////////////////////////////////////////////////////////////////////////////////
697-
// Unix streams
698-
////////////////////////////////////////////////////////////////////////////////
699-
700-
pub struct UnixStream {
701-
priv fd: sock_t,
702-
}
703-
704-
impl UnixStream {
705-
pub fn connect(addr: &CString, ty: libc::c_int) -> IoResult<UnixStream> {
706-
unsafe {
707-
unix_socket(ty).and_then(|fd| {
708-
match addr_to_sockaddr_un(addr) {
709-
Err(e) => return Err(e),
710-
Ok((addr, len)) => {
711-
let ret = UnixStream{ fd: fd };
712-
let addrp = &addr as *libc::sockaddr_storage;
713-
match retry(|| {
714-
libc::connect(fd, addrp as *libc::sockaddr,
715-
len as libc::socklen_t)
716-
}) {
717-
-1 => return Err(super::last_error()),
718-
_ => return Ok(ret)
719-
}
720-
}
721-
}
722-
})
723-
}
724-
}
725-
726-
pub fn fd(&self) -> sock_t { self.fd }
727-
}
728-
729-
impl rtio::RtioPipe for UnixStream {
730-
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
731-
let ret = retry(|| {
732-
unsafe {
733-
libc::recv(self.fd,
734-
buf.as_ptr() as *mut libc::c_void,
735-
buf.len() as wrlen,
736-
0) as libc::c_int
737-
}
738-
});
739-
if ret == 0 {
740-
Err(io::standard_error(io::EndOfFile))
741-
} else if ret < 0 {
742-
Err(super::last_error())
743-
} else {
744-
Ok(ret as uint)
745-
}
746-
}
747-
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
748-
let ret = keep_going(buf, |buf, len| {
749-
unsafe {
750-
libc::send(self.fd,
751-
buf as *mut libc::c_void,
752-
len as wrlen,
753-
0) as i64
754-
}
755-
});
756-
if ret < 0 {
757-
Err(super::last_error())
758-
} else {
759-
Ok(())
760-
}
761-
}
762-
}
763-
764-
impl Drop for UnixStream {
765-
fn drop(&mut self) { unsafe { close(self.fd); } }
766-
}
767-
768-
////////////////////////////////////////////////////////////////////////////////
769-
// Unix Datagram
770-
////////////////////////////////////////////////////////////////////////////////
771-
772-
pub struct UnixDatagram {
773-
priv fd: sock_t,
774-
}
775-
776-
impl UnixDatagram {
777-
pub fn connect(addr: &CString, ty: libc::c_int) -> IoResult<UnixDatagram> {
778-
unsafe {
779-
unix_socket(ty).and_then(|fd| {
780-
match addr_to_sockaddr_un(addr) {
781-
Err(e) => return Err(e),
782-
Ok((addr, len)) => {
783-
let ret = UnixDatagram{ fd: fd };
784-
let addrp = &addr as *libc::sockaddr_storage;
785-
match retry(|| {
786-
libc::connect(fd, addrp as *libc::sockaddr,
787-
len as libc::socklen_t)
788-
}) {
789-
-1 => return Err(super::last_error()),
790-
_ => return Ok(ret)
791-
}
792-
}
793-
}
794-
})
795-
}
796-
}
797-
798-
pub fn bind(addr: &CString) -> IoResult<UnixDatagram> {
799-
unsafe {
800-
unix_socket(libc::SOCK_DGRAM).and_then(|fd| {
801-
match addr_to_sockaddr_un(addr) {
802-
Err(e) => return Err(e),
803-
Ok((addr, len)) => {
804-
let ret = UnixDatagram{ fd: fd };
805-
let addrp = &addr as *libc::sockaddr_storage;
806-
match libc::bind(fd, addrp as *libc::sockaddr,
807-
len as libc::socklen_t) {
808-
-1 => return Err(super::last_error()),
809-
_ => return Ok(ret)
810-
}
811-
}
812-
}
813-
})
814-
}
815-
}
816-
817-
pub fn fd(&self) -> sock_t { self.fd }
818-
}
819-
820-
impl rtio::RtioPipe for UnixDatagram {
821-
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
822-
let ret = retry(|| {
823-
unsafe {
824-
libc::recv(self.fd,
825-
buf.as_ptr() as *mut libc::c_void,
826-
buf.len() as wrlen,
827-
0) as libc::c_int
828-
}
829-
});
830-
if ret == 0 {
831-
Err(io::standard_error(io::EndOfFile))
832-
} else if ret < 0 {
833-
Err(super::last_error())
834-
} else {
835-
Ok(ret as uint)
836-
}
837-
}
838-
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
839-
let ret = keep_going(buf, |buf, len| {
840-
unsafe {
841-
libc::send(self.fd,
842-
buf as *mut libc::c_void,
843-
len as wrlen,
844-
0) as i64
845-
}
846-
});
847-
if ret < 0 {
848-
Err(super::last_error())
849-
} else {
850-
Ok(())
851-
}
852-
}
853-
}
854-
855-
impl rtio::RtioDatagramPipe for UnixDatagram {
856-
fn recvfrom(&mut self, buf: &mut [u8]) -> IoResult<(uint, CString)> {
857-
unsafe {
858-
let mut storage: libc::sockaddr_storage = intrinsics::init();
859-
let storagep = &mut storage as *mut libc::sockaddr_storage;
860-
let mut addrlen: libc::socklen_t =
861-
mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
862-
let ret = retry(|| {
863-
libc::recvfrom(self.fd,
864-
buf.as_ptr() as *mut libc::c_void,
865-
buf.len() as msglen_t,
866-
0,
867-
storagep as *mut libc::sockaddr,
868-
&mut addrlen) as libc::c_int
869-
});
870-
if ret < 0 { return Err(super::last_error()) }
871-
sockaddr_to_unix(&storage, addrlen as uint).and_then(|addr| {
872-
Ok((ret as uint, addr))
873-
})
874-
}
875-
}
876-
877-
fn sendto(&mut self, buf: &[u8], dst: &CString) -> IoResult<()> {
878-
match addr_to_sockaddr_un(dst) {
879-
Err(e) => Err(e),
880-
Ok((dst, len)) => {
881-
let dstp = &dst as *libc::sockaddr_storage;
882-
unsafe {
883-
let ret = retry(|| {
884-
libc::sendto(self.fd,
885-
buf.as_ptr() as *libc::c_void,
886-
buf.len() as msglen_t,
887-
0,
888-
dstp as *libc::sockaddr,
889-
len as libc::socklen_t) as libc::c_int
890-
});
891-
match ret {
892-
-1 => Err(super::last_error()),
893-
n if n as uint != buf.len() => {
894-
Err(io::IoError {
895-
kind: io::OtherIoError,
896-
desc: "couldn't send entire packet at once",
897-
detail: None,
898-
})
899-
}
900-
_ => Ok(())
901-
}
902-
}
903-
}
904-
}
905-
}
906-
}
907-
908-
impl Drop for UnixDatagram {
909-
fn drop(&mut self) { unsafe { close(self.fd); } }
910-
}
911-
////////////////////////////////////////////////////////////////////////////////
912-
// Unix Listener
913-
////////////////////////////////////////////////////////////////////////////////
914-
915-
pub struct UnixListener {
916-
priv fd: sock_t,
917-
}
918-
919-
impl UnixListener {
920-
pub fn bind(addr: &CString) -> IoResult<UnixListener> {
921-
unsafe {
922-
unix_socket(libc::SOCK_STREAM).and_then(|fd| {
923-
match addr_to_sockaddr_un(addr) {
924-
Err(e) => return Err(e),
925-
Ok((addr, len)) => {
926-
let ret = UnixListener{ fd: fd };
927-
let addrp = &addr as *libc::sockaddr_storage;
928-
match libc::bind(fd, addrp as *libc::sockaddr,
929-
len as libc::socklen_t) {
930-
-1 => return Err(super::last_error()),
931-
_ => return Ok(ret)
932-
}
933-
}
934-
}
935-
})
936-
}
937-
}
938-
939-
pub fn fd(&self) -> sock_t { self.fd }
940-
941-
pub fn native_listen(self, backlog: int) -> IoResult<UnixAcceptor> {
942-
match unsafe { libc::listen(self.fd, backlog as libc::c_int) } {
943-
-1 => Err(super::last_error()),
944-
_ => Ok(UnixAcceptor { listener: self })
945-
}
946-
}
947-
}
948-
949-
impl rtio::RtioUnixListener for UnixListener {
950-
fn listen(~self) -> IoResult<~rtio::RtioUnixAcceptor> {
951-
self.native_listen(128).map(|a| ~a as ~rtio::RtioUnixAcceptor)
952-
}
953-
}
954-
955-
impl Drop for UnixListener {
956-
fn drop(&mut self) { unsafe { close(self.fd); } }
957-
}
958-
959-
pub struct UnixAcceptor {
960-
priv listener: UnixListener,
961-
}
962-
963-
impl UnixAcceptor {
964-
pub fn fd(&self) -> sock_t { self.listener.fd }
965-
966-
pub fn native_accept(&mut self) -> IoResult<UnixStream> {
967-
unsafe {
968-
let mut storage: libc::sockaddr_storage = intrinsics::init();
969-
let storagep = &mut storage as *mut libc::sockaddr_storage;
970-
let size = mem::size_of::<libc::sockaddr_storage>();
971-
let mut size = size as libc::socklen_t;
972-
match retry(|| {
973-
libc::accept(self.fd(),
974-
storagep as *mut libc::sockaddr,
975-
&mut size as *mut libc::socklen_t) as libc::c_int
976-
}) as sock_t {
977-
-1 => Err(super::last_error()),
978-
fd => Ok(UnixStream { fd: fd })
979-
}
980-
}
981-
}
982-
}
983-
984-
impl rtio::RtioUnixAcceptor for UnixAcceptor {
985-
fn accept(&mut self) -> IoResult<~rtio::RtioPipe> {
986-
self.native_accept().map(|s| ~s as ~rtio::RtioPipe)
987-
}
988-
}
989-
990-

0 commit comments

Comments
 (0)