Skip to content

Commit

Permalink
support kcp protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
felipejfc committed Jan 31, 2019
1 parent ca12a79 commit 4d86d89
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 2 deletions.
65 changes: 63 additions & 2 deletions Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@
[[override]]
name = "github.com/topfreegames/go-workers"
version = "v0.0.1"

[[constraint]]
name = "github.com/xtaci/kcp-go"
version = "5.0.7"
88 changes: 88 additions & 0 deletions acceptor/kcp_acceptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) nano Author and TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package acceptor

import (
"net"

"github.com/topfreegames/pitaya/logger"
kcp "github.com/xtaci/kcp-go"
)

// KCPAcceptor struct
type KCPAcceptor struct {
addr string
connChan chan net.Conn
listener net.Listener
running bool
}

// NewKCPAcceptor creates a new instance of kcp acceptor
func NewKCPAcceptor(addr string) *KCPAcceptor {
return &KCPAcceptor{
addr: addr,
connChan: make(chan net.Conn),
running: false,
}
}

// GetAddr returns the addr the acceptor will listen on
func (a *KCPAcceptor) GetAddr() string {
if a.listener != nil {
return a.listener.Addr().String()
}
return ""
}

// GetConnChan gets a connection channel
func (a *KCPAcceptor) GetConnChan() chan net.Conn {
return a.connChan
}

// Stop stops the acceptor
func (a *KCPAcceptor) Stop() {
a.running = false
a.listener.Close()
}

// ListenAndServe using kcp acceptor
func (a *KCPAcceptor) ListenAndServe() {
listener, err := kcp.Listen(a.addr)
if err != nil {
logger.Log.Fatal(err)
}
a.listener = listener
a.running = true
a.serve()
}

func (a *KCPAcceptor) serve() {
defer a.Stop()
for a.running {
conn, err := a.listener.Accept()
if err != nil {
logger.Log.Error(err.Error())
continue
}

a.connChan <- conn
}
}
20 changes: 20 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/topfreegames/pitaya/conn/packet"
"github.com/topfreegames/pitaya/logger"
"github.com/topfreegames/pitaya/util/compression"
kcp "github.com/xtaci/kcp-go"
)

var (
Expand Down Expand Up @@ -342,6 +343,25 @@ func (c *Client) ConnectToTLS(addr string, skipVerify bool) error {
return nil
}

// ConnectKCP connects to the server at addr using kcp protocol
// this methods blocks as it also handles the messages from the server
func (c *Client) ConnectKCP(addr string) error {
conn, err := kcp.Dial(addr)
if err != nil {
return err
}
c.conn = conn
c.IncomingMsgChan = make(chan *message.Message, 10)

if err = c.handleHandshake(); err != nil {
return err
}

c.closeChan = make(chan struct{})

return nil
}

// ConnectTo connects to the server at addr, for now the only supported protocol is tcp
// this methods blocks as it also handles the messages from the server
func (c *Client) ConnectTo(addr string) error {
Expand Down
2 changes: 2 additions & 0 deletions client/pitayaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import (
"github.com/topfreegames/pitaya/conn/message"
)

// PitayaClient interface
type PitayaClient interface {
ConnectTo(addr string) error
ConnectKCP(addr string) error
ConnectToTLS(addr string, skipVerify bool) error
Disconnect()
SendNotify(route string, data []byte) error
Expand Down
2 changes: 2 additions & 0 deletions examples/demo/cluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func configureBackend() {

func configureFrontend(port int) {
tcp := acceptor.NewTCPAcceptor(fmt.Sprintf(":%d", port))
kcp := acceptor.NewKCPAcceptor(fmt.Sprintf(":%d", port+1))

pitaya.Register(&services.Connector{},
component.WithName("connector"),
Expand Down Expand Up @@ -71,6 +72,7 @@ func configureFrontend(port int) {
}

pitaya.AddAcceptor(tcp)
pitaya.AddAcceptor(kcp)
}

func main() {
Expand Down

0 comments on commit 4d86d89

Please sign in to comment.