fix some stateful stuff

This commit is contained in:
Ben Harris 2020-04-28 01:52:24 -04:00
parent f1c4ed9ae8
commit 8bc9eb4b75
3 changed files with 19 additions and 14 deletions

View File

@ -201,9 +201,9 @@ namespace IrcTokens
if (Params != null && Params.Any()) if (Params != null && Params.Any())
{ {
var last = Params[^1]; var last = Params[^1];
Params.RemoveAt(Params.Count - 1); var withoutLast = Params.SkipLast(1).ToList();
foreach (var p in Params) foreach (var p in withoutLast)
{ {
if (p.Contains(' ', StringComparison.Ordinal)) if (p.Contains(' ', StringComparison.Ordinal))
throw new ArgumentException(@"non-last parameters cannot have spaces", p); throw new ArgumentException(@"non-last parameters cannot have spaces", p);
@ -212,7 +212,7 @@ namespace IrcTokens
throw new ArgumentException(@"non-last parameters cannot start with colon", p); throw new ArgumentException(@"non-last parameters cannot start with colon", p);
} }
outs.AddRange(Params); outs.AddRange(withoutLast);
if (string.IsNullOrWhiteSpace(last) || last.Contains(' ', StringComparison.Ordinal) || if (string.IsNullOrWhiteSpace(last) || last.Contains(' ', StringComparison.Ordinal) ||
last.StartsWith(':')) last.StartsWith(':'))

View File

@ -90,20 +90,20 @@ namespace IrcTokens
} }
} }
var decodeLines = new List<string>(); _buffer = listLines[^1].ToArray();
foreach (var line in listLines.Select(l => l.ToArray()))
var decodeLines = new List<Line>();
foreach (var line in listLines.SkipLast(1).Select(l => l.ToArray()))
try try
{ {
decodeLines.Add(Encoding.GetString(line)); decodeLines.Add(new Line(Encoding.GetString(line)));
} }
catch (DecoderFallbackException) catch (DecoderFallbackException)
{ {
decodeLines.Add(Fallback.GetString(line)); decodeLines.Add(new Line(Fallback.GetString(line)));
} }
return decodeLines return decodeLines;
.Select(l => new Line(l))
.ToList();
} }
} }
} }

View File

@ -25,12 +25,11 @@ namespace TokensSample
_socket.Connect("127.0.0.1", 6667); _socket.Connect("127.0.0.1", 6667);
Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}}); Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}});
Send(new Line {Command = "USER", Params = new List<string> {"username", "0", "*", "real name"}}); Send(new Line {Command = "USER", Params = new List<string> {"tokensbot", "0", "*", "real name"}});
while (true) while (true)
{ {
var bytesReceived = _socket.Receive(_bytes); var bytesReceived = _socket.Receive(_bytes);
var lines = _decoder.Push(_bytes);
if (bytesReceived == 0) if (bytesReceived == 0)
{ {
@ -39,6 +38,8 @@ namespace TokensSample
break; break;
} }
var lines = _decoder.Push(_bytes);
foreach (var line in lines) foreach (var line in lines)
{ {
Console.WriteLine($"< {line.Format()}"); Console.WriteLine($"< {line.Format()}");
@ -49,7 +50,10 @@ namespace TokensSample
Send(new Line {Command = "PONG", Params = line.Params}); Send(new Line {Command = "PONG", Params = line.Params});
break; break;
case "001": case "001":
Send(new Line {Command = "JOIN", Params = new List<string> {"#channel"}}); Send(new Line {Command = "JOIN", Params = new List<string> {"#test"}});
break;
case "PRIVMSG":
Send(new Line {Command = "PRIVMSG", Params = new List<string> {line.Params[0], "hello there"}});
break; break;
} }
} }
@ -60,7 +64,8 @@ namespace TokensSample
{ {
Console.WriteLine($"> {line.Format()}"); Console.WriteLine($"> {line.Format()}");
_encoder.Push(line); _encoder.Push(line);
while (_encoder.PendingBytes.Length > 0) _encoder.Pop(_socket.Send(_encoder.PendingBytes)); while (_encoder.PendingBytes.Length > 0)
_encoder.Pop(_socket.Send(_encoder.PendingBytes));
} }
} }
} }