SlideShare a Scribd company logo
Of Owls and IO Objects
OR network programming with ASIO
Felix Morgner
Oct 25, 2017
Institute for Software
University of Applied Sciences Rapperswil
Introduction
What is ASIO
1
What is ASIO
• A type of owl
1
What is ASIO
• A type of owl
• Audio Stream Input/Output
1
What is ASIO
• A type of owl
• Audio Stream Input/Output
• Australian Security Intelligence Organisation
1
What is ASIO
• A type of owl
• Audio Stream Input/Output
• Australian Security Intelligence Organisation
• An asynchronous I/O toolkit for C++
1
Why should you care
• Networking TS (N4656)
2
Why should you care
• Networking TS (N4656)
• Networking is tedious
2
Why should you care
• Networking TS (N4656)
• Networking is tedious
• It’s more than networking
2
Agenda
Software Design Patterns
ASIO 101
Networking
Beyond Networking
Summary and Q&A
3
Software Design Patterns
Brief History
• Architecture: Christopher Alexander 1977/79
• A Place To Wait
4
Brief History
• Architecture: Christopher Alexander 1977/79
• A Place To Wait
• Beck & Cunningham 1987
4
Brief History
• Architecture: Christopher Alexander 1977/79
• A Place To Wait
• Beck & Cunningham 1987
• GoF Patterns (Gamma et al.) 1994
4
Brief History
• Architecture: Christopher Alexander 1977/79
• A Place To Wait
• Beck & Cunningham 1987
• GoF Patterns (Gamma et al.) 1994
• Many more since then
4
What Are Patterns
• Solutions to common problems
• NOT code
5
What Are Patterns
• Solutions to common problems
• NOT code
• Descriptions of relevant forces
5
What Are Patterns
• Solutions to common problems
• NOT code
• Descriptions of relevant forces
• Language independent
5
Misconceptions
• Patterns are invented
6
Misconceptions
• Patterns are invented
• GoF patterns are the only patterns
6
Misconceptions
• Patterns are invented
• GoF patterns are the only patterns
• More patterns == Better software
6
Misconceptions
• Patterns are invented
• GoF patterns are the only patterns
• More patterns == Better software
• Patterns have no drawbacks
6
Misconceptions
• Patterns are invented
• GoF patterns are the only patterns
• More patterns == Better software
• Patterns have no drawbacks
• Singleton is a good pattern
6
Pattern Sources
• GoF
7
Pattern Sources
• GoF
• POSA 1, 2, 3, 4
7
Pattern Sources
• GoF
• POSA 1, 2, 3, 4
• Security Patterns
7
Pattern Sources
• GoF
• POSA 1, 2, 3, 4
• Security Patterns
• Patterns for Fault Tolerant Software
7
Pattern Sources
• GoF
• POSA 1, 2, 3, 4
• Security Patterns
• Patterns for Fault Tolerant Software
• Game Programming Patterns
7
Pattern Sources
• GoF
• POSA 1, 2, 3, 4
• Security Patterns
• Patterns for Fault Tolerant Software
• Game Programming Patterns
• Many, many more
7
Two Patterns for Asynchronous I/O
• Reactor
8
Two Patterns for Asynchronous I/O
• Reactor
• Notify when I/O is ready
• “Consumer” must perform I/O
8
Two Patterns for Asynchronous I/O
• Reactor
• Notify when I/O is ready
• “Consumer” must perform I/O
• Proactor
8
Two Patterns for Asynchronous I/O
• Reactor
• Notify when I/O is ready
• “Consumer” must perform I/O
• Proactor
• Notify when I/O is done
• “Consumer” works with data
8
Two Patterns for Asynchronous I/O
• Reactor
• Notify when I/O is ready
• “Consumer” must perform I/O
• Proactor
• Notify when I/O is done
• “Consumer” works with data
8
Proactor Relations
9
ASIO 101
Overview
• Created by Christopher Kohlhoff in 2003
10
Overview
• Created by Christopher Kohlhoff in 2003
• Part of the Boost libraries since 2008
10
Overview
• Created by Christopher Kohlhoff in 2003
• Part of the Boost libraries since 2008
• Available on Linux, BSD, macOS, Windows, …
10
Overview
• Created by Christopher Kohlhoff in 2003
• Part of the Boost libraries since 2008
• Available on Linux, BSD, macOS, Windows, …
• Based on the Proactor pattern
10
Overview
• Created by Christopher Kohlhoff in 2003
• Part of the Boost libraries since 2008
• Available on Linux, BSD, macOS, Windows, …
• Based on the Proactor pattern
• NOT only for asynchronous I/O
10
ASIO Hello World
#include <asio.hpp>
#include <chrono>
#include <iostream>
int main() {
auto && service = asio::io_service{};
auto && timer = asio::steady_timer{service};
timer.expires_from_now(std::chrono::seconds{1});
timer.async_wait([](auto error){
std::cout << "timer fired!n";
});
service.run();
}
11
Disecting the Example
• Our Proactor
• service = asio::io_service{};
12
Disecting the Example
• Our Proactor
• service = asio::io_service{};
• The I/O object
• timer = asio::steady_timer{service};
12
Disecting the Example
• Our Proactor
• service = asio::io_service{};
• The I/O object
• timer = asio::steady_timer{service};
• Our asynchronous operation
• timer.async_wait(...)
12
Disecting the Example
• Our Proactor
• service = asio::io_service{};
• The I/O object
• timer = asio::steady_timer{service};
• Our asynchronous operation
• timer.async_wait(...)
• Our completion handler
• [](auto error){ ... }
12
This is the basic anatomy of an ASIO program
13
Networking
It’s almost as simple
• OOTB Support for ICMP, TCP, UDP, IPv4/6, Multicast
14
It’s almost as simple
• OOTB Support for ICMP, TCP, UDP, IPv4/6, Multicast
• Includes support for resolving addresses
14
It’s almost as simple
• OOTB Support for ICMP, TCP, UDP, IPv4/6, Multicast
• Includes support for resolving addresses
• Also support TLS through OpenSSL
14
It’s almost as simple
• OOTB Support for ICMP, TCP, UDP, IPv4/6, Multicast
• Includes support for resolving addresses
• Also support TLS through OpenSSL
• “Byte-based” and “Condition-based”
14
An echo server with:
• N connections
15
An echo server with:
• N connections
• Output queueing
15
An echo server with:
• N connections
• Output queueing
• Automatic connection lifetime
15
The connection class (head)
struct connection : std::enable_shared_from_this<connection>
// ... members
};
16
The connection class (data members)
struct connection : ... {
private:
asio::ip::tcp::socket m_sock;
asio::strand m_strand;
std::array<char, 1024> m_buff{};
std::deque<std::string> m_out{};
};
17
The connection class (ctor)
struct connection : ... {
connection(asio::ip::tcp::socket sock)
: m_sock{std::move(sock)}
, m_strand{m_sock.get_io_service()} {
}
};
18
The connection class (starting communication)
struct connection : ... {
void start() {
do_read();
}
};
19
The connection class (reading data)
struct connection : ... {
private:
void do_read() {
m_sock.async_read_some(asio::buffer(m_buff),
[&, self = shared_from_this()](auto error, auto read) {
if(!error) {
auto begin = m_buff.data();
auto end = begin + read;
m_strand.post([&, data=std::string{begin, end}]{
write(data);
});
do_read();
}
});
}
};
20
The connection class (writing data part 1)
struct connection : ... {
private:
void write(std::string data) {
m_out.push_back(data);
if(m_out.size() < 2) {
do_write();
}
}
};
21
The connection class (writing data part 2)
struct connection : ... {
private:
void do_write() {
asio::async_write(m_sock, asio::buffer(m_out.front()),
m_strand.wrap([&, self = shared_from_this()](auto error
auto writte
m_out.pop_front();
if(!error) {
if(!m_out.empty()) {
do_write();
}
}
}));
}
};
22
Review of the connection class
Connections:
23
Review of the connection class
Connections:
• keep themselves alive
23
Review of the connection class
Connections:
• keep themselves alive
• have a single input buffer
23
Review of the connection class
Connections:
• keep themselves alive
• have a single input buffer
• have an output queue
23
The server class (data members)
struct server {
private:
asio::ip::tcp::acceptor m_acceptor;
asio::ip::tcp::socket m_sock;
};
24
The server class (creation)
struct server {
server(asio::io_service & service)
: m_acceptor{service,
asio::ip::tcp::endpoint{
asio::ip::tcp::v4(),
4321}}
, m_sock{service} {}
};
25
The server class (starting)
struct server {
void start() {
do_accept();
}
};
26
The server class (accepting connections)
struct server {
private:
void do_accept() {
m_acceptor.async_accept(m_sock, [&](auto error){
if(!error) {
auto conn = std::make_shared<connection>(
std::move(m_sock)
);
conn->start();
do_accept();
}
});
}
};
27
Review of the server class
Our server:
28
Review of the server class
Our server:
• accepts new connections
28
Review of the server class
Our server:
• accepts new connections
• creates connection objects
28
Review of the server class
Our server:
• accepts new connections
• creates connection objects
• does not care about the connections
28
Glueing it all together
auto && service = asio::io_service{};
auto && server = ::server{service};
server.start();
29
Glueing it all together (contd.)
service.run();
30
Glueing it all together (contd.)
auto const cpus = std::thread::hardware_concurrency();
auto pool = std::vector<std::future<void>>{cpus};
for(auto & future : pool) {
future = std::async(std::launch::async, [&]{
service.run();
});
}
31
Demo Time!
32
There is more
• stream buffers
33
There is more
• stream buffers
• conditions
33
There is more
• stream buffers
• conditions
• TLS
33
There is more
• stream buffers
• conditions
• TLS
• Name resolution
33
There is more
• stream buffers
• conditions
• TLS
• Name resolution
• Synchronous I/O
33
Beyond Networking
It’s More Than Networking
34
It’s More Than Networking
• Signal handling
34
It’s More Than Networking
• Signal handling
• General worker pool
34
It’s More Than Networking
• Signal handling
• General worker pool
• Stream I/O
34
It’s More Than Networking
• Signal handling
• General worker pool
• Stream I/O
• Serial ports
34
It’s More Than Networking
• Signal handling
• General worker pool
• Stream I/O
• Serial ports
• Timers
34
It’s More Than Networking
• Signal handling
• General worker pool
• Stream I/O
• Serial ports
• Timers
• File I/O (Windows)
34
Signal handling
auto && signals = asio::signal_set(service, SIGINT);
signals.add(SIGINT);
signals.async_wait([&](auto error, auto signal){
if(!error && !service.stopped()) {
std::cout << "Received SIGINT. Terminating. " << 'n';
service.stop();
}
});
35
Worker Pool (posting the work)
service.post([&]{
prove_p_equals_np();
});
36
Worker Pool (posting the work)
service.post([&]{
factor_large_rsa_key();
});
37
Worker Pool (posting the work)
service.post([&]{
invent_time_machine();
});
38
Worker Pool (posting the work)
service.post([&]{
std::do_stuff();
});
39
Worker Pool (running the workers)
auto const cpus = std::thread::hardware_concurrency();
auto pool = std::vector<std::future<void>>{cpus};
for(auto & future : pool) {
future = std::async(std::launch::async, [&]{
service.run();
});
}
40
Summary and Q&A
Conclusion
• ASIO is not a framework!
41
Conclusion
• ASIO is not a framework!
• It is not that scary
41
Conclusion
• ASIO is not a framework!
• It is not that scary
• Provides rich I/O and event handling
41
Conclusion
• ASIO is not a framework!
• It is not that scary
• Provides rich I/O and event handling
• Parts of it will be standard C++
41
Questions?
42

