-
Notifications
You must be signed in to change notification settings - Fork 6
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
19 changed files
with
393 additions
and
26 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
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,41 @@ | ||
package tcp | ||
|
||
import ( | ||
"net" | ||
"net/netip" | ||
"time" | ||
|
||
"github.com/fumiama/WireGold/gold/p2p" | ||
) | ||
|
||
type Config struct { | ||
PeersTimeout time.Duration | ||
ReceiveChannelSize int | ||
} | ||
|
||
func NewEndpoint(endpoint string, configs ...any) p2p.EndPoint { | ||
return newEndpoint(endpoint, configs...) | ||
} | ||
|
||
func newEndpoint(endpoint string, configs ...any) *EndPoint { | ||
var cfg *Config | ||
if len(configs) == 0 || configs[0] == nil { | ||
cfg = &Config{} | ||
} else { | ||
cfg = configs[0].(*Config) | ||
} | ||
return &EndPoint{ | ||
addr: net.TCPAddrFromAddrPort( | ||
netip.MustParseAddrPort(endpoint), | ||
), | ||
peerstimeout: cfg.PeersTimeout, | ||
recvchansize: cfg.ReceiveChannelSize, | ||
} | ||
} | ||
|
||
func init() { | ||
_, hasexist := p2p.Register("tcp", NewEndpoint) | ||
if hasexist { | ||
panic("network tcp has been registered") | ||
} | ||
} |
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,65 @@ | ||
package tcp | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
"net" | ||
|
||
"github.com/fumiama/WireGold/helper" | ||
) | ||
|
||
type packetType uint8 | ||
|
||
const ( | ||
packetTypeKeepAlive packetType = iota | ||
packetTypeNormal | ||
) | ||
|
||
type packet struct { | ||
typ packetType | ||
len uint16 | ||
dat []byte | ||
io.ReaderFrom | ||
io.WriterTo | ||
} | ||
|
||
func (p *packet) pack() (net.Buffers, func()) { | ||
d, cl := helper.OpenWriterF(func(w *helper.Writer) { | ||
w.WriteByte(byte(p.typ)) | ||
w.WriteUInt16(p.len) | ||
}) | ||
return net.Buffers{d, p.dat}, cl | ||
} | ||
|
||
func (p *packet) Read(_ []byte) (int, error) { | ||
panic("stub") | ||
} | ||
|
||
func (p *packet) Write(_ []byte) (int, error) { | ||
panic("stub") | ||
} | ||
|
||
func (p *packet) ReadFrom(r io.Reader) (n int64, err error) { | ||
var buf [3]byte | ||
cnt, err := io.ReadFull(r, buf[:]) | ||
n = int64(cnt) | ||
if err != nil { | ||
return | ||
} | ||
p.typ = packetType(buf[0]) | ||
p.len = binary.LittleEndian.Uint16(buf[1:3]) | ||
w := helper.SelectWriter() | ||
copied, err := io.CopyN(w, r, int64(p.len)) | ||
n += copied | ||
if err != nil { | ||
return | ||
} | ||
p.dat = w.Bytes() | ||
return | ||
} | ||
|
||
func (p *packet) WriteTo(w io.Writer) (n int64, err error) { | ||
buf, cl := p.pack() | ||
defer cl() | ||
return io.Copy(w, &buf) | ||
} |
Oops, something went wrong.