SlideShare a Scribd company logo
1 of 37
Download to read offline
NetWork & IO
I/O and binary programming

2013/11/14
#GoCon
●
●
●
●
●
●

id: Jxck
github: Jxck
twitter: jxck_
about: http://jxck.io
blog: http://d.hatena.ne.jp/jxck
Love: music

Jack
Network server in go  #gocon 2013-11-14
Go研

https://github.com/goken/goken
Now working on ...
● http2 server/client
○
○
○
○

github.com/Jxck/http2
github.com/Jxck/hpack
github.com/Jxck/logger
github.com/Jxck/color

● Caution
○
○
○
○

not enough error handling
not enough test
not enough api/feature
under heavy working :p
network server
I/O and binary programming
NetWork Server in Go
●
●
●
●
●
●
●

net
[]byte
bytes
encoding/binary
io
io/ioutil
bufio
No Error Handling in Sample Codes
func main() {
listener, err := net.Listen("tcp", "127.0.0.1:3000") // err
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept() // err
if err != nil {
log.Fatal(err)
}
defer func() {
log.Println("close connection")
conn.Close()
}()
_, err = conn.Write([]byte("hellon")) // send hello
if err != nil {
log.Fatal(err)
}
}
}

Hard To Read on Slide
import “net”
tcp server in go
TCP Server
package main
import "net"
func main() {
listener, _ := net.Listen("tcp", ":3000")
for {
conn, _ := listener.Accept()
conn.Write([]byte("hellon"))
conn.Close()
}
}
TCP Server With Handler
func main() {
listener, _ := net.Listen("tcp", ":3000")
for {
conn, _ := listener.Accept()
handleConn(conn)
}
}
func handleConn(conn net.Conn) {
conn.Write([]byte("hellon"))
}
TCP Server with Goroutine
func main() {
listener, _ := net.Listen("tcp", ":3000")
for {
conn, _ := listener.Accept()
go handleConn(conn)
}
}
func handleConn(conn net.Conn) {
conn.Write([]byte("hellon"))
}
[]byte
handle octet stream in go
binary format (example)
R

Length(14)

R
Reserved(8)

Type(8)

Flags(8)

Stream Identifier(31)
Setting Identifier(24)
Value(32)

[]byte{0, 8, 4, 0, 0, 0, 0, 1, 0, 0,
0, 1, 0, 0, 16, 0}
low level byte slice handling
func main() {
b := []byte{}
b = append(b, []byte{0,
b = append(b, 4)
b = append(b, 0)
// stream id 1
b = append(b, []byte{0,
// settings id 1
b = append(b, []byte{0,
// value 4096
b = append(b, []byte{0,
fmt.Println(b)
// [0 8 4 0 0 0 0 1 0 0
}

8}...)

0, 0, 1}...)
0, 0, 1}...)
0, 16, 0}...)
0 1 0 0 16 0]

// length 8
// type 4
// flags 0
SWrap (slice wrap)
import “github.com/Jxck/swrap”
type SWrap
●
●
●
●
●
●
●
●
●
●
●
●

func
func
func
func
func
func
func
func
func
func
func
func

New(a []byte) SWrap
(sw *SWrap) Add(a byte)
(sw *SWrap) Bytes() []byte
(sw *SWrap) Compare(b []byte) bool
(sw *SWrap) Delete(i int)
(sw *SWrap) Len() int
(sw *SWrap) Merge(a []byte)
(sw *SWrap) Pop() byte
(sw *SWrap) Push(b byte)
(sw *SWrap) Replace(i int, b byte)
(sw *SWrap) Shift(b byte)
(sw *SWrap) UnShift() byte
import “io”
handle IO in go
important: io.Reader, io.Writer
type Reader interface {
Read(p []byte) (n int, err error)
}
type Writer interface {
Write(p []byte) (n int, err error)
}
type ReadWriter interface {
Reader
Writer
}
net.Conn implements io.Writer
func handleConn(conn net.Conn) {
conn.Write([]byte("hellon"))
}
func handleConn(conn io.Writer) {
conn.Write(
[]byte{104, 101, 108, 108, 111, 10}
)
}
import “bytes”
utility for byte slice and Buffer
bytes.Buffer: io.ReadWriter for []byte
func handleConn(conn io.Writer) {
conn.Write([]byte{104, 101, 108, 108, 111, 10})
}
func main() {
b := []byte{}
buf := bytes.NewBuffer(b) // io.ReadeWriter
handleConn(buf)
actual := buf.Bytes()
bytes.Equal(
actual,
[]byte{104, 101, 108, 108, 111, 10},
) // true
}
import
“encoding/binary”
read/write fixed size value
we did...
func main() {
b := []byte{}
b = append(b, []byte{0,
b = append(b, 4)
b = append(b, 0)
// stream id 1
b = append(b, []byte{0,
// settings id 1
b = append(b, []byte{0,
// value 4096
b = append(b, []byte{0,
fmt.Println(b)
// [0 8 4 0 0 0 0 1 0 0
}

8}...)

0, 0, 1}...)
0, 0, 1}...)
0, 16, 0}...)
0 1 0 0 16 0]

