SlideShare una empresa de Scribd logo
1 de 37
Descargar para leer sin conexión
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
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 :)

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 
シェル芸でライフハック(特論)
シェル芸でライフハック(特論)シェル芸でライフハック(特論)
シェル芸でライフハック(特論)
 

Destacado

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
 

Destacado (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 a 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. 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
 
Антон Минашкин "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
 
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 a 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. 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.
 
Антон Минашкин "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"
 
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
 

Más de 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
 

Más de 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
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 

Último (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 

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
  • 3.
  • 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月