package swarm

import (
	
	
	
	

	
	
)

// Validate Stream conforms to the go-libp2p-net Stream interface
var _ network.Stream = &Stream{}

// Stream is the stream type used by swarm. In general, you won't use this type
// directly.
type Stream struct {
	id uint64

	stream network.MuxedStream
	conn   *Conn
	scope  network.StreamManagementScope

	closeMx  sync.Mutex
	isClosed bool
	// acceptStreamGoroutineCompleted indicates whether the goroutine handling the incoming stream has exited
	acceptStreamGoroutineCompleted bool

	protocol atomic.Pointer[protocol.ID]

	stat network.Stats
}

func ( *Stream) () string {
	// format: <first 10 chars of peer id>-<global conn ordinal>-<global stream ordinal>
	return fmt.Sprintf("%s-%d", .conn.ID(), .id)
}

func ( *Stream) () string {
	return fmt.Sprintf(
		"<swarm.Stream[%s] %s (%s) <-> %s (%s)>",
		.conn.conn.Transport(),
		.conn.LocalMultiaddr(),
		.conn.LocalPeer(),
		.conn.RemoteMultiaddr(),
		.conn.RemotePeer(),
	)
}

// Conn returns the Conn associated with this stream, as an network.Conn
func ( *Stream) () network.Conn {
	return .conn
}

// Read reads bytes from a stream.
func ( *Stream) ( []byte) (int, error) {
	,  := .stream.Read()
	// TODO: push this down to a lower level for better accuracy.
	if .conn.swarm.bwc != nil {
		.conn.swarm.bwc.LogRecvMessage(int64())
		.conn.swarm.bwc.LogRecvMessageStream(int64(), .Protocol(), .Conn().RemotePeer())
	}
	return , 
}

// Write writes bytes to a stream, flushing for each call.
func ( *Stream) ( []byte) (int, error) {
	,  := .stream.Write()
	// TODO: push this down to a lower level for better accuracy.
	if .conn.swarm.bwc != nil {
		.conn.swarm.bwc.LogSentMessage(int64())
		.conn.swarm.bwc.LogSentMessageStream(int64(), .Protocol(), .Conn().RemotePeer())
	}
	return , 
}

// Close closes the stream, closing both ends and freeing all associated
// resources.
func ( *Stream) () error {
	 := .stream.Close()
	.closeAndRemoveStream()
	return 
}

// Reset resets the stream, signaling an error on both ends and freeing all
// associated resources.
func ( *Stream) () error {
	 := .stream.Reset()
	.closeAndRemoveStream()
	return 
}

func ( *Stream) ( network.StreamErrorCode) error {
	 := .stream.ResetWithError()
	.closeAndRemoveStream()
	return 
}

func ( *Stream) () {
	.closeMx.Lock()
	defer .closeMx.Unlock()
	if .isClosed {
		return
	}
	.isClosed = true
	// We don't want to keep swarm from closing till the stream handler has exited
	.conn.swarm.refs.Done()
	// Cleanup the stream from connection only after the stream handler has completed
	if .acceptStreamGoroutineCompleted {
		.conn.removeStream()
	}
}

// CloseWrite closes the stream for writing, flushing all data and sending an EOF.
// This function does not free resources, call Close or Reset when done with the
// stream.
func ( *Stream) () error {
	return .stream.CloseWrite()
}

// CloseRead closes the stream for reading. This function does not free resources,
// call Close or Reset when done with the stream.
func ( *Stream) () error {
	return .stream.CloseRead()
}

func ( *Stream) () {
	.closeMx.Lock()
	defer .closeMx.Unlock()
	if .acceptStreamGoroutineCompleted {
		return
	}
	.acceptStreamGoroutineCompleted = true
	if .isClosed {
		.conn.removeStream()
	}
}

// Protocol returns the protocol negotiated on this stream (if set).
func ( *Stream) () protocol.ID {
	 := .protocol.Load()
	if  == nil {
		return ""
	}
	return *
}

// SetProtocol sets the protocol for this stream.
//
// This doesn't actually *do* anything other than record the fact that we're
// speaking the given protocol over this stream. It's still up to the user to
// negotiate the protocol. This is usually done by the Host.
func ( *Stream) ( protocol.ID) error {
	if  := .scope.SetProtocol();  != nil {
		return 
	}

	.protocol.Store(&)
	return nil
}

// SetDeadline sets the read and write deadlines for this stream.
func ( *Stream) ( time.Time) error {
	return .stream.SetDeadline()
}

// SetReadDeadline sets the read deadline for this stream.
func ( *Stream) ( time.Time) error {
	return .stream.SetReadDeadline()
}

// SetWriteDeadline sets the write deadline for this stream.
func ( *Stream) ( time.Time) error {
	return .stream.SetWriteDeadline()
}

// Stat returns metadata information for this stream.
func ( *Stream) () network.Stats {
	return .stat
}

func ( *Stream) () network.StreamScope {
	return .scope
}