Skip to content

Commit

Permalink
Merge pull request #69 from wolfendale/performance-tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbarrow authored Jul 5, 2024
2 parents 293c20d + 22d2698 commit d633dca
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
9 changes: 6 additions & 3 deletions kerberos.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,14 @@ func NewKerberosTicketInternalData(server *PRUDPServer) *KerberosTicketInternalD

// DeriveKerberosKey derives a users kerberos encryption key based on their PID and password
func DeriveKerberosKey(pid *types.PID, password []byte) []byte {
key := password
iterationCount := 65000 + pid.Value()%1024
key := make([]byte, md5.Size)
copy(key, password)

for i := 0; i < 65000+int(pid.Value())%1024; i++ {
var i uint64 = 0
for ; i < iterationCount; i++ {
hash := md5.Sum(key)
key = hash[:]
copy(key, hash[:])
}

return key
Expand Down
9 changes: 3 additions & 6 deletions prudp_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ func (pep *PRUDPEndPoint) on(name string, handler func(packet PacketInterface))
func (pep *PRUDPEndPoint) emit(name string, packet PRUDPPacketInterface) {
if handlers, ok := pep.packetEventHandlers[name]; ok {
for _, handler := range handlers {
go handler(packet)
handler(packet)
}
}
}

func (pep *PRUDPEndPoint) emitConnectionEnded(connection *PRUDPConnection) {
for _, handler := range pep.connectionEndedEventHandlers {
go handler(connection)
handler(connection)
}
}

// EmitError calls all the endpoints error event handlers with the provided error
func (pep *PRUDPEndPoint) EmitError(err *Error) {
for _, handler := range pep.errorEventHandlers {
go handler(err)
handler(err)
}
}

Expand Down Expand Up @@ -440,9 +440,6 @@ func (pep *PRUDPEndPoint) handlePing(packet PRUDPPacketInterface) {
}

if packet.HasFlag(constants.PacketFlagReliable) {
connection.Lock()
defer connection.Unlock()

substreamID := packet.SubstreamID()
packetDispatchQueue := connection.PacketDispatchQueue(substreamID)
packetDispatchQueue.Queue(packet)
Expand Down
2 changes: 1 addition & 1 deletion prudp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func (ps *PRUDPServer) ListenUDP(port int) {

func (ps *PRUDPServer) listenDatagram(quit chan struct{}) {
var err error
buffer := make([]byte, 64000)

for err == nil {
buffer := make([]byte, 64000)
var read int
var addr *net.UDPAddr

Expand Down
14 changes: 6 additions & 8 deletions timeout_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,20 @@ func (tm *TimeoutManager) SchedulePacketTimeout(packet PRUDPPacketInterface) {

// AcknowledgePacket marks a pending packet as acknowledged. It will be ignored at the next resend attempt
func (tm *TimeoutManager) AcknowledgePacket(sequenceID uint16) {
if packet, ok := tm.packets.Get(sequenceID); ok {
// * Acknowledge the packet
tm.packets.Delete(sequenceID)

// * Acknowledge the packet
tm.packets.RunAndDelete(sequenceID, func(_ uint16, packet PRUDPPacketInterface) {
// * Update the RTT on the connection if the packet hasn't been resent
if packet.SendCount() <= tm.streamSettings.RTTRetransmit {
if packet.SendCount() >= tm.streamSettings.RTTRetransmit {
rttm := time.Since(packet.SentAt())
packet.Sender().(*PRUDPConnection).rtt.Adjust(rttm)
}
}
})
}

func (tm *TimeoutManager) start(packet PRUDPPacketInterface) {
<-packet.getTimeout().ctx.Done()

connection := packet.Sender().(*PRUDPConnection)
connection.Lock()
defer connection.Unlock()

// * If the connection is closed stop trying to resend
if connection.ConnectionState != StateConnected {
Expand Down Expand Up @@ -80,6 +76,8 @@ func (tm *TimeoutManager) start(packet PRUDPPacketInterface) {
server.sendRaw(connection.Socket, data)
} else {
// * Packet has been retried too many times, consider the connection dead
connection.Lock()
defer connection.Unlock()
connection.cleanup()
}
}
Expand Down

0 comments on commit d633dca

Please sign in to comment.