1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-28 05:10:36 +02:00

add generic server service factory

This commit is contained in:
Nikolay Kim
2018-09-08 14:50:16 -07:00
parent 4264574af1
commit 552d19a0eb
5 changed files with 50 additions and 50 deletions

View File

@ -9,11 +9,11 @@ extern crate tokio_io;
extern crate tokio_openssl;
extern crate tokio_tcp;
use std::fmt;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
use std::{fmt, io};
use futures::{future, Future};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
@ -25,7 +25,7 @@ use actix_net::{IntoNewService, NewServiceExt, Server};
/// Simple logger service, it just prints fact of the new connections
fn logger<T: AsyncRead + AsyncWrite + fmt::Debug>(
stream: T,
) -> impl Future<Item = T, Error = io::Error> {
) -> impl Future<Item = T, Error = ()> {
println!("New connection: {:?}", stream);
future::ok(stream)
}
@ -40,7 +40,7 @@ struct ServiceState {
/// Service function for our stateful service
fn service<T: AsyncRead + AsyncWrite>(
st: &mut ServiceState, _stream: T,
) -> impl Future<Item = (), Error = io::Error> {
) -> impl Future<Item = (), Error = ()> {
let num = st.num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
@ -75,7 +75,7 @@ fn main() {
// service for converting incoming TcpStream to a SslStream<TcpStream>
(move |stream| {
SslAcceptorExt::accept_async(&acceptor, stream)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
.map_err(|e| println!("Openssl error: {}", e))
})
// convert closure to a `NewService`
.into_new_service()
@ -89,7 +89,7 @@ fn main() {
// actix-net generates `NewService` impl that creates `ServiceState` instance for each new service
// and use `service` function as `Service::call`
.and_then((service, move || {
Ok::<_, io::Error>(ServiceState { num: num.clone() })
Ok(ServiceState { num: num.clone() })
}))
},
).unwrap()

View File

@ -5,7 +5,6 @@ extern crate openssl;
extern crate tokio_io;
extern crate tokio_tcp;
use std::io;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
@ -24,7 +23,7 @@ struct ServiceState {
fn service<T: AsyncRead + AsyncWrite>(
st: &mut ServiceState, _: T,
) -> impl Future<Item = (), Error = io::Error> {
) -> impl Future<Item = (), Error = ()> {
let num = st.num.fetch_add(1, Ordering::Relaxed);
println!("got ssl connection {:?}", num);
future::ok(())
@ -43,7 +42,7 @@ fn main() {
.unwrap();
let num = Arc::new(AtomicUsize::new(0));
let openssl = ssl::OpensslAcceptor::new(builder);
let openssl = ssl::OpensslAcceptor::new(builder.build());
// server start mutiple workers, it runs supplied `Fn` in each worker.
Server::default()
@ -53,10 +52,8 @@ fn main() {
// configure service
openssl
.clone()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
.and_then((service, move || {
Ok::<_, io::Error>(ServiceState { num: num.clone() })
}))
.map_err(|e| println!("Openssl error: {}", e))
.and_then((service, move || Ok(ServiceState { num: num.clone() })))
}).unwrap()
.start();