mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-10 06:20:26 +00:00
WARNING: CRYPTO DISABLED while speeding up stream writeMsg
This commit is contained in:
parent
1e6a6d2160
commit
f52955ee0f
@ -172,6 +172,7 @@ func BoxOpen(shared *BoxSharedKey,
|
||||
boxed []byte,
|
||||
nonce *BoxNonce) ([]byte, bool) {
|
||||
out := util.GetBytes()
|
||||
return append(out, boxed...), true
|
||||
s := (*[BoxSharedKeyLen]byte)(shared)
|
||||
n := (*[BoxNonceLen]byte)(nonce)
|
||||
unboxed, success := box.OpenAfterPrecomputation(out, boxed, n, s)
|
||||
@ -184,6 +185,7 @@ func BoxSeal(shared *BoxSharedKey, unboxed []byte, nonce *BoxNonce) ([]byte, *Bo
|
||||
}
|
||||
nonce.Increment()
|
||||
out := util.GetBytes()
|
||||
return append(out, unboxed...), nonce
|
||||
s := (*[BoxSharedKeyLen]byte)(shared)
|
||||
n := (*[BoxNonceLen]byte)(nonce)
|
||||
boxed := box.SealAfterPrecomputation(out, unboxed, n, s)
|
||||
|
@ -34,6 +34,15 @@ func PutBytes(bs []byte) {
|
||||
byteStore.Put(bs)
|
||||
}
|
||||
|
||||
// Gets a slice of the appropriate length, reusing existing slice capacity when possible
|
||||
func ResizeBytes(bs []byte, length int) []byte {
|
||||
if cap(bs) >= length {
|
||||
return bs[:length]
|
||||
} else {
|
||||
return make([]byte, length)
|
||||
}
|
||||
}
|
||||
|
||||
// This is a workaround to go's broken timer implementation
|
||||
func TimerStop(t *time.Timer) bool {
|
||||
stopped := t.Stop()
|
||||
|
@ -127,7 +127,6 @@ func (r *router) mainLoop() {
|
||||
r.core.switchTable.doMaintenance()
|
||||
r.core.dht.doMaintenance()
|
||||
r.core.sessions.cleanup()
|
||||
util.GetBytes() // To slowly drain things
|
||||
}
|
||||
case f := <-r.admin:
|
||||
f()
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
||||
)
|
||||
@ -12,10 +13,11 @@ import (
|
||||
var _ = linkInterfaceMsgIO(&stream{})
|
||||
|
||||
type stream struct {
|
||||
rwc io.ReadWriteCloser
|
||||
inputBuffer []byte // Incoming packet stream
|
||||
frag [2 * streamMsgSize]byte // Temporary data read off the underlying rwc, on its way to the inputBuffer
|
||||
outputBuffer [2 * streamMsgSize]byte // Temporary data about to be written to the rwc
|
||||
rwc io.ReadWriteCloser
|
||||
inputBuffer []byte // Incoming packet stream
|
||||
frag [2 * streamMsgSize]byte // Temporary data read off the underlying rwc, on its way to the inputBuffer
|
||||
//outputBuffer [2 * streamMsgSize]byte // Temporary data about to be written to the rwc
|
||||
outputBuffer net.Buffers
|
||||
}
|
||||
|
||||
func (s *stream) close() error {
|
||||
@ -35,14 +37,17 @@ func (s *stream) init(rwc io.ReadWriteCloser) {
|
||||
// writeMsg writes a message with stream padding, and is *not* thread safe.
|
||||
func (s *stream) writeMsg(bs []byte) (int, error) {
|
||||
buf := s.outputBuffer[:0]
|
||||
buf = append(buf, streamMsg[:]...)
|
||||
buf = wire_put_uint64(uint64(len(bs)), buf)
|
||||
padLen := len(buf)
|
||||
buf = append(buf, bs...)
|
||||
buf = append(buf, streamMsg[:])
|
||||
l := wire_put_uint64(uint64(len(bs)), util.GetBytes())
|
||||
defer util.PutBytes(l)
|
||||
buf = append(buf, l)
|
||||
padLen := len(buf[0]) + len(buf[1])
|
||||
buf = append(buf, bs)
|
||||
totalLen := padLen + len(bs)
|
||||
var bn int
|
||||
for bn < len(buf) {
|
||||
n, err := s.rwc.Write(buf[bn:])
|
||||
bn += n
|
||||
for bn < totalLen {
|
||||
n, err := buf.WriteTo(s.rwc)
|
||||
bn += int(n)
|
||||
if err != nil {
|
||||
l := bn - padLen
|
||||
if l < 0 {
|
||||
|
@ -820,7 +820,7 @@ func (t *switchTable) doWorker() {
|
||||
select {
|
||||
case bs := <-t.toRouter:
|
||||
buf = append(buf, bs)
|
||||
for len(buf) > 32 {
|
||||
for len(buf) > 32768 { // FIXME realistically don't drop anything, just for testing
|
||||
util.PutBytes(buf[0])
|
||||
buf = buf[1:]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user