forked from gnet-io/gnet-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2019 Xu Xu. All rights reserved. | ||
// Copyright 2017 Joshua J Baker. All rights reserved. | ||
|
||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Allenxuxu/eviop" | ||
"github.com/Allenxuxu/ringbuffer" | ||
) | ||
|
||
func main() { | ||
var port int | ||
var loops int | ||
var udp bool | ||
var trace bool | ||
var reuseport bool | ||
|
||
flag.IntVar(&port, "port", 5000, "server port") | ||
flag.BoolVar(&udp, "udp", false, "listen on udp") | ||
flag.BoolVar(&reuseport, "reuseport", false, "reuseport (SO_REUSEPORT)") | ||
flag.BoolVar(&trace, "trace", false, "print packets to console") | ||
flag.IntVar(&loops, "loops", 0, "num loops") | ||
flag.Parse() | ||
|
||
var events eviop.Events | ||
events.NumLoops = loops | ||
events.Serving = func(srv eviop.Server) (action eviop.Action) { | ||
log.Printf("echo server started on port %d (loops: %d)", port, srv.NumLoops) | ||
if reuseport { | ||
log.Printf("reuseport") | ||
} | ||
return | ||
} | ||
events.Data = func(c *eviop.Conn, in *ringbuffer.RingBuffer) (out []byte, action eviop.Action) { | ||
first, end := in.PeekAll() | ||
if trace { | ||
log.Printf("%s", strings.TrimSpace(string(first)+string(end))) | ||
} | ||
out = first | ||
if len(end) > 0 { | ||
out = append(out, end...) | ||
} | ||
in.RetrieveAll() | ||
return | ||
} | ||
scheme := "tcp" | ||
if udp { | ||
scheme = "udp" | ||
} | ||
log.Fatal(eviop.Serve(events, time.Second*10, fmt.Sprintf("%s://:%d?reuseport=%t", scheme, port, reuseport))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"strconv" | ||
|
||
"github.com/Allenxuxu/gev" | ||
"github.com/Allenxuxu/gev/connection" | ||
"github.com/Allenxuxu/ringbuffer" | ||
) | ||
|
||
type example struct{} | ||
|
||
func (s *example) OnConnect(c *connection.Connection) { | ||
//log.Println(" OnConnect : ", c.PeerAddr()) | ||
} | ||
func (s *example) OnMessage(c *connection.Connection, buffer *ringbuffer.RingBuffer) (out []byte) { | ||
//log.Println("OnMessage") | ||
first, end := buffer.PeekAll() | ||
out = first | ||
if len(end) > 0 { | ||
out = append(out, end...) | ||
} | ||
buffer.RetrieveAll() | ||
return | ||
} | ||
|
||
func (s *example) OnClose(c *connection.Connection) { | ||
//log.Println("OnClose") | ||
} | ||
|
||
func main() { | ||
handler := new(example) | ||
var port int | ||
var loops int | ||
|
||
flag.IntVar(&port, "port", 1833, "server port") | ||
flag.IntVar(&loops, "loops", -1, "num loops") | ||
flag.Parse() | ||
|
||
s, err := gev.NewServer(handler, | ||
gev.Network("tcp"), | ||
gev.Address(":"+strconv.Itoa(port)), | ||
gev.NumLoops(loops)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
s.Start() | ||
} |