More Related Content

What's hot

Python concurrency: libraries overview
Python concurrency: libraries overviewPython concurrency: libraries overview
Python concurrency: libraries overview
Andrii Mishkovskyi
 
6.1. iCloud keychain and iOS 7 data protection
6.1. iCloud keychain and iOS 7 data protection6.1. iCloud keychain and iOS 7 data protection
6.1. iCloud keychain and iOS 7 data protection
defconmoscow
 
ぐだ生 Java入門第ニ回(synchronized and lock)
ぐだ生 Java入門第ニ回(synchronized and lock)ぐだ生 Java入門第ニ回(synchronized and lock)
ぐだ生 Java入門第ニ回(synchronized and lock)
Makoto Yamazaki
 
ぐだ生 Java入門第ニ回(synchronized and lock)
ぐだ生 Java入門第ニ回(synchronized and lock)ぐだ生 Java入門第ニ回(synchronized and lock)
ぐだ生 Java入門第ニ回(synchronized and lock)
Makoto Yamazaki
 
Tarantool как платформа для микросервисов / Антон Резников, Владимир Перепели...
Tarantool как платформа для микросервисов / Антон Резников, Владимир Перепели...Tarantool как платформа для микросервисов / Антон Резников, Владимир Перепели...
Tarantool как платформа для микросервисов / Антон Резников, Владимир Перепели...
Ontico
 

