Skip to content

Commit

Permalink
Add CancellationToken support.
Browse files Browse the repository at this point in the history
  • Loading branch information
pluskal committed Oct 2, 2020
1 parent 0b19f92 commit 280cd8c
Showing 1 changed file with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ class CSharpAsyncCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCo
out.dec
out.puts(s"} else if (${privateMemberName(EndianIdentifier)} == true) {")
out.inc
out.puts("await ReadLEAsync();")
out.puts("await ReadLEAsync(cancellationToken);")
out.dec
out.puts("} else {")
out.inc
out.puts("await ReadBEAsync();")
out.puts("await ReadBEAsync(cancellationToken);")
out.dec
out.puts("}")
}
Expand All @@ -158,7 +158,7 @@ class CSharpAsyncCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCo
case Some(e) => s"${e.toSuffix.toUpperCase}"
case None => ""
}
out.puts(s"public async Task<${type2class(_className)}> Read${suffix}Async()")
out.puts(s"public async Task<${type2class(_className)}> Read${suffix}Async(CancellationToken cancellationToken = default)")
out.puts("{")
out.inc
}
Expand Down Expand Up @@ -273,10 +273,10 @@ class CSharpAsyncCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCo
out.puts(s"long _pos = $io.Pos;")

override def seek(io: String, pos: Ast.expr): Unit =
out.puts(s"await $io.SeekAsync(${expression(pos)});")
out.puts(s"await $io.SeekAsync(${expression(pos)}, cancellationToken);")

override def popPos(io: String): Unit =
out.puts(s"await $io.SeekAsync(_pos);")
out.puts(s"await $io.SeekAsync(_pos, cancellationToken);")

override def alignToByte(io: String): Unit =
out.puts(s"$io.AlignToByte();")
Expand Down Expand Up @@ -305,7 +305,7 @@ class CSharpAsyncCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCo
out.puts("{")
out.inc
out.puts("var i = 0;")
out.puts(s"while (! await $io.IsEofAsync()) {")
out.puts(s"while (! await $io.IsEofAsync(cancellationToken)) {")
out.inc
}

Expand Down Expand Up @@ -380,17 +380,17 @@ class CSharpAsyncCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCo
override def parseExpr(dataType: DataType, assignType: DataType, io: String, defEndian: Option[FixedEndian]): String = {
dataType match {
case t: ReadableType =>
s"await $io.Read${Utils.capitalize(t.apiCall(defEndian))}Async()"
s"await $io.Read${Utils.capitalize(t.apiCall(defEndian))}Async(cancellationToken)"
case blt: BytesLimitType =>
s"await $io.ReadBytesAsync(${expression(blt.size)})"
s"await $io.ReadBytesAsync(${expression(blt.size)}, cancellationToken)"
case _: BytesEosType =>
s"await $io.ReadBytesFullAsync()"
s"await $io.ReadBytesFullAsync(cancellationToken)"
case BytesTerminatedType(terminator, include, consume, eosError, _) =>
s"await $io.ReadBytesTermAsync($terminator, $include, $consume, $eosError)"
s"await $io.ReadBytesTermAsync($terminator, $include, $consume, $eosError, cancellationToken)"
case BitsType1 =>
s"await $io.ReadBitsIntAsync(1) != 0"
s"await $io.ReadBitsIntAsync(1, cancellationToken) != 0"
case BitsType(width: Int) =>
s"await $io.ReadBitsIntAsync($width)"
s"await $io.ReadBitsIntAsync($width, cancellationToken)"
case t: UserType =>
val addParams = Utils.join(t.args.map((a) => translator.translate(a)), "", ", ", ", ")
val addArgs = if (t.isOpaque) {
Expand All @@ -407,7 +407,7 @@ class CSharpAsyncCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCo
}
s", $parent, ${privateMemberName(RootIdentifier)}$addEndian"
}
s"await (new ${types2class(t.name)}($addParams$io$addArgs)).ReadAsync()"
s"await (new ${types2class(t.name)}($addParams$io$addArgs)).ReadAsync(cancellationToken)"
}
}

Expand All @@ -424,7 +424,7 @@ class CSharpAsyncCompiler(val typeProvider: ClassTypeProvider, config: RuntimeCo
}

override def userTypeDebugRead(id: String): Unit =
out.puts(s"await $id.ReadAsync();")
out.puts(s"await $id.ReadAsync(cancellationToken);")

override def switchRequiresIfs(onType: DataType): Boolean = onType match {
case _: IntType | _: EnumType | _: StrType => false
Expand Down

0 comments on commit 280cd8c

Please sign in to comment.