[PD1] Fixed almost everything

This commit is contained in:
Afonso Franco 2024-04-19 23:59:26 +01:00
parent 39a0e5c01f
commit 7b3172a850
Signed by: afonso
SSH key fingerprint: SHA256:aiLbdlPwXKJS5wMnghdtod0SPy8imZjlVvCyUX9DJNk
13 changed files with 534 additions and 192 deletions

View file

@ -4,6 +4,8 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"io"
"log"
)
type Connection[T any] struct {
@ -20,16 +22,33 @@ func NewConnection[T any](netConn *tls.Conn) Connection[T] {
}
}
func (c Connection[T]) Send(obj T) {
if err := c.encoder.Encode(&obj); err != nil {
panic("Failed encoding data or sending it to connection")
}
func (c Connection[T]) Send(obj T) bool {
if err := c.encoder.Encode(&obj); err!=nil {
if err == io.EOF {
log.Println("Connection closed by peer")
//Return false as connection not active
return false
} else {
log.Panic(err)
}
}
//Return true as connection active
return true
}
func (c Connection[T]) Receive(objPtr *T) {
if err := c.decoder.Decode(objPtr); err != nil {
panic("Failed decoding data or reading it from connection")
func (c Connection[T]) Receive() (*T, bool) {
var obj T
if err := c.decoder.Decode(&obj); err != nil {
if err == io.EOF {
log.Println("Connection closed by peer")
//Return false as connection not active
return nil,false
} else {
log.Panic(err)
}
}
//Return true as connection active
return &obj, true
}
func (c Connection[T]) GetPeerCertificate() *x509.Certificate {