-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(knetty): add listener ,dialer for tcp protocol
- Loading branch information
1 parent
444212a
commit 55c249e
Showing
19 changed files
with
294 additions
and
197 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
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,96 @@ | ||
/* | ||
Copyright 2022 Phoenix | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package listener | ||
|
||
import ( | ||
"net" | ||
"time" | ||
|
||
"github.com/Softwarekang/knetty/internal/net/connection" | ||
errors "github.com/Softwarekang/knetty/pkg/err" | ||
netutil "github.com/Softwarekang/knetty/pkg/net" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// Listener A Listener is a generic network listener for stream-oriented protocols. | ||
type Listener interface { | ||
// Accept waits for and returns the next connection to the listener. | ||
Accept() (connection.Connection, error) | ||
|
||
// Close closes the listener. | ||
// Any blocked Accept operations will be unblocked and return errors. | ||
Close() error | ||
|
||
// Addr returns the listener's network address. | ||
Addr() net.Addr | ||
|
||
// FD returns the listener's fd | ||
FD() int | ||
} | ||
|
||
// TcpListener tcp network listener. | ||
type TcpListener struct { | ||
Fd int | ||
TcpAddr *net.TCPAddr | ||
keepAlive time.Duration | ||
} | ||
|
||
// Accept implements Listener. | ||
func (t *TcpListener) Accept() (connection.Connection, error) { | ||
if !t.ok() { | ||
return nil, errors.IllegalListenerErr("tcp") | ||
} | ||
|
||
cfd, sa, err := unix.Accept(t.Fd) | ||
if err != nil { | ||
if err == unix.EAGAIN { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
rsa := netutil.SocketAddrToAddr(sa) | ||
return connection.NewTcpConn(cfd, t.TcpAddr, rsa), unix.SetNonblock(cfd, true) | ||
} | ||
|
||
// Close implements Listener. | ||
func (t *TcpListener) Close() error { | ||
if t.Fd != 0 { | ||
return unix.Close(t.Fd) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Addr implements Listener. | ||
func (t *TcpListener) Addr() net.Addr { | ||
return t.TcpAddr | ||
} | ||
|
||
// FD implements Listener. | ||
func (t *TcpListener) FD() int { | ||
return t.Fd | ||
} | ||
|
||
func (t *TcpListener) ok() bool { | ||
if t.Fd != 0 && t.TcpAddr != nil { | ||
return true | ||
} | ||
|
||
return false | ||
} |
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,103 @@ | ||
/* | ||
Copyright 2022 Phoenix | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package net | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/Softwarekang/knetty/internal/net/connection" | ||
"github.com/Softwarekang/knetty/internal/net/listener" | ||
errors "github.com/Softwarekang/knetty/pkg/err" | ||
netutil "github.com/Softwarekang/knetty/pkg/net" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func Listen(network, address string) (listener.Listener, error) { | ||
switch network { | ||
case "tcp": | ||
return listenTcp(network, address) | ||
default: | ||
return nil, errors.UnKnowNetworkErr(network) | ||
} | ||
|
||
} | ||
|
||
func listenTcp(network, address string) (*listener.TcpListener, error) { | ||
tcpAddr, err := net.ResolveTCPAddr(network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := unix.Bind(fd, &unix.SockaddrInet4{ | ||
Port: tcpAddr.Port, | ||
Addr: tcpAddr.AddrPort().Addr().As4(), | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := unix.Listen(fd, unix.SOMAXCONN); err != nil { | ||
return nil, err | ||
} | ||
return &listener.TcpListener{ | ||
Fd: fd, | ||
TcpAddr: tcpAddr, | ||
}, unix.SetNonblock(fd, true) | ||
} | ||
|
||
func Dial(network, address string) (connection.Connection, error) { | ||
switch network { | ||
case "tcp": | ||
return dialTcp(network, address) | ||
default: | ||
return nil, errors.UnKnowNetworkErr(network) | ||
} | ||
|
||
} | ||
|
||
func dialTcp(network string, address string) (*connection.TcpConn, error) { | ||
tcpAddr, err := net.ResolveTCPAddr(network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rsa, err := netutil.ResolveNetAddrToSocketAddr(tcpAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fmt.Println(rsa) | ||
if err = unix.Connect(fd, rsa); err != nil { | ||
return nil, err | ||
} | ||
|
||
lsa, err := unix.Getsockname(fd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return connection.NewTcpConn(fd, netutil.SocketAddrToAddr(lsa), netutil.SocketAddrToAddr(rsa)), unix.SetNonblock(fd, true) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.