// length 8
// type 4
// flags 0
Fixed Size
R

Length(14)

R

Type(8)

Flags(8)

Stream Identifier(31)

Reserved(8)

Setting Identifier(24)
Value(32)

var
var
var
var

Length uint16 = 8
Type, Flags uint8 = 4, 0
StreamId, SettingsId uint32 = 1, 1
Value uint32 = 4096
encoding/binary.Write()
buf
var
var
var
var

:= bytes.NewBuffer([]byte{})
Length uint16 = 8
Type, Flags uint8 = 4, 0
StreamId, SettingsId uint32 = 1, 1
Value uint32 = 4096

binary.Write(buf, binary.BigEndian, Length)
binary.Write(buf, binary.BigEndian, Type)
// ...
binary.Write(buf, binary.BigEndian, Value)
fmt.Println(buf.Bytes())
// [0 8 4 0 0 0 0 1 0 0 0 1 0 0 16 0]
encoding/binary.Write()
buf := bytes.NewBuffer([]byte{})
frame := Frame{
Length:
Type:
Flags:
StreamId:
SettingsId:
Value:
}

8,
4,
0,
1,
1,
4096,

type Frame struct {
Length
uint16
Type
uint8
Flags
uint8
StreamId
uint32
SettingsId uint32
Value
uint32
}

// func Write(w io.Writer, order ByteOrder, data
interface{}) error
binary.Write(buf, binary.BigEndian, frame)
fmt.Println(buf.Bytes())
import “net”
tcp client in go
TCP Client
func main() {
conn, _ := net.Dial("tcp", ":3000")
b := make([]byte, 100)
n, _ := conn.Read(b)
fmt.Print(string(b[:n]))
// hello
}
import “bufio”
buffered io
bufio := []byte(4096) + io
// bufio.Reader
type Reader struct {}
// bufio.Writer
type Writer struct {}
func NewReader(rd io.Reader) *Reader {
// convert io.Reader to *bufio.Reader
}
func NewWriter(wr io.Writer) *Writer {
// convert io.Writer to *bufio.Writer
}
TCP Client with bufio
func main() {
conn, _ := net.Dial("tcp", ":3000")
br := bufio.NewReader(conn)
line, _ := br.ReadString('n')
fmt.Print(line) // hello
}
TCP Client with encoding/binary
type Frame struct {
Length
uint16
Type
uint8
Flags
uint8
StreamId
uint32
SettingsId uint32
Value
uint32
}
func main() {
conn, _ := net.Dial("tcp", ":3000")
frame := &Frame{}
binary.Read(conn, binary.BigEndian, frame)
fmt.Print(frame) // &{8 4 0 1 1 4096}
}
HTTP/2.0?
HTTP/2.0 with Go
● tcp/tls connection
○ net

● multiplexed stream
○ goroutine / channel

● binary frame
○ static type / encoding/binary

● crypto
○ crypto

● build
○ go build (cross compile)

● test
○ go test (and testing)
HTTP2.0 Study (#http2study)
● http2.0 勉強会 #2
○ http://bit.ly/158zE4C

● http2.0 hackathon
○ 12月 or 1月
Q & A
anyone ?
END
thanks :)

More Related Content

What's hot

Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersJen Yee Hong
 
Go serving: Building server app with go
Go serving: Building server app with goGo serving: Building server app with go
Go serving: Building server app with goHean Hong Leong
 