What's hot (20)

Extensions vs Services: Digging Deeper - Neil Bartlett
Extensions vs Services: Digging Deeper - Neil BartlettExtensions vs Services: Digging Deeper - Neil Bartlett
Extensions vs Services: Digging Deeper - Neil Bartlett
 
The Ring programming language version 1.10 book - Part 102 of 212
The Ring programming language version 1.10 book - Part 102 of 212The Ring programming language version 1.10 book - Part 102 of 212
The Ring programming language version 1.10 book - Part 102 of 212
 
Androsia: A step ahead in securing in-memory Android application data by Sami...
Androsia: A step ahead in securing in-memory Android application data by Sami...Androsia: A step ahead in securing in-memory Android application data by Sami...
Androsia: A step ahead in securing in-memory Android application data by Sami...
 
Wait for your fortune without Blocking!
Wait for your fortune without Blocking!Wait for your fortune without Blocking!
Wait for your fortune without Blocking!
 
"ClojureScript journey: from little script, to CLI program, to AWS Lambda fun...
"ClojureScript journey: from little script, to CLI program, to AWS Lambda fun..."ClojureScript journey: from little script, to CLI program, to AWS Lambda fun...
"ClojureScript journey: from little script, to CLI program, to AWS Lambda fun...
 
Python Async IO Horizon
Python Async IO HorizonPython Async IO Horizon
Python Async IO Horizon
 
