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.
- Loading branch information
Showing
20 changed files
with
400 additions
and
241 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
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,83 @@ | ||
// 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 darwin netbsd freebsd openbsd dragonfly linux | ||
|
||
package gnet | ||
|
||
import ( | ||
"net" | ||
"os" | ||
|
||
"github.com/panjf2000/gnet/ringbuffer" | ||
"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 := &conn{ | ||
fd: nfd, | ||
sa: sa, | ||
loop: lp, | ||
inboundBuffer: ringbuffer.New(socketRingBufferSize), | ||
outboundBuffer: ringbuffer.New(socketRingBufferSize), | ||
} | ||
_ = lp.loopOpen(c) | ||
_ = lp.poller.Trigger(func() (err error) { | ||
if err = lp.poller.AddRead(nfd); err == nil { | ||
lp.connections[nfd] = c | ||
return | ||
} | ||
return | ||
}) | ||
return nil | ||
} | ||
|
||
func (ln *listener) close() { | ||
if ln.f != nil { | ||
sniffError(ln.f.Close()) | ||
} | ||
if ln.ln != nil { | ||
sniffError(ln.ln.Close()) | ||
} | ||
if ln.pconn != nil { | ||
sniffError(ln.pconn.Close()) | ||
} | ||
if ln.network == "unix" { | ||
sniffError(os.RemoveAll(ln.addr)) | ||
} | ||
} | ||
|
||
// system takes the net listener and detaches it from it's parent | ||
// event loop, grabs the file descriptor, and makes it non-blocking. | ||
func (ln *listener) system() error { | ||
var err error | ||
switch netln := ln.ln.(type) { | ||
case nil: | ||
switch pconn := ln.pconn.(type) { | ||
case *net.UDPConn: | ||
ln.f, err = pconn.File() | ||
} | ||
case *net.TCPListener: | ||
ln.f, err = netln.File() | ||
case *net.UnixListener: | ||
ln.f, err = netln.File() | ||
} | ||
if err != nil { | ||
ln.close() | ||
return err | ||
} | ||
ln.fd = int(ln.f.Fd()) | ||
return unix.SetNonblock(ln.fd, true) | ||
} |
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
Oops, something went wrong.