Skip to content

Commit

Permalink
wip mt
Browse files Browse the repository at this point in the history
  • Loading branch information
tidwall committed Oct 28, 2017
1 parent d4d7bca commit 17aa5a0
Show file tree
Hide file tree
Showing 7 changed files with 727 additions and 238 deletions.
29 changes: 10 additions & 19 deletions doppio.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Options struct {
// Events represents server events
type Events struct {
// Serving fires when the server can accept connections.
// The wake parameter is a goroutine-safe function that's triggers
// The wake parameter is a goroutine-safe function that triggers
// a Data event (with a nil `in` parameter) for the specified id.
Serving func(wake func(id int) bool) (action Action)
// Opened fires when a new connection has opened.
Expand Down Expand Up @@ -81,7 +81,7 @@ func Serve(events Events, addr ...string) error {
ln.close()
}
}()
var compat bool
var stdlib bool
for _, addr := range addr {
ln := listener{network: "tcp", addr: addr}
if strings.Contains(addr, "://") {
Expand All @@ -91,7 +91,7 @@ func Serve(events Events, addr ...string) error {
ln.network = "unix"
}
if strings.HasSuffix(ln.network, "-stdlib") {
compat = true
stdlib = true
ln.network = ln.network[:len(ln.network)-7]
}
if ln.network == "unix" {
Expand All @@ -102,14 +102,14 @@ func Serve(events Events, addr ...string) error {
if err != nil {
return err
}
if !compat {
if !stdlib {
if err := ln.system(); err != nil {
return err
}
}
lns = append(lns, &ln)
}
if compat {
if stdlib {
return servestdlib(events, lns)
}
return serve(events, lns)
Expand All @@ -125,6 +125,7 @@ type cconn struct {
detached bool
}

// servestdlib uses the stdlib net package instead of syscalls.
func servestdlib(events Events, lns []*listener) error {
type fail struct{ err error }
type accept struct{ conn net.Conn }
Expand Down Expand Up @@ -325,13 +326,8 @@ again:
unlock()
action := events.Prewrite(c.id, len(c.outbuf[c.outpos:]))
lock()
switch action {
case Shutdown:
c.action = Shutdown
case Close:
if c.action != Shutdown {
c.action = Close
}
if action > c.action {
c.action = action
}
}
c.conn.SetWriteDeadline(time.Now().Add(time.Millisecond))
Expand All @@ -344,13 +340,8 @@ again:
unlock()
action := events.Postwrite(c.id, amount, len(c.outbuf)-c.outpos-amount)
lock()
switch action {
case Shutdown:
c.action = Shutdown
case Close:
if c.action != Shutdown {
c.action = Close
}
if action > c.action {
c.action = action
}
}
if err != nil {
Expand Down
14 changes: 2 additions & 12 deletions doppio_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,8 @@ func serve(events Events, lns []*listener) error {
unlock()
action := events.Prewrite(c.id, len(c.outbuf[c.outpos:]))
lock()
switch action {
case Shutdown:
if action == Shutdown {
c.action = Shutdown
case Close:
if c.action != Shutdown {
c.action = Close
}
}
}
n, err = syscall.Write(c.fd, c.outbuf[c.outpos:])
Expand All @@ -257,13 +252,8 @@ func serve(events Events, lns []*listener) error {
unlock()
action := events.Postwrite(c.id, amount, len(c.outbuf)-c.outpos-amount)
lock()
switch action {
case Shutdown:
if action == Shutdown {
c.action = Shutdown
case Close:
if c.action != Shutdown {
c.action = Close
}
}
}
if n == 0 || err != nil {
Expand Down
11 changes: 7 additions & 4 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# `✨ shiny ✨ examples`
# `doppio examples`

## redis-server

Runs on port 6380

```
go run examples/redis-server/main.go [--port int] [--appendonly yes/no]
go run examples/redis-server/main.go
```

- `GET`, `SET`, `DEL`, `QUIT`, `PING`, `SHUTDOWN` commands.
- `--appendonly yes` option for disk persistence.
- Compatible with the [redis-cli](https://redis.io/topics/rediscli) and all [redis clients](https://redis.io/clients).


## echo-server

Runs on port 9999

```
go run examples/echo-server/main.go [--port int]
go run examples/echo-server/main.go
```
87 changes: 32 additions & 55 deletions examples/echo-server/main.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,41 @@
package main

import (
"flag"
"fmt"
"log"

"github.com/tidwall/shiny"
"github.com/tidwall/doppio"
)

func main() {
var port int
flag.IntVar(&port, "port", 9999, "server port")
flag.Parse()

var shutdown bool
var started bool
fmt.Println(shiny.Serve("tcp", fmt.Sprintf(":%d", port),
// handle - the incoming client socket data.
func(id int, data []byte, ctx interface{}) (send []byte, keepopen bool) {
if shutdown {
return nil, false
}
keepopen = true
if string(data) == "shutdown\r\n" {
shutdown = true
} else if string(data) == "quit\r\n" {
keepopen = false
}
return data, keepopen
},
// accept - a new client socket has opened.
// 'wake' is a function that when called will fire a 'handle' event
// for the specified ID, and is goroutine-safe.
func(id int, addr string, wake func(), ctx interface{}) (send []byte, keepopen bool) {
if shutdown {
return nil, false
}
// this is a good place to create a user-defined socket context.
return []byte(
"Welcome to the echo server!\n" +
"Enter 'quit' to close your connection or " +
"'shutdown' to close the server.\n"), true
},
// closed - a client socket has closed
func(id int, err error, ctx interface{}) {
// teardown the socket context here
},
// ticker - a ticker that fires between 1 and 1/20 of a second
// depending on the traffic.
func(ctx interface{}) (keepserveropen bool) {
if shutdown {
// do server teardown here
return false
}
if !started {
fmt.Printf("echo server started on port %d\n", port)
started = true
}
// perform various non-socket-io related operation here
return true
},
// an optional user-defined context
nil))
var events doppio.Events

// Serving fires when the server can accept connections.
events.Serving = func(wake func(id int) bool) (action doppio.Action) {
log.Print("echo server started on port 5000")
return
}

// // Accept a new client connection for the specified ID.
// events.Opened = func(id int, addr string) (out []byte, opts doppio.Options, action doppio.Action) {
// // This is a good place to create a user-defined connection context.
// out = []byte("Welcome to the echo server!\r\n" +
// "Enter 'quit' to close your connection or " +
// "'shutdown' to close the server.\r\n")
// return
// }

// Handle incoming data.
events.Data = func(id int, in []byte) (out []byte, action doppio.Action) {
out = in
// if string(in) == "shutdown\r\n" {
// action = doppio.Shutdown
// } else if string(in) == "quit\r\n" {
// action = doppio.Close
// }
// out = in
return
}

log.Fatal(doppio.Serve(events, "tcp://0.0.0.0:5000", "unix://socket"))
}
Loading

0 comments on commit 17aa5a0

Please sign in to comment.