Created
January 29, 2018 00:59
-
-
Save singchia/d725754dbc7f83c9fa75be6da0b28326 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"flag" | |
"net" | |
"os" | |
"time" | |
"github.com/singchia/go-hammer/circulinker" | |
) | |
func main() { | |
var addr string | |
flag.StringVar(&addr, "addr", ":1202", "listen addr") | |
flag.Parse() | |
l, err := net.Listen("tcp", addr) | |
if err != nil { | |
os.Exit(1) | |
} | |
defer l.Close() | |
connMap := make(map[net.Conn]circulinker.CircuID) | |
cl := circulinker.NewCirculinker() | |
for i := 0; i < 100; i++ { | |
go func() { | |
buf := make([]byte, 1024) | |
for { | |
time.Sleep(time.Microsecond * 50) | |
if len(connMap) == 0 { | |
continue | |
} | |
cl.Rightshift() | |
conn := cl.RetrieveCur().(net.Conn) | |
conn.SetReadDeadline(time.Now().Add(time.Microsecond * 50)) | |
len, err := conn.Read(buf) | |
E, ok := err.(net.Error) | |
if ok && E.Timeout() { | |
continue | |
} | |
if err != nil { | |
conn.Close() | |
cl.Delete(connMap[conn]) | |
delete(connMap, conn) | |
continue | |
} | |
conn.Write(append([]byte(addr+" "), buf[:len]...)) | |
} | |
}() | |
} | |
for { | |
conn, err := l.Accept() | |
if err != nil { | |
os.Exit(1) | |
} | |
id := cl.Add(conn) | |
connMap[conn] = id | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment