@@ -18,13 +18,13 @@ import kotlin.coroutines.*
18
18
19
19
internal class DatagramSocketImpl (
20
20
private val descriptor : Int ,
21
- private val selector : SelectorManager ,
21
+ val selector : SelectorManager ,
22
22
private val _localAddress : NetworkAddress ,
23
23
private val _remoteAddress : NetworkAddress ? ,
24
24
parent : CoroutineContext = EmptyCoroutineContext
25
25
) : BoundDatagramSocket, ConnectedDatagramSocket, Socket, CoroutineScope {
26
26
private val _context : CompletableJob = Job (parent[Job ])
27
- private val selectable: SelectableNative = SelectableNative (descriptor)
27
+ val selectable: SelectableNative = SelectableNative (descriptor)
28
28
29
29
override val coroutineContext: CoroutineContext = parent + Dispatchers .Unconfined + _context
30
30
@@ -36,7 +36,7 @@ internal class DatagramSocketImpl(
36
36
override val remoteAddress: NetworkAddress
37
37
get() = _remoteAddress !! // TODO: What should happen here?
38
38
39
- private val sender = Channel <Datagram >( )
39
+ private val sender: SendChannel <Datagram > = DatagramSendChannel (descriptor, this )
40
40
41
41
private val receiver = produce<Datagram >(coroutineContext) {
42
42
while (true ) {
@@ -47,12 +47,6 @@ internal class DatagramSocketImpl(
47
47
48
48
init {
49
49
makeShared()
50
-
51
- launch {
52
- sender.consumeEach { datagram ->
53
- sendImpl(datagram)
54
- }
55
- }
56
50
}
57
51
58
52
override val outgoing: SendChannel <Datagram >
@@ -66,36 +60,6 @@ internal class DatagramSocketImpl(
66
60
override fun attachForWriting (channel : ByteChannel ): ReaderJob =
67
61
attachForWritingImpl(channel, descriptor, selectable, selector)
68
62
69
- private tailrec suspend fun sendImpl (
70
- datagram : Datagram ,
71
- bytes : ByteArray = datagram.packet.readBytes()
72
- ) {
73
- var bytesWritten: Int? = null
74
- bytes.usePinned { pinned ->
75
- datagram.address.address.nativeAddress { address, addressSize ->
76
- bytesWritten = sendto(
77
- descriptor,
78
- pinned.addressOf(0 ),
79
- bytes.size.convert(),
80
- 0 ,
81
- address,
82
- addressSize
83
- ).toInt()
84
- }
85
- }
86
- when (bytesWritten ? : error(" bytesWritten cannot be null" )) {
87
- 0 -> throw IOException (" Failed writing to closed socket" )
88
- - 1 -> {
89
- if (errno == EAGAIN ) {
90
- selector.select(selectable, SelectInterest .WRITE )
91
- sendImpl(datagram, bytes)
92
- } else {
93
- throw PosixException .forErrno()
94
- }
95
- }
96
- }
97
- }
98
-
99
63
private tailrec suspend fun receiveImpl (
100
64
buffer : IoBuffer = DefaultDatagramByteBufferPool .borrow()
101
65
): Datagram {
0 commit comments