-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Convert YamuxType to enum * Refactor YamuxFlags: convert them to Set of enum values.
- Loading branch information
1 parent
ee02cf9
commit e843836
Showing
7 changed files
with
106 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.libp2p.mux.yamux | ||
|
||
import io.libp2p.mux.InvalidFrameMuxerException | ||
|
||
/** | ||
* Contains all the permissible values for flags in the <code>yamux</code> protocol. | ||
*/ | ||
enum class YamuxFlag(val intFlag: Int) { | ||
SYN(1), | ||
ACK(2), | ||
FIN(4), | ||
RST(8); | ||
|
||
val asSet: Set<YamuxFlag> = setOf(this) | ||
|
||
companion object { | ||
val NONE = emptySet<YamuxFlag>() | ||
|
||
private val validFlagCombinations = mapOf( | ||
0 to NONE, | ||
SYN.intFlag to SYN.asSet, | ||
ACK.intFlag to ACK.asSet, | ||
FIN.intFlag to FIN.asSet, | ||
RST.intFlag to RST.asSet, | ||
) | ||
|
||
fun fromInt(flags: Int) = | ||
validFlagCombinations[flags] ?: throw InvalidFrameMuxerException("Invalid Yamux flags value: $flags") | ||
|
||
fun Set<YamuxFlag>.toInt() = this | ||
.fold(0) { acc, flag -> acc or flag.intFlag } | ||
.also { require(it in validFlagCombinations) { "Invalid Yamux flags combination: $this" } } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
package io.libp2p.mux.yamux | ||
|
||
import io.libp2p.mux.InvalidFrameMuxerException | ||
|
||
/** | ||
* Contains all the permissible values for types in the <code>yamux</code> protocol. | ||
*/ | ||
object YamuxType { | ||
const val DATA = 0 | ||
const val WINDOW_UPDATE = 1 | ||
const val PING = 2 | ||
const val GO_AWAY = 3 | ||
enum class YamuxType(val intValue: Int) { | ||
DATA(0), | ||
WINDOW_UPDATE(1), | ||
PING(2), | ||
GO_AWAY(3); | ||
|
||
companion object { | ||
private val intToTypeCache = values().associateBy { it.intValue } | ||
|
||
fun fromInt(intValue: Int): YamuxType = | ||
intToTypeCache[intValue] ?: throw InvalidFrameMuxerException("Invalid Yamux type value: $intValue") | ||
} | ||
} |
Oops, something went wrong.