Theming Plone with Deliverance
Theming Plone with DeliveranceTheming Plone with Deliverance
Theming Plone with DeliveranceRok Garbas
 
Beauty and Power of Go
Beauty and Power of GoBeauty and Power of Go
Beauty and Power of GoFrank Müller
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operationsarchikabhatia
 
Start Wrap Episode 11: A New Rope
Start Wrap Episode 11: A New RopeStart Wrap Episode 11: A New Rope
Start Wrap Episode 11: A New RopeYung-Yu Chen
 
Python Generators
Python GeneratorsPython Generators
Python GeneratorsAkshar Raaj
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about goDvir Volk
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrencyjgrahamc
 
Golang concurrency design
Golang concurrency designGolang concurrency design
Golang concurrency designHyejong
 
Untitled presentation(4)
Untitled presentation(4)Untitled presentation(4)
Untitled presentation(4)chan20kaur
 
GoLightly: A Go Library For Building Virtual Machines
GoLightly: A Go Library For Building Virtual MachinesGoLightly: A Go Library For Building Virtual Machines
GoLightly: A Go Library For Building Virtual MachinesEleanor McHugh
 
Cotap Tech Talks: Keith Lazuka, Digital Communication using Sound and Swift
Cotap Tech Talks: Keith Lazuka, Digital Communication using Sound and SwiftCotap Tech Talks: Keith Lazuka, Digital Communication using Sound and Swift
Cotap Tech Talks: Keith Lazuka, Digital Communication using Sound and SwiftEvan Owen
 
To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2Bahul Neel Upadhyaya
 
シェル芸でライフハック(特論)
シェル芸でライフハック(特論)シェル芸でライフハック(特論)
シェル芸でライフハック(特論)Yuki Shimazaki
 

What's hot (20)

Basic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmersBasic c++ 11/14 for python programmers
Basic c++ 11/14 for python programmers
 
The Big Three
The Big ThreeThe Big Three
The Big Three
 
Mario
MarioMario
Mario
 
Go serving: Building server app with go
Go serving: Building server app with goGo serving: Building server app with go
Go serving: Building server app with go
 
Theming Plone with Deliverance
Theming Plone with DeliveranceTheming Plone with Deliverance
Theming Plone with Deliverance
 
Beauty and Power of Go
Beauty and Power of GoBeauty and Power of Go
Beauty and Power of Go
 
Why learn Internals?
Why learn Internals?Why learn Internals?
Why learn Internals?
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operations
 
Start Wrap Episode 11: A New Rope
Start Wrap Episode 11: A New RopeStart Wrap Episode 11: A New Rope
Start Wrap Episode 11: A New Rope
 
Python Generators
Python GeneratorsPython Generators
Python Generators
 
10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about go
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrency
 
constructors
constructorsconstructors
constructors
 
Golang concurrency design
Golang concurrency designGolang concurrency design
Golang concurrency design
 
Untitled presentation(4)
Untitled presentation(4)Untitled presentation(4)
Untitled presentation(4)
 
expression in cpp
expression in cppexpression in cpp
expression in cpp
 
GoLightly: A Go Library For Building Virtual Machines
GoLightly: A Go Library For Building Virtual MachinesGoLightly: A Go Library For Building Virtual Machines
GoLightly: A Go Library For Building Virtual Machines
 
Cotap Tech Talks: Keith Lazuka, Digital Communication using Sound and Swift
Cotap Tech Talks: Keith Lazuka, Digital Communication using Sound and SwiftCotap Tech Talks: Keith Lazuka, Digital Communication using Sound and Swift
Cotap Tech Talks: Keith Lazuka, Digital Communication using Sound and Swift
 
To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2
 
シェル芸でライフハック(特論)
シェル芸でライフハック(特論)シェル芸でライフハック(特論)
シェル芸でライフハック(特論)
 

Viewers also liked

Html5day
Html5dayHtml5day
Html5daymeco300
 
Fast Web Applications with Go
Fast Web Applications with Go Fast Web Applications with Go
Fast Web Applications with Go Eylem Ozekin
 
Building Web Applications in Go
Building Web Applications in GoBuilding Web Applications in Go
Building Web Applications in Gomicrypt
 
