3
3
import java .io .IOException ;
4
4
import java .nio .ByteBuffer ;
5
5
6
+ import net .lecousin .framework .application .LCCore ;
6
7
import net .lecousin .framework .collections .TurnArray ;
8
+ import net .lecousin .framework .concurrent .CancelException ;
7
9
import net .lecousin .framework .concurrent .synch .AsyncWork ;
8
10
import net .lecousin .framework .concurrent .synch .ISynchronizationPoint ;
9
11
import net .lecousin .framework .concurrent .synch .SynchronizationPoint ;
10
12
import net .lecousin .framework .exception .NoException ;
11
13
import net .lecousin .framework .io .IO ;
12
14
import net .lecousin .framework .util .Pair ;
13
- import net .lecousin .framework .util .RunnableWithParameter ;
14
15
15
16
/**
16
17
* This class allows to queue write operations, but blocks if too many are waiting.
@@ -42,55 +43,68 @@ public AsyncWork<Integer,IOException> write(ByteBuffer buffer) throws IOExceptio
42
43
synchronized (waiting ) {
43
44
if (lastWrite .isCancelled ()) return lastWrite ;
44
45
if (waiting .isEmpty () && lastWrite .isUnblocked ()) {
45
- lastWrite = io .writeAsync (buffer , new RunnableWithParameter <Pair <Integer ,IOException >>() {
46
- @ Override
47
- public void run (Pair <Integer , IOException > param ) {
48
- writeDone (buffer );
49
- }
50
- });
51
- lastWrite .onCancel ((cancel ) -> {
52
- writeDone (buffer );
53
- });
46
+ LCCore .getApplication ().getConsole ().out ("write direct" );
47
+ lastWrite = io .writeAsync (buffer );
48
+ lastWrite .listenInline (
49
+ (nb ) -> { writeDone (buffer , null ); },
50
+ (error ) -> { writeDone (buffer , null ); },
51
+ (cancel ) -> { writeDone (buffer , cancel ); }
52
+ );
54
53
return lastWrite ;
55
54
}
56
55
if (!waiting .isFull ()) {
56
+ LCCore .getApplication ().getConsole ().out ("push write" );
57
57
AsyncWork <Integer ,IOException > res = new AsyncWork <>();
58
58
waiting .addLast (new Pair <>(buffer , res ));
59
+ lastWrite = res ;
59
60
return res ;
60
61
}
62
+ LCCore .getApplication ().getConsole ().out ("waiting has " + waiting .size ());
61
63
if (lock != null )
62
64
throw new IOException ("Concurrent write" );
63
65
lock = new SynchronizationPoint <>();
64
66
lk = lock ;
65
67
}
68
+ LCCore .getApplication ().getConsole ().out ("block" );
66
69
lk .block (0 );
70
+ LCCore .getApplication ().getConsole ().out ("unblocked" );
67
71
} while (true );
68
72
}
69
73
70
- protected void writeDone (@ SuppressWarnings ("unused" ) ByteBuffer buffer ) {
74
+ protected void writeDone (@ SuppressWarnings ("unused" ) ByteBuffer buffer , CancelException cancelled ) {
75
+ LCCore .getApplication ().getConsole ().out ("writeDone" );
71
76
SynchronizationPoint <NoException > sp = null ;
72
77
synchronized (waiting ) {
73
78
Pair <ByteBuffer ,AsyncWork <Integer ,IOException >> b = waiting .pollFirst ();
79
+ LCCore .getApplication ().getConsole ().out ("b = " + b );
74
80
if (b != null ) {
75
81
if (lock != null ) {
76
82
sp = lock ;
77
83
lock = null ;
78
84
}
79
- if (lastWrite . isCancelled () ) {
85
+ if (cancelled != null ) {
80
86
while (b != null ) {
81
- b .getValue2 ().cancel (lastWrite . getCancelEvent () );
87
+ b .getValue2 ().cancel (cancelled );
82
88
b = waiting .pollFirst ();
83
89
}
84
90
} else {
85
91
ByteBuffer buf = b .getValue1 ();
86
- lastWrite = io .writeAsync (buf , new RunnableWithParameter <Pair <Integer ,IOException >>() {
87
- @ Override
88
- public void run (Pair <Integer , IOException > param ) {
89
- writeDone (buf );
92
+ AsyncWork <Integer , IOException > write = io .writeAsync (buf );
93
+ Pair <ByteBuffer ,AsyncWork <Integer ,IOException >> bb = b ;
94
+ write .listenInline (
95
+ (nb ) -> {
96
+ bb .getValue2 ().unblockSuccess (nb );
97
+ writeDone (buf , null );
98
+ },
99
+ (error ) -> {
100
+ bb .getValue2 ().error (error );
101
+ writeDone (buf , null );
102
+ },
103
+ (cancel ) -> {
104
+ bb .getValue2 ().cancel (cancel );
105
+ writeDone (buf , cancel );
90
106
}
91
- });
92
- lastWrite .onCancel ((cancel ) -> { writeDone (buf ); });
93
- lastWrite .listenInline (b .getValue2 ());
107
+ );
94
108
}
95
109
}
96
110
}
@@ -100,10 +114,7 @@ public void run(Pair<Integer, IOException> param) {
100
114
101
115
/** Return the last pending operation, or null. */
102
116
public AsyncWork <Integer , IOException > getLastPendingOperation () {
103
- Pair <ByteBuffer ,AsyncWork <Integer ,IOException >> b = waiting .peekLast ();
104
- if (b == null )
105
- return lastWrite .isUnblocked () ? null : lastWrite ;
106
- return b .getValue2 ();
117
+ return lastWrite .isUnblocked () ? null : lastWrite ;
107
118
}
108
119
109
120
/** Same as getLastPendingOperation but never return null (return an unblocked synchronization point instead). */
0 commit comments