afonso
cdaae8fb7e
Still need to create crypto object and use it to encrypt messages Need to create TLS still
35 lines
669 B
Go
35 lines
669 B
Go
package networking
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net"
|
|
)
|
|
|
|
type Connection[T any] struct {
|
|
Conn net.Conn
|
|
encoder *json.Encoder
|
|
decoder *json.Decoder
|
|
}
|
|
|
|
|
|
func NewConnection[T any](netConn net.Conn) Connection[T] {
|
|
return Connection[T]{
|
|
Conn: netConn,
|
|
encoder: json.NewEncoder(netConn),
|
|
decoder: json.NewDecoder(netConn),
|
|
}
|
|
}
|
|
|
|
func (jc Connection[T]) Send(obj T) {
|
|
if err := jc.encoder.Encode(&obj); err != nil {
|
|
panic("Failed encoding data or sending it to connection")
|
|
}
|
|
}
|
|
|
|
func (jc Connection[T]) Receive() T {
|
|
var obj T
|
|
if err := jc.decoder.Decode(&obj); err != nil {
|
|
panic("Failed decoding data or reading it from connection")
|
|
}
|
|
return obj
|
|
}
|