Html5 japan cup趣意書 2014020
Html5 japan cup趣意書 2014020Html5 japan cup趣意書 2014020
Html5 japan cup趣意書 2014020meco300
 
Why use Go for web development?
Why use Go for web development?Why use Go for web development?
Why use Go for web development?Weng Wei
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming GoWeng Wei
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with DataSeth Familian
 

Viewers also liked (7)

Html5day
Html5dayHtml5day
Html5day
 
Fast Web Applications with Go
Fast Web Applications with Go Fast Web Applications with Go
Fast Web Applications with Go
 
Building Web Applications in Go
Building Web Applications in GoBuilding Web Applications in Go
Building Web Applications in Go
 
Html5 japan cup趣意書 2014020
Html5 japan cup趣意書 2014020Html5 japan cup趣意書 2014020
Html5 japan cup趣意書 2014020
 
Why use Go for web development?
Why use Go for web development?Why use Go for web development?
Why use Go for web development?
 
Metaprogramming Go
Metaprogramming GoMetaprogramming Go
Metaprogramming Go
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with Data
 

Similar to Network server in go #gocon 2013-11-14

Go debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFxGo debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFxSignalFx
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoRodolfo Carvalho
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptKamil Toman
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io
 
Introduction to Griffon
Introduction to GriffonIntroduction to Griffon
Introduction to GriffonJames Williams
 
The Ring programming language version 1.5.4 book - Part 83 of 185
The Ring programming language version 1.5.4 book - Part 83 of 185The Ring programming language version 1.5.4 book - Part 83 of 185
The Ring programming language version 1.5.4 book - Part 83 of 185Mahmoud Samir Fayed
 
Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Itzik Kotler
 
Decision making - for loop , nested loop ,if-else statements , switch in goph...
Decision making - for loop , nested loop ,if-else statements , switch in goph...Decision making - for loop , nested loop ,if-else statements , switch in goph...
Decision making - for loop , nested loop ,if-else statements , switch in goph...sangam biradar
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثامنة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثامنةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثامنة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثامنةجامعة القدس المفتوحة
 
Java Performance Tips (So Code Camp San Diego 2014)
Java Performance Tips (So Code Camp San Diego 2014)Java Performance Tips (So Code Camp San Diego 2014)
Java Performance Tips (So Code Camp San Diego 2014)Kai Chan
 
Story Writing Byte Serializer in Golang
Story Writing Byte Serializer in GolangStory Writing Byte Serializer in Golang
Story Writing Byte Serializer in GolangHuy Do
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data sciencedeepak teja
 
Python GTK (Hacking Camp)
Python GTK (Hacking Camp)Python GTK (Hacking Camp)
Python GTK (Hacking Camp)Yuren Ju
 
Http2 on go1.6rc2
Http2 on go1.6rc2Http2 on go1.6rc2
Http2 on go1.6rc2Jxck Jxck
 
Антон Минашкин "Data transfering faster, stronger, better and not harder"
Антон Минашкин "Data transfering  faster, stronger, better and not harder"Антон Минашкин "Data transfering  faster, stronger, better and not harder"
Антон Минашкин "Data transfering faster, stronger, better and not harder"Fwdays
 
Data transfering: faster, stronger, better and not harder. UA Mobile 2016.
Data transfering: faster, stronger, better and not harder. UA Mobile 2016.Data transfering: faster, stronger, better and not harder. UA Mobile 2016.
Data transfering: faster, stronger, better and not harder. UA Mobile 2016.UA Mobile
 
Advanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriterAdvanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriterWeaveworks
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineMatt Provost
 
AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8Phil Eaton
 

Similar to Network server in go #gocon 2013-11-14 (20)

Go debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFxGo debugging and troubleshooting tips - from real life lessons at SignalFx
Go debugging and troubleshooting tips - from real life lessons at SignalFx
 
Golang
GolangGolang
Golang
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
Fantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and JavascriptFantom - Programming Language for JVM, CLR, and Javascript
Fantom - Programming Language for JVM, CLR, and Javascript
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
 
Introduction to Griffon
Introduction to GriffonIntroduction to Griffon
Introduction to Griffon
 
The Ring programming language version 1.5.4 book - Part 83 of 185
The Ring programming language version 1.5.4 book - Part 83 of 185The Ring programming language version 1.5.4 book - Part 83 of 185
The Ring programming language version 1.5.4 book - Part 83 of 185
 
Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)
 
