Skip to content

Commit

Permalink
added code highlighting.
Browse files Browse the repository at this point in the history
  • Loading branch information
b1ngster committed Sep 1, 2023
1 parent ce32f03 commit 3e1ec22
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
54 changes: 54 additions & 0 deletions gists/UDPMessenger/UDPReceiver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class UDPReceiver
{
private UdpClient listener;
private IPEndPoint senderEndPoint;
private int expectedSequenceNumber = 0;

public UDPReceiver(int listenPort)
{
listener = new UdpClient(listenPort);
senderEndPoint = new IPEndPoint(IPAddress.Any, 0);
}

public void Receive()
{
while (true)
{
byte[] packet = ReceivePacket();
int receivedSequenceNumber = BitConverter.ToInt32(packet, 0);
byte[] data = new byte[packet.Length - sizeof(int)];
Buffer.BlockCopy(packet, sizeof(int), data, 0, data.Length);

if (receivedSequenceNumber == expectedSequenceNumber)
{
SendAck(expectedSequenceNumber);
Console.WriteLine("Received message: " + Encoding.UTF8.GetString(data));
expectedSequenceNumber++;
}
else if (receivedSequenceNumber < expectedSequenceNumber)
{
SendAck(receivedSequenceNumber); // Acknowledge the duplicate
}
else
{
// Out-of-order packet received, don't send ACK
}
}
}

private byte[] ReceivePacket()
{
return listener.Receive(ref senderEndPoint);
}

private void SendAck(int ackSequenceNumber)
{
byte[] ackData = BitConverter.GetBytes(ackSequenceNumber);
listener.Send(ackData, ackData.Length, senderEndPoint);
}
}
60 changes: 60 additions & 0 deletions gists/UDPMessenger/UDPSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class UDPSender
{
private UdpClient client;
private IPEndPoint serverEndPoint;
private int sequenceNumber = 0;

public UDPSender(string serverIp, int serverPort)
{
client = new UdpClient();
serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIp), serverPort);
}

public void Send(string message)
{
byte[] data = Encoding.UTF8.GetBytes(message);
byte[] packet = CreatePacket(data);

while (true)
{
SendPacket(packet);

// Wait for acknowledgment (ACK)
byte[] ackData = ReceiveAck();
int ackSequenceNumber = BitConverter.ToInt32(ackData, 0);

if (ackSequenceNumber == sequenceNumber)
{
Console.WriteLine("Message sent and acknowledged: " + message);
sequenceNumber++;
break;
}
}
}

private byte[] CreatePacket(byte[] data)
{
byte[] sequenceBytes = BitConverter.GetBytes(sequenceNumber);
byte[] packet = new byte[sequenceBytes.Length + data.Length];

Buffer.BlockCopy(sequenceBytes, 0, packet, 0, sequenceBytes.Length);
Buffer.BlockCopy(data, 0, packet, sequenceBytes.Length, data.Length);

return packet;
}

private void SendPacket(byte[] packet)
{
client.Send(packet, packet.Length, serverEndPoint);
}

private byte[] ReceiveAck()
{
return client.Receive(ref serverEndPoint);
}
}
53 changes: 52 additions & 1 deletion main.tex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
\usepackage{hyperref}
\usepackage{listings}
\usepackage{blindtext}

