package exchange

import (
	
	

	

	
	
	
	
	
)

type unencryptedWriter struct {
	clock   clock.Clock
	conn    transport.Conn
	timeout time.Duration
	input   proto.MessageType
	output  proto.MessageType
}

func ( unencryptedWriter) ( context.Context,  *bin.Buffer,  bin.Encoder) error {
	.Reset()

	if  := .Encode();  != nil {
		return 
	}
	 := proto.UnencryptedMessage{
		MessageID:   int64(proto.NewMessageID(.clock.Now(), .output)),
		MessageData: .Copy(),
	}
	.Reset()
	if  := .Encode();  != nil {
		return 
	}

	,  := context.WithTimeout(, .timeout)
	defer ()
	return .conn.Send(, )
}

func ( unencryptedWriter) ( context.Context,  *bin.Buffer) error {
	,  := context.WithTimeout(, .timeout)
	defer ()

	if  := .conn.Recv(, );  != nil {
		return 
	}

	return nil
}

func ( unencryptedWriter) () bool {
	return .output == proto.MessageFromClient
}

func ( unencryptedWriter) ( context.Context,  *bin.Buffer,  bin.Decoder) error {
	.Reset()

	for {
		if  := .tryRead(, );  != nil {
			var  *codec.ProtocolErr
			if .isClient() &&
				errors.As(, &) &&
				.Code == codec.CodeAuthKeyNotFound {
				continue
			}
			return 
		}

		break
	}

	var  proto.UnencryptedMessage
	if  := .Decode();  != nil {
		return 
	}
	if  := .checkMsgID(.MessageID);  != nil {
		return 
	}
	.ResetTo(.MessageData)

	return .Decode()
}

func ( unencryptedWriter) ( int64) error {
	if proto.MessageID().Type() != .input {
		return errors.New("bad msg type")
	}
	return nil
}