Decision making - for loop , nested loop ,if-else statements , switch in goph...
Decision making - for loop , nested loop ,if-else statements , switch in goph...Decision making - for loop , nested loop ,if-else statements , switch in goph...
Decision making - for loop , nested loop ,if-else statements , switch in goph...
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثامنة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثامنةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثامنة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثامنة
 
Java Performance Tips (So Code Camp San Diego 2014)
Java Performance Tips (So Code Camp San Diego 2014)Java Performance Tips (So Code Camp San Diego 2014)
Java Performance Tips (So Code Camp San Diego 2014)
 
Story Writing Byte Serializer in Golang
Story Writing Byte Serializer in GolangStory Writing Byte Serializer in Golang
Story Writing Byte Serializer in Golang
 
Python introduction towards data science
Python introduction towards data sciencePython introduction towards data science
Python introduction towards data science
 
Python GTK (Hacking Camp)
Python GTK (Hacking Camp)Python GTK (Hacking Camp)
Python GTK (Hacking Camp)
 
Http2 on go1.6rc2
Http2 on go1.6rc2Http2 on go1.6rc2
Http2 on go1.6rc2
 
Антон Минашкин "Data transfering faster, stronger, better and not harder"
Антон Минашкин "Data transfering  faster, stronger, better and not harder"Антон Минашкин "Data transfering  faster, stronger, better and not harder"
Антон Минашкин "Data transfering faster, stronger, better and not harder"
 
Data transfering: faster, stronger, better and not harder. UA Mobile 2016.
Data transfering: faster, stronger, better and not harder. UA Mobile 2016.Data transfering: faster, stronger, better and not harder. UA Mobile 2016.
Data transfering: faster, stronger, better and not harder. UA Mobile 2016.
 
Advanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriterAdvanced Patterns with io.ReadWriter
Advanced Patterns with io.ReadWriter
 
Rust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command LineRust LDN 24 7 19 Oxidising the Command Line
Rust LDN 24 7 19 Oxidising the Command Line
 
AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8AOT-compilation of JavaScript with V8
AOT-compilation of JavaScript with V8
 

More from Jxck Jxck

ORTC SVC SimulCast
ORTC SVC SimulCastORTC SVC SimulCast
ORTC SVC SimulCastJxck Jxck
 
HTTP2 時代の Web - web over http2
HTTP2 時代の Web - web over http2HTTP2 時代の Web - web over http2
HTTP2 時代の Web - web over http2Jxck Jxck
 
Isomorphic Architecture & Interface
Isomorphic Architecture & InterfaceIsomorphic Architecture & Interface
Isomorphic Architecture & InterfaceJxck Jxck
 
HTTP2 RFC 発行記念祝賀会
HTTP2 RFC 発行記念祝賀会HTTP2 RFC 発行記念祝賀会
HTTP2 RFC 発行記念祝賀会Jxck Jxck
 
Extensible web #html5j
Extensible web #html5jExtensible web #html5j
Extensible web #html5jJxck Jxck
 
Extensible web
Extensible webExtensible web
Extensible webJxck Jxck
 
HTTP2Study chronicle #http2conf
HTTP2Study chronicle #http2confHTTP2Study chronicle #http2conf
HTTP2Study chronicle #http2confJxck Jxck
 
mozaicfm-ep8 #altJS @ll-diver
mozaicfm-ep8 #altJS @ll-divermozaicfm-ep8 #altJS @ll-diver
mozaicfm-ep8 #altJS @ll-diverJxck Jxck
 
Updates of socket.io@1.0
Updates of socket.io@1.0Updates of socket.io@1.0
Updates of socket.io@1.0Jxck Jxck
 
Why HTML Form dose not support PUT & DELETE ?
Why HTML Form dose not support PUT & DELETE ?Why HTML Form dose not support PUT & DELETE ?
Why HTML Form dose not support PUT & DELETE ?Jxck Jxck
 
Next generation web talk @cross2014
Next generation web talk @cross2014Next generation web talk @cross2014
Next generation web talk @cross2014Jxck Jxck
 
