Skip to content

Commit

Permalink
Merge pull request #3 from Vinz1911/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Vinz1911 authored Jul 4, 2021
2 parents 6715da3 + a226ed6 commit 298b971
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 133 deletions.
13 changes: 13 additions & 0 deletions .network.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 50 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,62 @@
<div align="center">
<h1>
<a href="https://github.com/Vinz1911/network-go"><img src="https://github.com/Vinz1911/network-go/blob/main/.network.svg" alt="Network-GO" width="300"></a>
<br>
Network-GO
<br>
Network-GO
</h1>
</div>

`Network-GO` is a `Go` implementation of the `NWK-Protocol`. The `NWK-Protocol` is a proprietary networking protocol based on top of `TCP`.
This protocol has a very small overhead which makes it as fast as nearly raw tcp connection throughput.
`Network-GO` is a library which implements the `NWC-Protocol`. The `NWC-Protocol` is proprietary networking protocol which uses a small and lightweight header with a performance as fast as raw tcp performance.
Built directly on top of Go's `net.Listener` with support for plain tcp and tls encrypted connections.
The implementation for the Client is [NetworKit](https://github.com/Vinz1911/networkit) written in swift on top of `Network.framework` to ensure maximum performance.

## License:
[![License](https://img.shields.io/badge/license-GPLv3-blue.svg?longCache=true&style=flat)](https://github.com/Vinz1911/network-go/blob/main/LICENSE)

## Golang Version:
[![Golang 1.16](https://img.shields.io/badge/Golang-1.16-00ADD8.svg?logo=go&style=flat)](https://golang.org) [![Golang 1.16](https://img.shields.io/badge/Modules-Support-00ADD8.svg?logo=go&style=flat)](https://golang.org)
[![Golang 1.16](https://img.shields.io/badge/Golang-1.16-00ADD8.svg?logo=go&style=flat)](https://golang.org) [![Golang 1.16](https://img.shields.io/badge/Modules-Support-00ADD8.svg?logo=go&style=flat)](https://blog.golang.org/using-go-modules)

## Installation:
### Go Modules
[Go Modules](https://blog.golang.org/using-go-modules). Just add this repo to your go.sum file.

## Import:
```go
// import the framework
import "github.com/Vinz1911/network-go"

// create a new connection listener
listener := network.Listener{}

// ...
```

## Callback:
```go
// create a new connection listener
listener := network.Listener{}

// listener is ready
listener.Ready = func(socket net.Conn) { }

// listener received message from a connection
listener.Message = func(conn net.Conn, text *string, binary []byte) {
// message is text based
if text != nil { }
// message is binary based
if binary != nil { }
}

// listener connection failed
listener.Failed = func(conn net.Conn, err error) { }

// listener connection cancelled
listener.Cancelled = func(conn net.Conn) { }

// start listener
err := listener.Start(network.TCPConnection, uint16(8080))
```

## Author:
👨🏼‍💻 [Vinzenz Weist](https://github.com/Vinz1911)
129 changes: 0 additions & 129 deletions connection.go

This file was deleted.

128 changes: 128 additions & 0 deletions listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Package network

// Copyright 2021 Vinzenz Weist. All rights reserved.
// Use of this source code is risked by yourself.
// license that can be found in the LICENSE file.

package network

import (
"crypto/tls"
"errors"
"io"
"net"
"strconv"
)

const (
TCPConnection uint8 = 0x0
TLSConnection uint8 = 0x1

TextMessage uint8 = 0x1
BinaryMessage uint8 = 0x2
PingMessage uint8 = 0x3
)

// Listener is a a tcp based connection listener
// this is for handling incoming pure tcp connections
type Listener struct {
frame frame
listener net.Listener

Cert string
Key string

Ready func(conn net.Conn)
Message func(conn net.Conn, text *string, binary []byte)
Failed func(conn net.Conn, err error)
Cancelled func(conn net.Conn)
}

// Start the NetworkGO connection listener
// waits for incoming connections
func (listener *Listener) Start(parameter uint8, port uint16) error {
switch parameter {
case TCPConnection:
var err error
listener.listener, err = net.Listen("tcp", ":" + strconv.Itoa(int(port)))
if err != nil { return err }
case TLSConnection:
cer, err := tls.LoadX509KeyPair(listener.Cert, listener.Key)
if err != nil { return err }
config := &tls.Config{Certificates: []tls.Certificate{cer}}
listener.listener, err = tls.Listen("tcp", ":" + strconv.Itoa(int(port)), config)
if err != nil { return err }
}
defer listener.listener.Close()
for {
conn, err := listener.listener.Accept()
if err != nil { return err }
go listener.receiveMessage(conn)
}
}

// Cancel closes all connections and stops
// the listener from accepting new connections
func (listener *Listener) Cancel() {
if listener.listener == nil { return }
err := listener.listener.Close()
if err != nil { listener.Failed(nil, err) }
listener.listener = nil
}

// SendTextMessage is for sending a text based message
func (listener *Listener) SendTextMessage(conn net.Conn, str string) {
listener.processingSend(conn, []byte(str), TextMessage)
}

// SendBinaryMessage is for sending a text based message
func (listener *Listener) SendBinaryMessage(conn net.Conn, data []byte) {
listener.processingSend(conn, data, BinaryMessage)
}

/// MARK: - Private API

// create and send message frame
func (listener *Listener) processingSend(conn net.Conn, data []byte, opcode uint8) {
if listener.listener == nil { return }
message, err := listener.frame.create(data, opcode)
if err != nil { listener.Failed(conn, err); listener.remove(conn) }
_, err = conn.Write(message)
if err != nil { listener.Failed(conn, err) }
}

// parse a message frame
func (listener *Listener) processingParse(conn net.Conn, frame *frame, data []byte) error {
if listener.listener == nil { return errors.New(parsingFailed) }
err := frame.parse(data, func(text *string, data []byte, ping []byte) {
listener.Message(conn, text, data)
if ping != nil { listener.sendPong(conn, ping) }
})
return err
}

// remove is for terminating a specific connection
func (listener *Listener) remove(conn net.Conn) {
err := conn.Close()
if err != nil { listener.Failed(conn, err) }
listener.Cancelled(conn)
}

// sendPong is for sending a pong based message
func (listener *Listener) sendPong(conn net.Conn, data []byte) {
listener.processingSend(conn, data, PingMessage)
}

// receiveMessage is handling all incoming input
// keeps track broken connections
func (listener *Listener) receiveMessage(conn net.Conn) {
frame := frame{}
listener.Ready(conn)
buffer := make([]byte, 0x2000)
for {
size, err := conn.Read(buffer)
if err != nil { if err == io.EOF { listener.Cancelled(conn) } else { listener.Failed(conn, err) }; break }
err = listener.processingParse(conn, &frame, buffer[:size])
if err != nil { listener.Failed(conn, err); listener.remove(conn); break }
}
}

0 comments on commit 298b971

Please sign in to comment.