mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -05:00
add connection adaptor func
This commit is contained in:
parent
690b4924ae
commit
7ee06c8239
@ -2,6 +2,7 @@ package packetaddr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
gonet "net"
|
gonet "net"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -105,3 +106,36 @@ func (c packetConnectionAdaptor) SetReadDeadline(t time.Time) error {
|
|||||||
func (c packetConnectionAdaptor) SetWriteDeadline(t time.Time) error {
|
func (c packetConnectionAdaptor) SetWriteDeadline(t time.Time) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToPacketAddrReadWriteCloser(conn net.PacketConn, isStream bool) io.ReadWriteCloser {
|
||||||
|
return &packetConnWrapper{conn}
|
||||||
|
}
|
||||||
|
|
||||||
|
type packetConnWrapper struct {
|
||||||
|
conn net.PacketConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *packetConnWrapper) Read(p []byte) (n int, err error) {
|
||||||
|
recbuf := buf.StackNew()
|
||||||
|
recbuf.Extend(2048)
|
||||||
|
n, addr, err := pc.conn.ReadFrom(recbuf.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
result := AttachAddressToPacket(recbuf.Bytes()[0:n], addr)
|
||||||
|
n = copy(p, result)
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *packetConnWrapper) Write(p []byte) (n int, err error) {
|
||||||
|
data, addr := ExtractAddressFromPacket(p)
|
||||||
|
_, err = pc.conn.WriteTo(data, addr)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *packetConnWrapper) Close() error {
|
||||||
|
return pc.Close()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user