forked from panjf2000/gnet
-
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.
Support Windows with net stdlib, fix panjf2000#19
- Loading branch information
Showing
23 changed files
with
754 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ env: | |
os: | ||
- linux | ||
- osx | ||
- windows | ||
go: | ||
- "1.11.x" | ||
- "1.12.x" | ||
|
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
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,35 @@ | ||
// Copyright 2019 Andy Pan. All rights reserved. | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build linux darwin netbsd freebsd openbsd dragonfly | ||
|
||
package gnet | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func (svr *server) acceptNewConnection(fd int) error { | ||
nfd, sa, err := unix.Accept(fd) | ||
if err != nil { | ||
if err == unix.EAGAIN { | ||
return nil | ||
} | ||
return err | ||
} | ||
if err := unix.SetNonblock(nfd, true); err != nil { | ||
return err | ||
} | ||
lp := svr.subLoopGroup.next() | ||
c := newConn(nfd, lp, sa) | ||
_ = lp.poller.Trigger(func() (err error) { | ||
if err = lp.poller.AddRead(nfd); err != nil { | ||
return | ||
} | ||
lp.connections[nfd] = c | ||
err = lp.loopOpen(c) | ||
return | ||
}) | ||
return nil | ||
} |
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,69 @@ | ||
// Copyright 2019 Andy Pan. All rights reserved. | ||
// Copyright 2018 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. | ||
|
||
// +build windows | ||
|
||
package gnet | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/panjf2000/gnet/pool" | ||
"github.com/panjf2000/gnet/ringbuffer" | ||
) | ||
|
||
func (svr *server) listenerRun() { | ||
var err error | ||
defer svr.signalShutdown(err) | ||
var packet [0xFFFF]byte | ||
inBuf := svr.bytesPool.Get().(*ringbuffer.RingBuffer) | ||
bytesPool := pool.NewBytesPool() | ||
for { | ||
if svr.ln.pconn != nil { | ||
// Read data from UDP socket. | ||
n, addr, e := svr.ln.pconn.ReadFrom(packet[:]) | ||
if e != nil { | ||
err = e | ||
return | ||
} | ||
buf := bytesPool.GetLen(n) | ||
copy(buf, packet[:n]) | ||
|
||
lp := svr.subLoopGroup.next() | ||
c := &stdConn{ | ||
loop: lp, | ||
localAddr: svr.ln.lnaddr, | ||
remoteAddr: addr, | ||
inboundBuffer: inBuf, | ||
cache: buf, | ||
} | ||
lp.ch <- &udpIn{c} | ||
} else { | ||
// Accept TCP socket. | ||
conn, e := svr.ln.ln.Accept() | ||
if e != nil { | ||
err = e | ||
return | ||
} | ||
lp := svr.subLoopGroup.next() | ||
c := newConn(conn, lp) | ||
lp.ch <- c | ||
go func() { | ||
var packet [0xFFFF]byte | ||
for { | ||
n, err := c.conn.Read(packet[:]) | ||
if err != nil { | ||
_ = c.conn.SetReadDeadline(time.Time{}) | ||
lp.ch <- &stderr{c, err} | ||
return | ||
} | ||
buf := bytesPool.GetLen(n) | ||
copy(buf, packet[:n]) | ||
lp.ch <- &tcpIn{c, buf} | ||
} | ||
}() | ||
} | ||
} | ||
} |
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
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,148 @@ | ||
// Copyright 2019 Andy Pan. All rights reserved. | ||
// Copyright 2018 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. | ||
|
||
// +build windows | ||
|
||
package gnet | ||
|
||
import ( | ||
"github.com/panjf2000/gnet/pool" | ||
"net" | ||
|
||
"github.com/panjf2000/gnet/ringbuffer" | ||
) | ||
|
||
type stderr struct { | ||
c *stdConn | ||
err error | ||
} | ||
|
||
type wakeReq struct { | ||
c *stdConn | ||
} | ||
|
||
type tcpIn struct { | ||
c *stdConn | ||
in []byte | ||
} | ||
|
||
type udpIn struct { | ||
c *stdConn | ||
} | ||
|
||
type stdConn struct { | ||
ctx interface{} // user-defined context | ||
conn net.Conn // original connection | ||
loop *loop // owner loop | ||
done int32 // 0: attached, 1: closed | ||
cache []byte // reuse memory of inbound data | ||
localAddr net.Addr // local server addr | ||
remoteAddr net.Addr // remote peer addr | ||
inboundBuffer *ringbuffer.RingBuffer // buffer for data from client | ||
} | ||
|
||
func newConn(conn net.Conn, lp *loop) *stdConn { | ||
return &stdConn{ | ||
conn: conn, | ||
loop: lp, | ||
inboundBuffer: lp.svr.bytesPool.Get().(*ringbuffer.RingBuffer), | ||
} | ||
} | ||
|
||
func (c *stdConn) release() { | ||
//c.conn = nil | ||
c.ctx = nil | ||
c.localAddr = nil | ||
c.remoteAddr = nil | ||
c.inboundBuffer.Reset() | ||
c.loop.svr.bytesPool.Put(c.inboundBuffer) | ||
c.inboundBuffer = nil | ||
pool.PutBytes(c.cache) | ||
c.cache = nil | ||
} | ||
|
||
// ================================= Public APIs of gnet.Conn ================================= | ||
|
||
func (c *stdConn) ReadFrame() []byte { | ||
buf, _ := c.loop.svr.codec.Decode(c) | ||
return buf | ||
} | ||
|
||
func (c *stdConn) Read() []byte { | ||
if c.inboundBuffer.IsEmpty() { | ||
return c.cache | ||
} | ||
head, _ := c.inboundBuffer.LazyReadAll() | ||
return append(head, c.cache...) | ||
} | ||
|
||
func (c *stdConn) ResetBuffer() { | ||
c.cache = c.cache[:0] | ||
c.inboundBuffer.Reset() | ||
} | ||
|
||
func (c *stdConn) ReadN(n int) (size int, buf []byte) { | ||
oneOffBufferLen := len(c.cache) | ||
inBufferLen := c.inboundBuffer.Length() | ||
if inBufferLen+oneOffBufferLen < n { | ||
return | ||
} | ||
if c.inboundBuffer.IsEmpty() { | ||
size = n | ||
buf = c.cache[:n] | ||
if n == oneOffBufferLen { | ||
c.cache = c.cache[:0] | ||
} else { | ||
c.cache = c.cache[n:] | ||
} | ||
return | ||
} | ||
size = n | ||
buf, tail := c.inboundBuffer.LazyRead(n) | ||
if tail != nil { | ||
buf = append(buf, tail...) | ||
} | ||
if inBufferLen >= n { | ||
c.inboundBuffer.Shift(n) | ||
return | ||
} | ||
c.inboundBuffer.Reset() | ||
|
||
restSize := n - inBufferLen | ||
buf = append(buf, c.cache[:restSize]...) | ||
if restSize == oneOffBufferLen { | ||
c.cache = c.cache[:0] | ||
} else { | ||
c.cache = c.cache[restSize:] | ||
} | ||
return | ||
} | ||
|
||
func (c *stdConn) InboundBuffer() *ringbuffer.RingBuffer { | ||
return c.inboundBuffer | ||
} | ||
|
||
func (c *stdConn) OutboundBuffer() *ringbuffer.RingBuffer { | ||
return nil | ||
} | ||
|
||
func (c *stdConn) BufferLength() int { | ||
return c.inboundBuffer.Length() + len(c.cache) | ||
} | ||
|
||
func (c *stdConn) AsyncWrite(buf []byte) { | ||
if encodedBuf, err := c.loop.svr.codec.Encode(buf); err == nil { | ||
c.loop.ch <- func() error { | ||
_, _ = c.conn.Write(encodedBuf) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
func (c *stdConn) Context() interface{} { return c.ctx } | ||
func (c *stdConn) SetContext(ctx interface{}) { c.ctx = ctx } | ||
func (c *stdConn) LocalAddr() net.Addr { return c.localAddr } | ||
func (c *stdConn) RemoteAddr() net.Addr { return c.remoteAddr } | ||
func (c *stdConn) Wake() { c.loop.ch <- wakeReq{c} } |
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
Oops, something went wrong.