66 "github.com/quic-go/quic-go"
77 "github.com/yomorun/yomo/core/frame"
88 "github.com/yomorun/yomo/core/metadata"
9+ "golang.org/x/exp/slog"
910)
1011
1112// ConnectionInfo holds the information of connection.
@@ -22,77 +23,87 @@ type ConnectionInfo interface {
2223 ObserveDataTags () []frame.Tag
2324}
2425
25- // Connection wraps conneciton and stream to transfer frames.
26- // Connection be used to read and write frames, and be managed by Connector.
27- type Connection interface {
28- Context () context.Context
29- ConnectionInfo
30- frame.ReadWriteCloser
31- // CloseWithError closes the connection with an error string.
32- CloseWithError (string ) error
33- }
34-
35- type connection struct {
26+ // Connection wraps connection and stream for transmitting frames, it can be
27+ // used for reading and writing frames, and is managed by the Connector.
28+ type Connection struct {
3629 name string
3730 id string
3831 clientType ClientType
3932 metadata metadata.M
4033 observeDataTags []uint32
4134 conn quic.Connection
4235 fs * FrameStream
36+ Logger * slog.Logger
4337}
4438
4539func newConnection (
4640 name string , id string , clientType ClientType , md metadata.M , tags []uint32 ,
47- conn quic.Connection , fs * FrameStream ) * connection {
48- return & connection {
41+ conn quic.Connection , fs * FrameStream , logger * slog.Logger ) * Connection {
42+
43+ logger = logger .With ("conn_id" , id , "conn_name" , name )
44+ if conn != nil {
45+ logger .Info ("new client connected" , "remote_addr" , conn .RemoteAddr ().String (), "client_type" , clientType .String ())
46+ }
47+
48+ return & Connection {
4949 name : name ,
5050 id : id ,
5151 clientType : clientType ,
5252 metadata : md ,
5353 observeDataTags : tags ,
5454 conn : conn ,
5555 fs : fs ,
56+ Logger : logger ,
5657 }
5758}
5859
59- func (c * connection ) Close () error {
60+ // Close closes the connection.
61+ func (c * Connection ) Close () error {
6062 return c .fs .Close ()
6163}
6264
63- func (c * connection ) Context () context.Context {
65+ // Context returns the context of the connection.
66+ func (c * Connection ) Context () context.Context {
6467 return c .fs .Context ()
6568}
6669
67- func (c * connection ) ID () string {
70+ // ID returns the connection ID.
71+ func (c * Connection ) ID () string {
6872 return c .id
6973}
7074
71- func (c * connection ) Metadata () metadata.M {
75+ // Metadata returns the extra info of the application.
76+ func (c * Connection ) Metadata () metadata.M {
7277 return c .metadata
7378}
7479
75- func (c * connection ) Name () string {
80+ // Name returns the name of the connection
81+ func (c * Connection ) Name () string {
7682 return c .name
7783}
7884
79- func (c * connection ) ObserveDataTags () []uint32 {
85+ // ObserveDataTags returns the observed data tags.
86+ func (c * Connection ) ObserveDataTags () []uint32 {
8087 return c .observeDataTags
8188}
8289
83- func (c * connection ) ReadFrame () (frame.Frame , error ) {
90+ // ReadFrame reads a frame from the connection.
91+ func (c * Connection ) ReadFrame () (frame.Frame , error ) {
8492 return c .fs .ReadFrame ()
8593}
8694
87- func (c * connection ) ClientType () ClientType {
95+ // ClientType returns the client type of the connection.
96+ func (c * Connection ) ClientType () ClientType {
8897 return c .clientType
8998}
9099
91- func (c * connection ) WriteFrame (f frame.Frame ) error {
100+ // WriteFrame writes a frame to the connection.
101+ func (c * Connection ) WriteFrame (f frame.Frame ) error {
92102 return c .fs .WriteFrame (f )
93103}
94104
95- func (c * connection ) CloseWithError (errString string ) error {
105+ // CloseWithError closes the connection with error.
106+ func (c * Connection ) CloseWithError (errString string ) error {
96107 return c .conn .CloseWithError (YomoCloseErrorCode , errString )
97108}
98109
0 commit comments