Python concurrency: libraries overview
Python concurrency: libraries overviewPython concurrency: libraries overview
Python concurrency: libraries overview
 
Volker Fröhlich - How to Debug Common Agent Issues
Volker Fröhlich - How to Debug Common Agent IssuesVolker Fröhlich - How to Debug Common Agent Issues
Volker Fröhlich - How to Debug Common Agent Issues
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Shellcode mastering
Shellcode masteringShellcode mastering
Shellcode mastering
 
Jwt == insecurity?
Jwt == insecurity?Jwt == insecurity?
Jwt == insecurity?
 
6.1. iCloud keychain and iOS 7 data protection
6.1. iCloud keychain and iOS 7 data protection6.1. iCloud keychain and iOS 7 data protection
6.1. iCloud keychain and iOS 7 data protection
 
ぐだ生 Java入門第ニ回(synchronized and lock)
ぐだ生 Java入門第ニ回(synchronized and lock)ぐだ生 Java入門第ニ回(synchronized and lock)
ぐだ生 Java入門第ニ回(synchronized and lock)
 
ぐだ生 Java入門第ニ回(synchronized and lock)
ぐだ生 Java入門第ニ回(synchronized and lock)ぐだ生 Java入門第ニ回(synchronized and lock)
ぐだ生 Java入門第ニ回(synchronized and lock)
 
Ln monitoring repositories
Ln monitoring repositoriesLn monitoring repositories
Ln monitoring repositories
 
Tarantool как платформа для микросервисов / Антон Резников, Владимир Перепели...
Tarantool как платформа для микросервисов / Антон Резников, Владимир Перепели...Tarantool как платформа для микросервисов / Антон Резников, Владимир Перепели...
Tarantool как платформа для микросервисов / Антон Резников, Владимир Перепели...
 
Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)
 
OSMC 2012 | Distributed Monitoring mit NSClient++ by Michael Medin
OSMC 2012 | Distributed Monitoring mit NSClient++ by Michael MedinOSMC 2012 | Distributed Monitoring mit NSClient++ by Michael Medin
OSMC 2012 | Distributed Monitoring mit NSClient++ by Michael Medin
 
Everything you wanted to know about Stack Traces and Heap Dumps
Everything you wanted to know about Stack Traces and Heap DumpsEverything you wanted to know about Stack Traces and Heap Dumps
Everything you wanted to know about Stack Traces and Heap Dumps
 
Ce e nou in Rails 4
Ce e nou in Rails 4Ce e nou in Rails 4
Ce e nou in Rails 4
 

Similar to Of Owls and IO Objects

Reducing Redundancies in Multi-Revision Code Analysis
Reducing Redundancies in Multi-Revision Code AnalysisReducing Redundancies in Multi-Revision Code Analysis
Reducing Redundancies in Multi-Revision Code Analysis
Sebastiano Panichella
 
How ElasticSearch lives in my DevOps life
How ElasticSearch lives in my DevOps lifeHow ElasticSearch lives in my DevOps life
How ElasticSearch lives in my DevOps life
琛琳 饶
 

Similar to Of Owls and IO Objects (20)

Utilizing the open ntf domino api
Utilizing the open ntf domino apiUtilizing the open ntf domino api
Utilizing the open ntf domino api
 
The Future of Node - @rvagg - NodeConf Christchurch 2015
The Future of Node - @rvagg - NodeConf Christchurch 2015The Future of Node - @rvagg - NodeConf Christchurch 2015
The Future of Node - @rvagg - NodeConf Christchurch 2015
 
Reducing Redundancies in Multi-Revision Code Analysis
Reducing Redundancies in Multi-Revision Code AnalysisReducing Redundancies in Multi-Revision Code Analysis
Reducing Redundancies in Multi-Revision Code Analysis
 
Windows 8 Apps and the Outside World
Windows 8 Apps and the Outside WorldWindows 8 Apps and the Outside World
Windows 8 Apps and the Outside World
 
How ElasticSearch lives in my DevOps life
How ElasticSearch lives in my DevOps lifeHow ElasticSearch lives in my DevOps life
How ElasticSearch lives in my DevOps life
 
Utilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino APIUtilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino API
 
Effective C++
Effective C++Effective C++
Effective C++
 
Modern C++
Modern C++Modern C++
Modern C++
 
Utilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino APIUtilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino API
 
20161029 py con-mysq-lv3
20161029 py con-mysq-lv320161029 py con-mysq-lv3
20161029 py con-mysq-lv3
 
TypeScript and SharePoint Framework
TypeScript and SharePoint FrameworkTypeScript and SharePoint Framework
TypeScript and SharePoint Framework
 
Angular2
Angular2Angular2
Angular2
 
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a serviceCOMMitMDE'18: Eclipse Hawk: model repository querying as a service
COMMitMDE'18: Eclipse Hawk: model repository querying as a service
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
 
OSGi Community Event 2010 - Dependencies, dependencies, dependencies
OSGi Community Event 2010 - Dependencies, dependencies, dependenciesOSGi Community Event 2010 - Dependencies, dependencies, dependencies
OSGi Community Event 2010 - Dependencies, dependencies, dependencies
 
C++ Windows Forms L01 - Intro
C++ Windows Forms L01 - IntroC++ Windows Forms L01 - Intro
C++ Windows Forms L01 - Intro
 
SIP Tutorial/Workshop 3
SIP Tutorial/Workshop 3SIP Tutorial/Workshop 3
SIP Tutorial/Workshop 3
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Happy Go Programming
Happy Go ProgrammingHappy Go Programming
Happy Go Programming
 
LibreSSL, one year later
LibreSSL, one year laterLibreSSL, one year later
LibreSSL, one year later
 

Recently uploaded

Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
Atif Razi
 

