public void Handle(ref SocketPipelineContext context, ref ReadOnlySequence <byte> data) { using (MemoryStream memoryStream = new MemoryStream()) { using (ICryptoTransform decryptor = this.Crypt.CreateDecryptor()) { using CryptoStream stream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write, leaveOpen: true); byte[] array = data.ToArray(); Base64.DecodeFromUtf8InPlace(array, out int writtenBytes); stream.Write(array, 0, writtenBytes); } ReadOnlySequence <byte> plank = new ReadOnlySequence <byte>(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); //Encryption leaves some ugly null bytes SequencePosition?uglyNulls = plank.PositionOf((byte)'\0'); if (uglyNulls != null) { plank = plank.Slice(start: 0, end: uglyNulls.Value); } context.ProgressHandlerIn(ref plank); } }
public void Handle(ref SocketPipelineContext context, ref ReadOnlySequence <byte> data) { Utf8JsonReader jsonReader = new Utf8JsonReader(data); Console.WriteLine($"-> {Encoding.UTF8.GetString(data.ToArray())}"); //All packets are arrays! Yey! string packetName = jsonReader.ReadString(); switch (packetName) { case "greetings": { context.Connection.Send(@"[""ver"", ""Creation Server"", ""0""]"); //Server type, Server id(?) context.Connection.Send(@"[""pleaseLogin""]"); } break; case "clientVersion": { //Please login here } break; case "login": { context.Connection.Send(@"[""selfIntroduction"", [""1"", ""j"", ""Joni"", ""2"", ""what"", ""{}""]]"); //Id, Site, Name, Power, Avatar, Server vars? context.Connection.Send(@"[""connectionReady""]"); } break; case "createGroup": { string groupType = jsonReader.ReadString(); if (groupType == "CreationGroup") { string groupName = jsonReader.ReadString(); context.Connection.Send(@$ "[" "createGroupResult" ", " "{groupName}" ", " "true" "]"); //Success context.Connection.Send(@$ "[" "{groupName}cp" ", " "1" "]"); //User id } } break; case "update": { string groupName = jsonReader.ReadString(); List <int[][]> tiles = JsonSerializer.Deserialize <List <int[][]> >(jsonReader.ReadString()); tiles.RemoveAll((i) => i == null); //For whatever reason the client sends null's if (tiles.Count > 0) { context.Connection.Send(@$ "[" "{groupName}step" ", " "{this.Step++}" ", {JsonSerializer.Serialize(tiles)}, " "" "]"); //Step num, tiles (x, y) coord pairs, user server vars } } break; } }
public void Handle(ref SocketPipelineContext context, ref ReadOnlySequence <byte> data) { SequencePosition?position = data.PositionOf(SplitMessageHandler.BREAK_CHAR); if (position != null) { ReadOnlySequence <byte> withoutTerminator = data.Slice(start: 0, end: position.Value); context.ProgressReadHandler(ref withoutTerminator); data = data.Slice(start: data.GetPosition(1, position.Value)); } }
public void Handle(ref SocketPipelineContext context, ref ReadOnlySequence <byte> data) { Span <byte> checksum = stackalloc byte[3]; CryptoUtils.CreateChecksumHash(++this.IncomingCount, ref checksum); Span <byte> checkAgainst = stackalloc byte[3]; data.Slice(start: 0, length: 3).CopyTo(checkAgainst); if (checkAgainst.SequenceEqual(checksum)) { data = data.Slice(start: 3); context.ProgressReadHandler(ref data); } else { context.Disconnect("Incoming checksum error"); } }
public void Handle <T>(ref SocketPipelineContext context, in T data, ref PacketWriter writer)
public void Handle(ref SocketPipelineContext context, in byte[] data, ref PacketWriter writer)