Skip to content

Commit

Permalink
Add RUDP transport option for client connections. Merge heroiclabs#117
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro committed Oct 31, 2017
1 parent c69a539 commit af8bf7a
Show file tree
Hide file tree
Showing 42 changed files with 3,618 additions and 708 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com/) and this project uses [semantic versioning](http://semver.org/).

## [Unreleased]
### Added
- RUDP transport option for client connections.

### [1.1.0] - 2017-10-17
### Added
Expand Down
14 changes: 10 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

[[constraint]]
name = "github.com/gorilla/websocket"
version = "~1.0.0"
version = "~1.2.0"

[[constraint]]
name = "github.com/lib/pq"
Expand Down Expand Up @@ -65,3 +65,6 @@

[[constraint]]
name = "golang.org/x/net"

[[constraint]]
name = "github.com/wirepair/netcode"
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
_ "github.com/lib/pq"
"github.com/satori/go.uuid"
"go.uber.org/zap"
"runtime"
)

const (
Expand Down Expand Up @@ -74,7 +75,7 @@ func main() {

// Print startup information
multiLogger.Info("Nakama starting")
multiLogger.Info("Node", zap.String("name", config.GetName()), zap.String("version", semver))
multiLogger.Info("Node", zap.String("name", config.GetName()), zap.String("version", semver), zap.String("runtime", runtime.Version()))
multiLogger.Info("Data directory", zap.String("path", config.GetDataDir()))
multiLogger.Info("Database connections", zap.Strings("dsns", config.GetDatabase().Addresses))

Expand Down
100 changes: 100 additions & 0 deletions pkg/multicode/byte_array_rw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2017 The Nakama Authors
//
// 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 multicode

import "io"

type ByteArrayReaderWriter struct {
data []byte
readPosition int
writePosition int
}

// TODO Cache and reuse?
func NewByteArrayReaderWriter(data []byte) *ByteArrayReaderWriter {
return &ByteArrayReaderWriter{
data: data,
readPosition: 0,
writePosition: 0,
}
}

// Reset the read position in the stream to the given offset relative to the beginning of the stream.
func (s *ByteArrayReaderWriter) SeekRead(offset int) int {
s.readPosition = offset
return s.readPosition
}

// Reset the read position in the stream to the given offset relative to the beginning of the stream.
func (s *ByteArrayReaderWriter) SeekWrite(offset int) int {
s.writePosition = offset
return s.writePosition
}

func (s *ByteArrayReaderWriter) ReadByte() (byte, error) {
buf := make([]byte, 1)
if s.read(buf, 0, 1) != 1 {
return 0, io.EOF
}
return buf[0], nil
}

func (s *ByteArrayReaderWriter) ReadUint16() (uint16, error) {
buf := make([]byte, 2)
if s.read(buf, 0, 2) != 2 {
return 0, io.EOF
}
return uint16(uint32(buf[0]) | uint32(buf[1]<<8)), nil
}

func (s *ByteArrayReaderWriter) ReadBuffer(buf []byte, length int) error {
if s.read(buf, 0, length) != length {
return io.EOF
}
return nil
}

func (s *ByteArrayReaderWriter) WriteByte(b byte) {
s.write([]byte{b}, 0, 1)
}

func (s *ByteArrayReaderWriter) WriteUint16(u uint16) {
s.write([]byte{byte(u), byte(uint32(u) >> 8)}, 0, 2)
}

func (s *ByteArrayReaderWriter) WriteBuffer(buf []byte, length int) {
s.write(buf, 0, length)
//for i := 0; i < length; i++ {
// s.WriteByte(buf[i])
//}
}

func (s *ByteArrayReaderWriter) read(buffer []byte, offset, count int) int {
readBytes := 0
length := len(s.data)
for i := 0; i < count && s.readPosition < length; i++ {
buffer[i+offset] = s.data[s.readPosition]
s.readPosition++
readBytes++
}
return readBytes
}

func (s *ByteArrayReaderWriter) write(buffer []byte, offset, count int) {
for i := 0; i < count; i++ {
s.data[s.writePosition] = buffer[i+offset]
s.writePosition++
}
}
Loading

0 comments on commit af8bf7a

Please sign in to comment.