add resharper and hover effect for modules; change navigation bar
This commit is contained in:
@@ -6,13 +6,14 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Server_Dashboard_Socket {
|
||||
|
||||
/// <summary>
|
||||
/// Client Socket
|
||||
/// </summary>
|
||||
/// <typeparam name="TProtocol">The Protocol type, either JsonMessageProtocol or XmlMessageProtocol</typeparam>
|
||||
/// <typeparam name="TMessageType">The message type, either JObject or XDocument</typeparam>
|
||||
public class ClientChannel<TProtocol, TMessageType> : SocketChannel<TProtocol, TMessageType>
|
||||
where TProtocol : Protocol<TMessageType>, new(){
|
||||
where TProtocol : Protocol<TMessageType>, new() {
|
||||
|
||||
/// <summary>
|
||||
/// Connect to the socket async
|
||||
@@ -28,4 +29,4 @@ namespace Server_Dashboard_Socket {
|
||||
Attach(socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Server_Dashboard_Socket {
|
||||
|
||||
/// <summary>
|
||||
/// Generic Channel class that handles the connection and message sending / receiving
|
||||
/// Inherits IDisposable to correctly cut the connection to the server
|
||||
@@ -14,13 +15,12 @@ namespace Server_Dashboard_Socket {
|
||||
/// <typeparam name="TMessageType">The message type, either JObject or XDocument</typeparam>
|
||||
public abstract class SocketChannel<TProtocol, TMessageType> : IDisposable
|
||||
where TProtocol : Protocol<TMessageType>, new() {
|
||||
|
||||
protected bool isDisposable = false;
|
||||
NetworkStream networkStream;
|
||||
readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
|
||||
readonly TProtocol protocol = new TProtocol();
|
||||
private NetworkStream networkStream;
|
||||
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
|
||||
private readonly TProtocol protocol = new TProtocol();
|
||||
|
||||
Func<TMessageType, Task> messageCallback;
|
||||
private Func<TMessageType, Task> messageCallback;
|
||||
|
||||
/// <summary>
|
||||
/// Attaches the socket to a network stream that owns the socket
|
||||
@@ -69,10 +69,12 @@ namespace Server_Dashboard_Socket {
|
||||
/// Deconstructor sets Dispose to false
|
||||
/// </summary>
|
||||
~SocketChannel() => Dispose(false);
|
||||
|
||||
/// <summary>
|
||||
/// Sets dispose to true
|
||||
/// </summary>
|
||||
public void Dispose() => Dispose(true);
|
||||
|
||||
/// <summary>
|
||||
/// Disposes open sockets
|
||||
/// </summary>
|
||||
@@ -86,10 +88,10 @@ namespace Server_Dashboard_Socket {
|
||||
Close();
|
||||
//If its closed, dispose it
|
||||
networkStream?.Dispose();
|
||||
//Wait with the garbage collection untill the disposing is done
|
||||
//Wait with the garbage collection until the disposing is done
|
||||
if (isDisposing)
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using Newtonsoft.Json.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Server_Dashboard_Socket {
|
||||
|
||||
public class SocketClient {
|
||||
|
||||
public SocketClient() {
|
||||
@@ -22,7 +23,7 @@ namespace Server_Dashboard_Socket {
|
||||
}
|
||||
|
||||
private async void Start() {
|
||||
//Creates a new endpoint with the IP adress and port
|
||||
//Creates a new endpoint with the IP address and port
|
||||
var endpoint = new IPEndPoint(IPAddress.Loopback, 9000);
|
||||
|
||||
//Creates a new Channel for the Json protocol
|
||||
@@ -50,16 +51,17 @@ namespace Server_Dashboard_Socket {
|
||||
/// </summary>
|
||||
/// <param name="jobject">The json to be converted back</param>
|
||||
/// <returns>Task completed</returns>
|
||||
static Task OnMessage(JObject jobject) {
|
||||
private static Task OnMessage(JObject jobject) {
|
||||
Console.WriteLine(Convert(jobject));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When it receives a message it gets converted from XDocument back to MyMessage
|
||||
/// </summary>
|
||||
/// <param name="xDocument">The xml to be converted back</param>
|
||||
/// <returns>Task completed</returns>
|
||||
static Task OnMessage(XDocument xDocument) {
|
||||
private static Task OnMessage(XDocument xDocument) {
|
||||
Console.WriteLine(Convert(xDocument));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
@@ -69,14 +71,14 @@ namespace Server_Dashboard_Socket {
|
||||
/// </summary>
|
||||
/// <param name="jObject">The json to be converted</param>
|
||||
/// <returns>MyMessage object</returns>
|
||||
static MyMessage Convert(JObject jObject) => jObject.ToObject(typeof(MyMessage)) as MyMessage;
|
||||
private static MyMessage Convert(JObject jObject) => jObject.ToObject(typeof(MyMessage)) as MyMessage;
|
||||
|
||||
/// <summary>
|
||||
/// Converts XDocument to MyMessage
|
||||
/// </summary>
|
||||
/// <param name="xmlDocument">The xml to be converted</param>
|
||||
/// <returns>MyMessage object</returns>
|
||||
static MyMessage Convert(XDocument xmlDocument) => new XmlSerializer(typeof(MyMessage)).Deserialize(new StringReader(xmlDocument.ToString())) as MyMessage;
|
||||
private static MyMessage Convert(XDocument xmlDocument) => new XmlSerializer(typeof(MyMessage)).Deserialize(new StringReader(xmlDocument.ToString())) as MyMessage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -87,4 +89,4 @@ namespace Server_Dashboard_Socket {
|
||||
public int IntProperty { get; set; }
|
||||
public string StringProperty { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,12 @@ using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Server_Dashboard_Socket {
|
||||
|
||||
/// <summary>
|
||||
/// Basic echo server to test the socket connection
|
||||
/// </summary>
|
||||
public class EchoServer {
|
||||
|
||||
/// <summary>
|
||||
/// Start the socket on 127.0.0.1:9000
|
||||
/// </summary>
|
||||
@@ -32,27 +34,26 @@ namespace Server_Dashboard_Socket {
|
||||
/// <returns>poop</returns>
|
||||
private async Task DoEcho(Socket socket) {
|
||||
while (true) {
|
||||
//Listen for incomming connection requests and accept them
|
||||
//Listen for incoming connection requests and accept them
|
||||
Socket clientSocket = await Task.Factory.FromAsync(
|
||||
new Func<AsyncCallback, object, IAsyncResult>(socket.BeginAccept),
|
||||
new Func<IAsyncResult, Socket>(socket.EndAccept),
|
||||
null).ConfigureAwait(false);
|
||||
//Create a network stream and make it the owner of the socket
|
||||
//This will close the socket if the stream is closed and vice verca
|
||||
using(NetworkStream stream = new NetworkStream(clientSocket, true)) {
|
||||
//New buffer for the message in bytes
|
||||
byte[] buffer = new byte[1024];
|
||||
while (true) {
|
||||
//Read everything that somes in
|
||||
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
|
||||
//If its 0 just leave since its a obscolete connection
|
||||
if (bytesRead == 0)
|
||||
break;
|
||||
//Send it back to the sender
|
||||
await stream.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
|
||||
}
|
||||
await using NetworkStream stream = new NetworkStream(clientSocket, true);
|
||||
//New buffer for the message in bytes
|
||||
byte[] buffer = new byte[1024];
|
||||
while (true) {
|
||||
//Read everything that comes in
|
||||
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
|
||||
//If its 0 just leave since its a obsolete connection
|
||||
if (bytesRead == 0)
|
||||
break;
|
||||
//Send it back to the sender
|
||||
await stream.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,21 +7,21 @@ using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace Server_Dashboard_Socket.Protocol {
|
||||
|
||||
/// <summary>
|
||||
/// Json serializer class
|
||||
/// </summary>
|
||||
public class JsonMessageProtocol : Protocol<JObject> {
|
||||
|
||||
//The Json serializer and the settings
|
||||
static readonly JsonSerializer serializer;
|
||||
static readonly JsonSerializerSettings settings;
|
||||
private static readonly JsonSerializer serializer;
|
||||
|
||||
/// <summary>
|
||||
/// Settings for the Json Serializer
|
||||
/// </summary>
|
||||
static JsonMessageProtocol() {
|
||||
//Set the settings
|
||||
settings = new JsonSerializerSettings {
|
||||
JsonSerializerSettings settings = new JsonSerializerSettings {
|
||||
Formatting = Formatting.Indented,
|
||||
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
|
||||
ContractResolver = new DefaultContractResolver {
|
||||
@@ -34,6 +34,7 @@ namespace Server_Dashboard_Socket.Protocol {
|
||||
//Creates the serializer with the settings
|
||||
serializer = JsonSerializer.Create(settings);
|
||||
}
|
||||
|
||||
//Decode the message, to Json
|
||||
protected override JObject Decode(byte[] message) => JObject.Parse(Encoding.UTF8.GetString(message));
|
||||
|
||||
@@ -50,4 +51,4 @@ namespace Server_Dashboard_Socket.Protocol {
|
||||
return Encoding.UTF8.GetBytes(sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,15 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Server_Dashboard_Socket {
|
||||
|
||||
/// <summary>
|
||||
/// Generic Protocol class for Json and Xml serialization
|
||||
/// </summary>
|
||||
/// <typeparam name="TMessageType">JsonMessageProtocol or XmlMessageProtocol</typeparam>
|
||||
public abstract class Protocol<TMessageType> {
|
||||
|
||||
//Header size is always 4
|
||||
const int HEADER_SIZE = 4;
|
||||
private const int HeaderSize = 4;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the message and checks with the header if the message is valid or not
|
||||
@@ -26,7 +28,7 @@ namespace Server_Dashboard_Socket {
|
||||
//Validates the length
|
||||
AssertValidMessageLength(bodyLength);
|
||||
//Returns the body message type
|
||||
return await Readbody(networkStream, bodyLength).ConfigureAwait(false);
|
||||
return await ReadBody(networkStream, bodyLength).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -50,8 +52,8 @@ namespace Server_Dashboard_Socket {
|
||||
/// </summary>
|
||||
/// <param name="networkStream">A network stream</param>
|
||||
/// <returns>Header as Integer</returns>
|
||||
async Task<int> ReadHeader(NetworkStream networkStream) {
|
||||
byte[] headerBytes = await ReadAsync(networkStream, HEADER_SIZE).ConfigureAwait(false);
|
||||
private async Task<int> ReadHeader(NetworkStream networkStream) {
|
||||
byte[] headerBytes = await ReadAsync(networkStream, HeaderSize).ConfigureAwait(false);
|
||||
return IPAddress.HostToNetworkOrder(BitConverter.ToInt32(headerBytes));
|
||||
}
|
||||
|
||||
@@ -61,7 +63,7 @@ namespace Server_Dashboard_Socket {
|
||||
/// <param name="networkStream">A network stream</param>
|
||||
/// <param name="bodyLength">Length of the body</param>
|
||||
/// <returns>Decoded body</returns>
|
||||
async Task<TMessageType> Readbody(NetworkStream networkStream, int bodyLength) {
|
||||
private async Task<TMessageType> ReadBody(NetworkStream networkStream, int bodyLength) {
|
||||
//Reads the bytes from the stream into an array
|
||||
byte[] bodyBytes = await ReadAsync(networkStream, bodyLength).ConfigureAwait(false);
|
||||
//Decodes it and returns the string
|
||||
@@ -74,13 +76,13 @@ namespace Server_Dashboard_Socket {
|
||||
/// <param name="networkStream">A network stream</param>
|
||||
/// <param name="bytesToRead">how many bytes there are to read</param>
|
||||
/// <returns>Every byte from the Stream</returns>
|
||||
async Task<byte[]> ReadAsync(NetworkStream networkStream, int bytesToRead) {
|
||||
private async Task<byte[]> ReadAsync(NetworkStream networkStream, int bytesToRead) {
|
||||
//new buffer that is as big as the content(watch out for buffer overflows)
|
||||
byte[] buffer = new byte[bytesToRead];
|
||||
//keep acount of the bytes that are already read
|
||||
//keep account of the bytes that are already read
|
||||
int bytesRead = 0;
|
||||
//White we still have something to read
|
||||
while(bytesRead < bytesToRead){
|
||||
while (bytesRead < bytesToRead) {
|
||||
//Read it from the stream
|
||||
var bytesReceived = await networkStream.ReadAsync(buffer, bytesRead, (bytesToRead - bytesRead)).ConfigureAwait(false);
|
||||
//If it happens to be 0 close the socket
|
||||
@@ -94,7 +96,7 @@ namespace Server_Dashboard_Socket {
|
||||
/// <summary>
|
||||
/// Encode the message from human readable to bytes for the stream
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The message as anything e.g. object or strng</typeparam>
|
||||
/// <typeparam name="T">The message as anything e.g. object or string</typeparam>
|
||||
/// <param name="message">The message to be send</param>
|
||||
/// <returns>The Header and Body as bytes[]</returns>
|
||||
protected (byte[] header, byte[] body) Encode<T>(T message) {
|
||||
@@ -121,12 +123,13 @@ namespace Server_Dashboard_Socket {
|
||||
if (messageLength < 1)
|
||||
throw new ArgumentOutOfRangeException("Invalid message length");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encode the message so it can be send via the network stream
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Message type e.g. object or string</typeparam>
|
||||
/// <param name="message">The message to be send</param>
|
||||
/// <returns></returns>
|
||||
protected abstract byte[] EncodeBody<T> (T message);
|
||||
protected abstract byte[] EncodeBody<T>(T message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user