[PD1] Fixed almost everything
This commit is contained in:
parent
39a0e5c01f
commit
7b3172a850
13 changed files with 534 additions and 192 deletions
|
@ -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 {
|
||||
|
|
|
@ -43,7 +43,6 @@ func (s *Server[T]) ListenLoop() {
|
|||
|
||||
state := tlsConn.ConnectionState()
|
||||
if len(state.PeerCertificates) == 0 {
|
||||
fmt.Println(state.PeerCertificates)
|
||||
log.Panicln("Client did not provide a certificate")
|
||||
}
|
||||
conn := NewConnection[T](tlsConn)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue