package transport

import (
	
	
	
	

	

	
)

// Conn is transport connection.
type Conn interface {
	Send(ctx context.Context, b *bin.Buffer) error
	Recv(ctx context.Context, b *bin.Buffer) error
	Close() error
}

var _ Conn = (*connection)(nil)

// connection is MTProto connection.
type connection struct {
	conn  net.Conn
	codec Codec

	readMux  sync.Mutex
	writeMux sync.Mutex
}

// Send sends message from buffer using MTProto connection.
func ( *connection) ( context.Context,  *bin.Buffer) error {
	// Serializing access to deadlines.
	.writeMux.Lock()
	defer .writeMux.Unlock()

	if  := .conn.SetWriteDeadline(time.Time{});  != nil {
		return errors.Wrap(, "reset write deadline")
	}
	if ,  := .Deadline();  {
		if  := .conn.SetWriteDeadline();  != nil {
			return errors.Wrap(, "set write deadline")
		}
	}

	if  := .codec.Write(.conn, );  != nil {
		return errors.Wrap(, "write")
	}

	return nil
}

// Recv reads message to buffer using MTProto connection.
func ( *connection) ( context.Context,  *bin.Buffer) error {
	// Serializing access to deadlines.
	.readMux.Lock()
	defer .readMux.Unlock()

	if  := .conn.SetReadDeadline(time.Time{});  != nil {
		return errors.Wrap(, "reset read deadline")
	}
	if ,  := .Deadline();  {
		if  := .conn.SetReadDeadline();  != nil {
			return errors.Wrap(, "set read deadline")
		}
	}

	if  := .codec.Read(.conn, );  != nil {
		return errors.Wrap(, "read")
	}

	return nil
}

// Close closes MTProto connection.
func ( *connection) () error {
	return .conn.Close()
}