HTTP2 & HPACK #pyfes 2013-11-30
HTTP2 & HPACK #pyfes 2013-11-30HTTP2 & HPACK #pyfes 2013-11-30
HTTP2 & HPACK #pyfes 2013-11-30Jxck Jxck
 
SPDY & HTTP2.0 & QUIC - #bpstudy 2013-08-28
SPDY & HTTP2.0 & QUIC - #bpstudy 2013-08-28SPDY & HTTP2.0 & QUIC - #bpstudy 2013-08-28
SPDY & HTTP2.0 & QUIC - #bpstudy 2013-08-28Jxck Jxck
 
Http2.0 Guide 2013-08-14 #http2study
Http2.0 Guide 2013-08-14 #http2studyHttp2.0 Guide 2013-08-14 #http2study
Http2.0 Guide 2013-08-14 #http2studyJxck Jxck
 
Gtug girls meetup web socket handson
Gtug girls meetup   web socket handsonGtug girls meetup   web socket handson
Gtug girls meetup web socket handsonJxck Jxck
 
Next generation web talk @cross2013
Next generation web talk @cross2013Next generation web talk @cross2013
Next generation web talk @cross2013Jxck Jxck
 
Nodefest2011-Live
Nodefest2011-LiveNodefest2011-Live
Nodefest2011-LiveJxck Jxck
 
Test it in Node.js
Test it in Node.jsTest it in Node.js
Test it in Node.jsJxck Jxck
 
Real Time App with Node.js
Real Time App with Node.jsReal Time App with Node.js
Real Time App with Node.jsJxck Jxck
 
I visited JSConf + NodeConf + Joyent
I visited JSConf + NodeConf + JoyentI visited JSConf + NodeConf + Joyent
I visited JSConf + NodeConf + JoyentJxck Jxck
 

More from Jxck Jxck (20)

ORTC SVC SimulCast
ORTC SVC SimulCastORTC SVC SimulCast
ORTC SVC SimulCast
 
HTTP2 時代の Web - web over http2
HTTP2 時代の Web - web over http2HTTP2 時代の Web - web over http2
HTTP2 時代の Web - web over http2
 
Isomorphic Architecture & Interface
Isomorphic Architecture & InterfaceIsomorphic Architecture & Interface
Isomorphic Architecture & Interface
 
HTTP2 RFC 発行記念祝賀会
HTTP2 RFC 発行記念祝賀会HTTP2 RFC 発行記念祝賀会
HTTP2 RFC 発行記念祝賀会
 
Extensible web #html5j
Extensible web #html5jExtensible web #html5j
Extensible web #html5j
 
Extensible web
Extensible webExtensible web
Extensible web
 
HTTP2Study chronicle #http2conf
HTTP2Study chronicle #http2confHTTP2Study chronicle #http2conf
HTTP2Study chronicle #http2conf
 
mozaicfm-ep8 #altJS @ll-diver
mozaicfm-ep8 #altJS @ll-divermozaicfm-ep8 #altJS @ll-diver
mozaicfm-ep8 #altJS @ll-diver
 
Updates of socket.io@1.0
Updates of socket.io@1.0Updates of socket.io@1.0
Updates of socket.io@1.0
 
Why HTML Form dose not support PUT & DELETE ?
Why HTML Form dose not support PUT & DELETE ?Why HTML Form dose not support PUT & DELETE ?
Why HTML Form dose not support PUT & DELETE ?
 
Next generation web talk @cross2014
Next generation web talk @cross2014Next generation web talk @cross2014
Next generation web talk @cross2014
 
HTTP2 & HPACK #pyfes 2013-11-30
HTTP2 & HPACK #pyfes 2013-11-30HTTP2 & HPACK #pyfes 2013-11-30
HTTP2 & HPACK #pyfes 2013-11-30
 
SPDY & HTTP2.0 & QUIC - #bpstudy 2013-08-28
SPDY & HTTP2.0 & QUIC - #bpstudy 2013-08-28SPDY & HTTP2.0 & QUIC - #bpstudy 2013-08-28
SPDY & HTTP2.0 & QUIC - #bpstudy 2013-08-28
 
Http2.0 Guide 2013-08-14 #http2study
Http2.0 Guide 2013-08-14 #http2studyHttp2.0 Guide 2013-08-14 #http2study
Http2.0 Guide 2013-08-14 #http2study
 