\newcommand{\click}[2]{\href{http://#1}{\colorlet{temp}{.}\color{blue}{\underline{\color{temp}#2}}\color{temp}}}
\newcommand{\clicks}[2]{\href{https://#1}{\colorlet{temp}{.}\color{blue}{\underline{\color{temp}#2}}\color{temp}}}

\newcommand{\cmdname}[1]{\texttt{#1}}
\newcommand\footurl[1]{\footnote{\url{#1}}}
\newcommand\urllink[2]{#1\footurl{#2}}
\newcommand\foothref[3]{#1\footnote{\href{#2}{#3}}}
\usepackage{upquote}

\hypersetup{
colorlinks=true,
linkcolor=blue,
Expand All @@ -23,7 +28,39 @@
pdftitle={Overleaf Example},
pdfpagemode=FullScreen,
}

\usepackage{xcolor}
\usepackage{caption}


%\DeclareCaptionFormat{mycaption}{#1}

% Set the custom caption format for all listings
%\captionsetup[lstlisting]{format=mycaption}
\renewcommand{\lstlistingname}{}% Listing -> Algorithm
\renewcommand{\lstlistlistingname}{List of \lstlistingname s}% List of Listings -> List of Algorithms

\lstdefinestyle{csharpstyle}{
language={[Sharp]C},
backgroundcolor=\color{black}, % Background color of the code
basicstyle=\color{white}\ttfamily,
keywordstyle=\color{blue},
commentstyle=\color{green},
stringstyle=\color{orange},
numberstyle=\tiny\color{white}, % Font color of line numbers
numbers=left,
breaklines=true,
showstringspaces=false,
tabsize=4,
frame=leftline,
framerule=2pt,
captionpos=t, % Position of the caption (bottom)
numbersep=10pt, % Space between line numbers and code
framexleftmargin=10pt, % Width of the line number background
rulecolor=\color{black}, % Color of the line separating code and line numbers
}
\lstset{
numbers=none, % Disable line numbering globally
}
\title{Blog}

\author{B1ngster}
Expand All @@ -42,6 +79,8 @@
\section*{31 August 2023 - \\ Acknowledgement to Security and Accessibility}
\addcontentsline{toc}{section}{\protect\numberline{} 31 August 2023 - \\ Acknowledgement to Security and Accessibility}



I have recently purchased a security certificate for bingster, so I can now use HTTP/3, which has recently been added to the stable version of NGINX. HTTP/1.1 and HTTP/2 use TCP (Transmission Control Protocol). QUIC QUIC (Quick UDP Internet Connections) is built on top of UDP (User Datagram Protocol) and is designed to reduce latency, improve connection migration, and enhance security, particularly for web applications. HTTP/1.1 and HTTP/2 use TCP (Transmission Control Protocol), whereas QUIC is built on UDP (User Datagram Protocol).

UDP is often used for video delivery, but there are other protocols, such as HTTP Live Streaming (HLS), which utilises adaptive streaming, or Real-Time Transport Protocol (RTP). With video content and audio messages, losing packets of data is not a problem as we can quickly move on, but there are messages where package loss is unacceptable. UDP is a faster protocol cos it does not maintain a connection.
Expand All @@ -58,6 +97,18 @@ \section*{31 August 2023 - \\ Acknowledgement to Security and Accessibility}

A sample code which uses acknowledgement to ensure message developed C\# is provided in this these lists: \href{https://gist.github.com/b1ngster/50f5814d010b6d2cd79eec733a0d2bf1}{UPDSender} and \href{https://gist.github.com/b1ngster/50f5814d010b6d2cd79eec733a0d2bf1}{UPDReceiver}.


\lstinputlisting[style=csharpstyle, title={ UDPSender }]{gists/UDPMessenger/UDPSender.cs}

\lstinputlisting[style=csharpstyle, title={ UDPReceiver }]{gists/UDPMessenger/UDPReceiver.cs}








In an architecture that uses multiple servers to communicate using other protocols, the TCP can help the network become more resilient, as there can often be problems with the network and ports. This recently happened to me when my server's memory became exhausted, only connecting via serial port. Therefore, developing a class that encapsulates messaging protocols using the strategy pattern should be considered. Indeed, Chaos Engineering is used by Netflix to ensure it is resilient, so switching off different services could also be implemented.

But it's not all about speed, but it helps to give users a great user experience. Well, if it was, I wouldn't be using Laravel... - It has Google's Lighthouse integrated, which enables Testing for accessible issues that can be difficult for designers to identify. Indeed, several other tools provide heuristic and accessible evaluations to the I have started implementing these classes.
Expand Down

0 comments on commit 3e1ec22

Please sign in to comment.