Add sample project
This commit is contained in:
parent
06a9882f65
commit
ca1518a970
|
@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio Version 16
|
# Visual Studio Version 16
|
||||||
VisualStudioVersion = 16.0.30011.22
|
VisualStudioVersion = 16.0.30011.22
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IrcTokens", "IrcTokens\IrcTokens.csproj", "{9E812F45-B2CD-42D2-8378-EBEBF8697905}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IrcTokens", "IrcTokens\IrcTokens.csproj", "{9E812F45-B2CD-42D2-8378-EBEBF8697905}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "Sample\Sample.csproj", "{A45DA39B-6B47-4713-8049-3B36E0235B67}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
@ -15,6 +17,10 @@ Global
|
||||||
{9E812F45-B2CD-42D2-8378-EBEBF8697905}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{9E812F45-B2CD-42D2-8378-EBEBF8697905}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{9E812F45-B2CD-42D2-8378-EBEBF8697905}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{9E812F45-B2CD-42D2-8378-EBEBF8697905}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{9E812F45-B2CD-42D2-8378-EBEBF8697905}.Release|Any CPU.Build.0 = Release|Any CPU
|
{9E812F45-B2CD-42D2-8378-EBEBF8697905}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{A45DA39B-6B47-4713-8049-3B36E0235B67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{A45DA39B-6B47-4713-8049-3B36E0235B67}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{A45DA39B-6B47-4713-8049-3B36E0235B67}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{A45DA39B-6B47-4713-8049-3B36E0235B67}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
|
@ -18,8 +18,21 @@ namespace IrcTokens
|
||||||
private Hostmask _hostmask;
|
private Hostmask _hostmask;
|
||||||
private readonly string _rawLine;
|
private readonly string _rawLine;
|
||||||
|
|
||||||
public override string ToString() =>
|
public override string ToString()
|
||||||
$"Line(source={Source}, command={Command}, tags={string.Join(";", Tags.Select(kvp => $"{kvp.Key}={kvp.Value}"))}, params={string.Join(",", Params)})";
|
{
|
||||||
|
var vars = new List<string>();
|
||||||
|
|
||||||
|
if (Command != null)
|
||||||
|
vars.Add($"command={Command}");
|
||||||
|
if (Source != null)
|
||||||
|
vars.Add($"source={Source}");
|
||||||
|
if (Params != null && Params.Any())
|
||||||
|
vars.Add($"params=[{string.Join(",", Params)}]");
|
||||||
|
if (Tags != null && Tags.Any())
|
||||||
|
vars.Add($"tags=[{string.Join(";", Tags.Select(kvp => $"{kvp.Key}={kvp.Value}"))}]");
|
||||||
|
|
||||||
|
return $"Line({string.Join(", ", vars)})";
|
||||||
|
}
|
||||||
|
|
||||||
public override int GetHashCode() => Format().GetHashCode(StringComparison.Ordinal);
|
public override int GetHashCode() => Format().GetHashCode(StringComparison.Ordinal);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using IrcTokens;
|
||||||
|
|
||||||
|
namespace Sample
|
||||||
|
{
|
||||||
|
public class Client
|
||||||
|
{
|
||||||
|
private readonly Socket _socket;
|
||||||
|
private readonly StatefulDecoder _decoder;
|
||||||
|
private readonly StatefulEncoder _encoder;
|
||||||
|
private readonly byte[] _bytes;
|
||||||
|
|
||||||
|
public Client()
|
||||||
|
{
|
||||||
|
_decoder = new StatefulDecoder();
|
||||||
|
_encoder = new StatefulEncoder();
|
||||||
|
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
|
||||||
|
_bytes = new byte[1024];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
_socket.Connect("127.0.0.1", 6667);
|
||||||
|
|
||||||
|
Send(new Line {Command = "USER", Params = new List<string> {"username", "0", "*", "real name"}});
|
||||||
|
Send(new Line {Command = "NICK", Params = new List<string> {"statefulbot"}});
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
var bytesReceived = _socket.Receive(_bytes);
|
||||||
|
var lines = _decoder.Push(_bytes);
|
||||||
|
|
||||||
|
if (lines.Count == 0)
|
||||||
|
{
|
||||||
|
Console.WriteLine("! disconnected");
|
||||||
|
_socket.Shutdown(SocketShutdown.Both);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var line in lines)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"< {line.Format()}");
|
||||||
|
|
||||||
|
switch (line.Command)
|
||||||
|
{
|
||||||
|
case "PING":
|
||||||
|
Send(new Line {Command = "PONG", Params = line.Params});
|
||||||
|
break;
|
||||||
|
case "001":
|
||||||
|
Send(new Line {Command = "JOIN", Params = new List<string> {"#channel"}});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Send(Line line)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"> {line.Format()}");
|
||||||
|
_encoder.Push(line);
|
||||||
|
while (_encoder.PendingBytes.Length > 0)
|
||||||
|
_encoder.Pop(_socket.Send(_encoder.PendingBytes));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using IrcTokens;
|
||||||
|
|
||||||
|
namespace Sample
|
||||||
|
{
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
// tokenization
|
||||||
|
var line = new Line("@id=123 :ben!~ben@hostname PRIVMSG #channel :hello there!");
|
||||||
|
Console.WriteLine(line);
|
||||||
|
Console.WriteLine(line.Format());
|
||||||
|
|
||||||
|
// formatting
|
||||||
|
var line2 = new Line
|
||||||
|
{
|
||||||
|
Command = "USER",
|
||||||
|
Params = new List<string> {"user", "0", "*", "real name"}
|
||||||
|
};
|
||||||
|
Console.WriteLine(line2);
|
||||||
|
Console.WriteLine(line2.Format());
|
||||||
|
|
||||||
|
// stateful example
|
||||||
|
var client = new Client();
|
||||||
|
client.Start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\IrcTokens\IrcTokens.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
Loading…
Reference in New Issue