Recently uploaded (20)

Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Danfoss NeoCharge Technology -A Revolution in 2024.pdf
Danfoss NeoCharge Technology -A Revolution in 2024.pdfDanfoss NeoCharge Technology -A Revolution in 2024.pdf
Danfoss NeoCharge Technology -A Revolution in 2024.pdf
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical SolutionsRS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
RS Khurmi Machine Design Clutch and Brake Exercise Numerical Solutions
 

Of Owls and IO Objects

  • 1. Of Owls and IO Objects OR network programming with ASIO Felix Morgner Oct 25, 2017 Institute for Software University of Applied Sciences Rapperswil
  • 4. What is ASIO • A type of owl 1
  • 5. What is ASIO • A type of owl • Audio Stream Input/Output 1
  • 6. What is ASIO • A type of owl • Audio Stream Input/Output • Australian Security Intelligence Organisation 1
  • 7. What is ASIO • A type of owl • Audio Stream Input/Output • Australian Security Intelligence Organisation • An asynchronous I/O toolkit for C++ 1
  • 8. Why should you care • Networking TS (N4656) 2
  • 9. Why should you care • Networking TS (N4656) • Networking is tedious 2
  • 10. Why should you care • Networking TS (N4656) • Networking is tedious • It’s more than networking 2
  • 11. Agenda Software Design Patterns ASIO 101 Networking Beyond Networking Summary and Q&A 3
  • 13. Brief History • Architecture: Christopher Alexander 1977/79 • A Place To Wait 4
  • 14. Brief History • Architecture: Christopher Alexander 1977/79 • A Place To Wait • Beck & Cunningham 1987 4
  • 15. Brief History • Architecture: Christopher Alexander 1977/79 • A Place To Wait • Beck & Cunningham 1987 • GoF Patterns (Gamma et al.) 1994 4
  • 16. Brief History • Architecture: Christopher Alexander 1977/79 • A Place To Wait • Beck & Cunningham 1987 • GoF Patterns (Gamma et al.) 1994 • Many more since then 4
  • 17. What Are Patterns • Solutions to common problems • NOT code 5
  • 18. What Are Patterns • Solutions to common problems • NOT code • Descriptions of relevant forces 5
  • 19. What Are Patterns • Solutions to common problems • NOT code • Descriptions of relevant forces • Language independent 5
  • 21. Misconceptions • Patterns are invented • GoF patterns are the only patterns 6
  • 22. Misconceptions • Patterns are invented • GoF patterns are the only patterns • More patterns == Better software 6
  • 23. Misconceptions • Patterns are invented • GoF patterns are the only patterns • More patterns == Better software • Patterns have no drawbacks 6
  • 24. Misconceptions • Patterns are invented • GoF patterns are the only patterns • More patterns == Better software • Patterns have no drawbacks • Singleton is a good pattern 6
  • 26. Pattern Sources • GoF • POSA 1, 2, 3, 4 7
  • 27. Pattern Sources • GoF • POSA 1, 2, 3, 4 • Security Patterns 7
  • 28. Pattern Sources • GoF • POSA 1, 2, 3, 4 • Security Patterns • Patterns for Fault Tolerant Software 7
  • 29. Pattern Sources • GoF • POSA 1, 2, 3, 4 • Security Patterns • Patterns for Fault Tolerant Software • Game Programming Patterns 7
  • 30. Pattern Sources • GoF • POSA 1, 2, 3, 4 • Security Patterns • Patterns for Fault Tolerant Software • Game Programming Patterns • Many, many more 7
  • 31. Two Patterns for Asynchronous I/O • Reactor 8
  • 32. Two Patterns for Asynchronous I/O • Reactor • Notify when I/O is ready • “Consumer” must perform I/O 8
  • 33. Two Patterns for Asynchronous I/O • Reactor • Notify when I/O is ready • “Consumer” must perform I/O • Proactor 8
  • 34. Two Patterns for Asynchronous I/O • Reactor • Notify when I/O is ready • “Consumer” must perform I/O • Proactor • Notify when I/O is done • “Consumer” works with data 8
  • 35. Two Patterns for Asynchronous I/O • Reactor • Notify when I/O is ready • “Consumer” must perform I/O • Proactor • Notify when I/O is done • “Consumer” works with data 8
  • 38. Overview • Created by Christopher Kohlhoff in 2003 10
  • 39. Overview • Created by Christopher Kohlhoff in 2003 • Part of the Boost libraries since 2008 10
  • 40. Overview • Created by Christopher Kohlhoff in 2003 • Part of the Boost libraries since 2008 • Available on Linux, BSD, macOS, Windows, … 10
  • 41. Overview • Created by Christopher Kohlhoff in 2003 • Part of the Boost libraries since 2008 • Available on Linux, BSD, macOS, Windows, … • Based on the Proactor pattern 10
  • 42. Overview • Created by Christopher Kohlhoff in 2003 • Part of the Boost libraries since 2008 • Available on Linux, BSD, macOS, Windows, … • Based on the Proactor pattern • NOT only for asynchronous I/O 10
  • 43. ASIO Hello World #include <asio.hpp> #include <chrono> #include <iostream> int main() { auto && service = asio::io_service{}; auto && timer = asio::steady_timer{service}; timer.expires_from_now(std::chrono::seconds{1}); timer.async_wait([](auto error){ std::cout << "timer fired!n"; }); service.run(); } 11
  • 44. Disecting the Example • Our Proactor • service = asio::io_service{}; 12
  • 45. Disecting the Example • Our Proactor • service = asio::io_service{}; • The I/O object • timer = asio::steady_timer{service}; 12
  • 46. Disecting the Example • Our Proactor • service = asio::io_service{}; • The I/O object • timer = asio::steady_timer{service}; • Our asynchronous operation • timer.async_wait(...) 12
  • 47. Disecting the Example • Our Proactor • service = asio::io_service{}; • The I/O object • timer = asio::steady_timer{service}; • Our asynchronous operation • timer.async_wait(...) • Our completion handler • [](auto error){ ... } 12
  • 48. This is the basic anatomy of an ASIO program 13
  • 50. It’s almost as simple • OOTB Support for ICMP, TCP, UDP, IPv4/6, Multicast 14
  • 51. It’s almost as simple • OOTB Support for ICMP, TCP, UDP, IPv4/6, Multicast • Includes support for resolving addresses 14
  • 52. It’s almost as simple • OOTB Support for ICMP, TCP, UDP, IPv4/6, Multicast • Includes support for resolving addresses • Also support TLS through OpenSSL 14
  • 53. It’s almost as simple • OOTB Support for ICMP, TCP, UDP, IPv4/6, Multicast • Includes support for resolving addresses • Also support TLS through OpenSSL • “Byte-based” and “Condition-based” 14
  • 54. An echo server with: • N connections 15
  • 55. An echo server with: • N connections • Output queueing 15
  • 56. An echo server with: • N connections • Output queueing • Automatic connection lifetime 15
  • 57. The connection class (head) struct connection : std::enable_shared_from_this<connection> // ... members }; 16
  • 58. The connection class (data members) struct connection : ... { private: asio::ip::tcp::socket m_sock; asio::strand m_strand; std::array<char, 1024> m_buff{}; std::deque<std::string> m_out{}; }; 17
  • 59. The connection class (ctor) struct connection : ... { connection(asio::ip::tcp::socket sock) : m_sock{std::move(sock)} , m_strand{m_sock.get_io_service()} { } }; 18
  • 60. The connection class (starting communication) struct connection : ... { void start() { do_read(); } }; 19
  • 61. The connection class (reading data) struct connection : ... { private: void do_read() { m_sock.async_read_some(asio::buffer(m_buff), [&, self = shared_from_this()](auto error, auto read) { if(!error) { auto begin = m_buff.data(); auto end = begin + read; m_strand.post([&, data=std::string{begin, end}]{ write(data); }); do_read(); } }); } }; 20
  • 62. The connection class (writing data part 1) struct connection : ... { private: void write(std::string data) { m_out.push_back(data); if(m_out.size() < 2) { do_write(); } } }; 21
  • 63. The connection class (writing data part 2) struct connection : ... { private: void do_write() { asio::async_write(m_sock, asio::buffer(m_out.front()), m_strand.wrap([&, self = shared_from_this()](auto error auto writte m_out.pop_front(); if(!error) { if(!m_out.empty()) { do_write(); } } })); } }; 22
  • 64. Review of the connection class Connections: 23
  • 65. Review of the connection class Connections: • keep themselves alive 23
  • 66. Review of the connection class Connections: • keep themselves alive • have a single input buffer 23
  • 67. Review of the connection class Connections: • keep themselves alive • have a single input buffer • have an output queue 23
  • 68. The server class (data members) struct server { private: asio::ip::tcp::acceptor m_acceptor; asio::ip::tcp::socket m_sock; }; 24
  • 69. The server class (creation) struct server { server(asio::io_service & service) : m_acceptor{service, asio::ip::tcp::endpoint{ asio::ip::tcp::v4(), 4321}} , m_sock{service} {} }; 25
  • 70. The server class (starting) struct server { void start() { do_accept(); } }; 26
  • 71. The server class (accepting connections) struct server { private: void do_accept() { m_acceptor.async_accept(m_sock, [&](auto error){ if(!error) { auto conn = std::make_shared<connection>( std::move(m_sock) ); conn->start(); do_accept(); } }); } }; 27
  • 72. Review of the server class Our server: 28
  • 73. Review of the server class Our server: • accepts new connections 28
  • 74. Review of the server class Our server: • accepts new connections • creates connection objects 28
  • 75. Review of the server class Our server: • accepts new connections • creates connection objects • does not care about the connections 28
  • 76. Glueing it all together auto && service = asio::io_service{}; auto && server = ::server{service}; server.start(); 29
  • 77. Glueing it all together (contd.) service.run(); 30
  • 78. Glueing it all together (contd.) auto const cpus = std::thread::hardware_concurrency(); auto pool = std::vector<std::future<void>>{cpus}; for(auto & future : pool) { future = std::async(std::launch::async, [&]{ service.run(); }); } 31
  • 80. There is more • stream buffers 33
  • 81. There is more • stream buffers • conditions 33
  • 82. There is more • stream buffers • conditions • TLS 33
  • 83. There is more • stream buffers • conditions • TLS • Name resolution 33
  • 84. There is more • stream buffers • conditions • TLS • Name resolution • Synchronous I/O 33
  • 86. It’s More Than Networking 34
  • 87. It’s More Than Networking • Signal handling 34
  • 88. It’s More Than Networking • Signal handling • General worker pool 34
  • 89. It’s More Than Networking • Signal handling • General worker pool • Stream I/O 34
  • 90. It’s More Than Networking • Signal handling • General worker pool • Stream I/O • Serial ports 34
  • 91. It’s More Than Networking • Signal handling • General worker pool • Stream I/O • Serial ports • Timers 34
  • 92. It’s More Than Networking • Signal handling • General worker pool • Stream I/O • Serial ports • Timers • File I/O (Windows) 34
  • 93. Signal handling auto && signals = asio::signal_set(service, SIGINT); signals.add(SIGINT); signals.async_wait([&](auto error, auto signal){ if(!error && !service.stopped()) { std::cout << "Received SIGINT. Terminating. " << 'n'; service.stop(); } }); 35
  • 94. Worker Pool (posting the work) service.post([&]{ prove_p_equals_np(); }); 36
  • 95. Worker Pool (posting the work) service.post([&]{ factor_large_rsa_key(); }); 37
  • 96. Worker Pool (posting the work) service.post([&]{ invent_time_machine(); }); 38
  • 97. Worker Pool (posting the work) service.post([&]{ std::do_stuff(); }); 39
  • 98. Worker Pool (running the workers) auto const cpus = std::thread::hardware_concurrency(); auto pool = std::vector<std::future<void>>{cpus}; for(auto & future : pool) { future = std::async(std::launch::async, [&]{ service.run(); }); } 40
  • 100. Conclusion • ASIO is not a framework! 41
  • 101. Conclusion • ASIO is not a framework! • It is not that scary 41
  • 102. Conclusion • ASIO is not a framework! • It is not that scary • Provides rich I/O and event handling 41
  • 103. Conclusion • ASIO is not a framework! • It is not that scary • Provides rich I/O and event handling • Parts of it will be standard C++ 41