-
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.
Merge pull request #26 from Trinoooo/test/transport
feat(server): 完成reactor server开发,但测试过程中遇到个问题
- Loading branch information
Showing
21 changed files
with
1,260 additions
and
238 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 |
---|---|---|
|
@@ -2,11 +2,12 @@ name: Go Test | |
|
||
on: | ||
pull_request: | ||
push: | ||
|
||
jobs: | ||
test: | ||
name: Run Tests | ||
runs-on: ubuntu-latest | ||
runs-on: macos-latest | ||
|
||
steps: | ||
- name: Set up Go | ||
|
@@ -18,7 +19,7 @@ jobs: | |
uses: actions/checkout@v2 | ||
|
||
- name: Run tests | ||
run: make -f makefile test-with-cover TestPackage=./storage/core/ragdoll/wal | ||
run: make -f makefile test-with-cover TestPackage=./storage/server | ||
|
||
- name: Upload coverage reports to Codecov | ||
uses: codecov/[email protected] | ||
|
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
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,18 @@ | ||
package connections | ||
|
||
import ( | ||
"io" | ||
"net" | ||
) | ||
|
||
type IListener interface { | ||
Accept() (IConnection, error) | ||
io.Closer | ||
} | ||
|
||
type IConnection interface { | ||
io.ReadWriteCloser | ||
RemoteAddr() net.Addr | ||
LocalAddr() net.Addr | ||
RawFd() int | ||
} |
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,97 @@ | ||
package connections | ||
|
||
import ( | ||
"net" | ||
"syscall" | ||
) | ||
|
||
const maxSoMaxConn = 500 | ||
|
||
type Connection struct { | ||
fd int | ||
localAddr *syscall.SockaddrInet4 | ||
remoteAddr *syscall.SockaddrInet4 | ||
} | ||
|
||
func (c *Connection) Read(buf []byte) (int, error) { | ||
return syscall.Read(c.fd, buf) | ||
} | ||
|
||
func (c *Connection) Write(buf []byte) (int, error) { | ||
return syscall.Write(c.fd, buf) | ||
} | ||
|
||
func (c *Connection) Close() error { | ||
return syscall.Close(c.fd) | ||
} | ||
|
||
func (c *Connection) RemoteAddr() net.Addr { | ||
ipv4 := net.IPv4(c.remoteAddr.Addr[0], c.remoteAddr.Addr[1], c.remoteAddr.Addr[2], c.remoteAddr.Addr[3]) | ||
return &net.TCPAddr{ | ||
IP: ipv4, | ||
Port: c.remoteAddr.Port, | ||
} | ||
} | ||
|
||
func (c *Connection) LocalAddr() net.Addr { | ||
ipv4 := net.IPv4(c.localAddr.Addr[0], c.localAddr.Addr[1], c.localAddr.Addr[2], c.localAddr.Addr[3]) | ||
return &net.TCPAddr{ | ||
IP: ipv4, | ||
Port: c.localAddr.Port, | ||
} | ||
} | ||
|
||
func (c *Connection) RawFd() int { | ||
return c.fd | ||
} | ||
|
||
type Listener struct { | ||
conn *Connection | ||
} | ||
|
||
func (l *Listener) Accept() (IConnection, error) { | ||
socket, sa, err := syscall.Accept(l.conn.fd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = syscall.SetNonblock(socket, true); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Connection{ | ||
fd: socket, | ||
localAddr: l.conn.localAddr, | ||
remoteAddr: sa.(*syscall.SockaddrInet4), | ||
}, nil | ||
} | ||
|
||
func (l *Listener) Close() error { | ||
return syscall.Close(l.conn.fd) | ||
} | ||
|
||
func Listen(addr [4]byte, port int) (IListener, error) { | ||
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
laddr := &syscall.SockaddrInet4{ | ||
Port: port, | ||
Addr: addr, | ||
} | ||
if err = syscall.Bind(fd, laddr); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = syscall.Listen(fd, maxSoMaxConn); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Listener{ | ||
conn: &Connection{ | ||
fd: fd, | ||
localAddr: laddr, | ||
}, | ||
}, nil | ||
} |
Oops, something went wrong.