Gtug girls meetup web socket handson
Gtug girls meetup   web socket handsonGtug girls meetup   web socket handson
Gtug girls meetup web socket handson
 
Next generation web talk @cross2013
Next generation web talk @cross2013Next generation web talk @cross2013
Next generation web talk @cross2013
 
Nodefest2011-Live
Nodefest2011-LiveNodefest2011-Live
Nodefest2011-Live
 
Test it in Node.js
Test it in Node.jsTest it in Node.js
Test it in Node.js
 
Real Time App with Node.js
Real Time App with Node.jsReal Time App with Node.js
Real Time App with Node.js
 
I visited JSConf + NodeConf + Joyent
I visited JSConf + NodeConf + JoyentI visited JSConf + NodeConf + Joyent
I visited JSConf + NodeConf + Joyent
 

Recently uploaded

Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 

Recently uploaded (20)

Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 

Network server in go #gocon 2013-11-14

  • 1. NetWork & IO I/O and binary programming 2013/11/14 #GoCon
  • 2. ● ● ● ● ● ● id: Jxck github: Jxck twitter: jxck_ about: http://jxck.io blog: http://d.hatena.ne.jp/jxck Love: music Jack
  • 5. Now working on ... ● http2 server/client ○ ○ ○ ○ github.com/Jxck/http2 github.com/Jxck/hpack github.com/Jxck/logger github.com/Jxck/color ● Caution ○ ○ ○ ○ not enough error handling not enough test not enough api/feature under heavy working :p
  • 6. network server I/O and binary programming
  • 7. NetWork Server in Go ● ● ● ● ● ● ● net []byte bytes encoding/binary io io/ioutil bufio
  • 8. No Error Handling in Sample Codes func main() { listener, err := net.Listen("tcp", "127.0.0.1:3000") // err if err != nil { log.Fatal(err) } for { conn, err := listener.Accept() // err if err != nil { log.Fatal(err) } defer func() { log.Println("close connection") conn.Close() }() _, err = conn.Write([]byte("hellon")) // send hello if err != nil { log.Fatal(err) } } } Hard To Read on Slide
  • 10. TCP Server package main import "net" func main() { listener, _ := net.Listen("tcp", ":3000") for { conn, _ := listener.Accept() conn.Write([]byte("hellon")) conn.Close() } }
  • 11. TCP Server With Handler func main() { listener, _ := net.Listen("tcp", ":3000") for { conn, _ := listener.Accept() handleConn(conn) } } func handleConn(conn net.Conn) { conn.Write([]byte("hellon")) }
  • 12. TCP Server with Goroutine func main() { listener, _ := net.Listen("tcp", ":3000") for { conn, _ := listener.Accept() go handleConn(conn) } } func handleConn(conn net.Conn) { conn.Write([]byte("hellon")) }
  • 14. binary format (example) R Length(14) R Reserved(8) Type(8) Flags(8) Stream Identifier(31) Setting Identifier(24) Value(32) []byte{0, 8, 4, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 16, 0}
  • 15. low level byte slice handling func main() { b := []byte{} b = append(b, []byte{0, b = append(b, 4) b = append(b, 0) // stream id 1 b = append(b, []byte{0, // settings id 1 b = append(b, []byte{0, // value 4096 b = append(b, []byte{0, fmt.Println(b) // [0 8 4 0 0 0 0 1 0 0 } 8}...) 0, 0, 1}...) 0, 0, 1}...) 0, 16, 0}...) 0 1 0 0 16 0] // length 8 // type 4 // flags 0
  • 16. SWrap (slice wrap) import “github.com/Jxck/swrap” type SWrap ● ● ● ● ● ● ● ● ● ● ● ● func func func func func func func func func func func func New(a []byte) SWrap (sw *SWrap) Add(a byte) (sw *SWrap) Bytes() []byte (sw *SWrap) Compare(b []byte) bool (sw *SWrap) Delete(i int) (sw *SWrap) Len() int (sw *SWrap) Merge(a []byte) (sw *SWrap) Pop() byte (sw *SWrap) Push(b byte) (sw *SWrap) Replace(i int, b byte) (sw *SWrap) Shift(b byte) (sw *SWrap) UnShift() byte
  • 18. important: io.Reader, io.Writer type Reader interface { Read(p []byte) (n int, err error) } type Writer interface { Write(p []byte) (n int, err error) } type ReadWriter interface { Reader Writer }
  • 19. net.Conn implements io.Writer func handleConn(conn net.Conn) { conn.Write([]byte("hellon")) } func handleConn(conn io.Writer) { conn.Write( []byte{104, 101, 108, 108, 111, 10} ) }
  • 20. import “bytes” utility for byte slice and Buffer
  • 21. bytes.Buffer: io.ReadWriter for []byte func handleConn(conn io.Writer) { conn.Write([]byte{104, 101, 108, 108, 111, 10}) } func main() { b := []byte{} buf := bytes.NewBuffer(b) // io.ReadeWriter handleConn(buf) actual := buf.Bytes() bytes.Equal( actual, []byte{104, 101, 108, 108, 111, 10}, ) // true }
  • 23. we did... func main() { b := []byte{} b = append(b, []byte{0, b = append(b, 4) b = append(b, 0) // stream id 1 b = append(b, []byte{0, // settings id 1 b = append(b, []byte{0, // value 4096 b = append(b, []byte{0, fmt.Println(b) // [0 8 4 0 0 0 0 1 0 0 } 8}...) 0, 0, 1}...) 0, 0, 1}...) 0, 16, 0}...) 0 1 0 0 16 0] // length 8 // type 4 // flags 0
  • 24. Fixed Size R Length(14) R Type(8) Flags(8) Stream Identifier(31) Reserved(8) Setting Identifier(24) Value(32) var var var var Length uint16 = 8 Type, Flags uint8 = 4, 0 StreamId, SettingsId uint32 = 1, 1 Value uint32 = 4096
  • 25. encoding/binary.Write() buf var var var var := bytes.NewBuffer([]byte{}) Length uint16 = 8 Type, Flags uint8 = 4, 0 StreamId, SettingsId uint32 = 1, 1 Value uint32 = 4096 binary.Write(buf, binary.BigEndian, Length) binary.Write(buf, binary.BigEndian, Type) // ... binary.Write(buf, binary.BigEndian, Value) fmt.Println(buf.Bytes()) // [0 8 4 0 0 0 0 1 0 0 0 1 0 0 16 0]
  • 26. encoding/binary.Write() buf := bytes.NewBuffer([]byte{}) frame := Frame{ Length: Type: Flags: StreamId: SettingsId: Value: } 8, 4, 0, 1, 1, 4096, type Frame struct { Length uint16 Type uint8 Flags uint8 StreamId uint32 SettingsId uint32 Value uint32 } // func Write(w io.Writer, order ByteOrder, data interface{}) error binary.Write(buf, binary.BigEndian, frame) fmt.Println(buf.Bytes())
  • 28. TCP Client func main() { conn, _ := net.Dial("tcp", ":3000") b := make([]byte, 100) n, _ := conn.Read(b) fmt.Print(string(b[:n])) // hello }
  • 30. bufio := []byte(4096) + io // bufio.Reader type Reader struct {} // bufio.Writer type Writer struct {} func NewReader(rd io.Reader) *Reader { // convert io.Reader to *bufio.Reader } func NewWriter(wr io.Writer) *Writer { // convert io.Writer to *bufio.Writer }
  • 31. TCP Client with bufio func main() { conn, _ := net.Dial("tcp", ":3000") br := bufio.NewReader(conn) line, _ := br.ReadString('n') fmt.Print(line) // hello }
  • 32. TCP Client with encoding/binary type Frame struct { Length uint16 Type uint8 Flags uint8 StreamId uint32 SettingsId uint32 Value uint32 } func main() { conn, _ := net.Dial("tcp", ":3000") frame := &Frame{} binary.Read(conn, binary.BigEndian, frame) fmt.Print(frame) // &{8 4 0 1 1 4096} }
  • 34. HTTP/2.0 with Go ● tcp/tls connection ○ net ● multiplexed stream ○ goroutine / channel ● binary frame ○ static type / encoding/binary ● crypto ○ crypto ● build ○ go build (cross compile) ● test ○ go test (and testing)
  • 35. HTTP2.0 Study (#http2study) ● http2.0 勉強会 #2 ○ http://bit.ly/158zE4C ● http2.0 hackathon ○ 12月 or 1月