-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7d8ff7
commit 4e072d7
Showing
3 changed files
with
103 additions
and
150 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// SOLUTION : KFlearning | ||
// PROJECT : KFlearning.Core | ||
// FILENAME : RemoteServer.cs | ||
// AUTHOR : Fahmi Noor Fiqri, Kodesiana.com | ||
// WEBSITE : https://kodesiana.com | ||
// REPO : https://github.com/Kodesiana or https://github.com/fahminlb33 | ||
// | ||
// This file is part of KFlearning, see LICENSE. | ||
// See this code in repository URL above! | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
|
||
namespace KFlearning.Core.Services | ||
{ | ||
public interface IRemoteShutdownServer : IDisposable | ||
{ | ||
void Listen(); | ||
void SendShutdown(string cluster); | ||
|
||
event EventHandler<ShutdownRequestedEventArgs> ShutdownRequested; | ||
} | ||
|
||
public class RemoteShutdownServer : IRemoteShutdownServer | ||
{ | ||
public const int ServicePort = 2021; | ||
private const string MessageShutdown = "SDWN"; | ||
|
||
private static readonly IPEndPoint ServiceEndpoint = new IPEndPoint(IPAddress.Any, ServicePort); | ||
|
||
private readonly UdpClient _socket = new UdpClient(); | ||
private MemoryStream _uploadStream; | ||
|
||
public event EventHandler<ShutdownRequestedEventArgs> ShutdownRequested; | ||
public event EventHandler<ShutdownRequestedEventArgs> FileBroadcast; | ||
|
||
public RemoteShutdownServer() | ||
{ | ||
_socket.Client.Bind(ServiceEndpoint); | ||
} | ||
|
||
#region Public Methods | ||
|
||
public void Listen() | ||
{ | ||
_socket.BeginReceive(ReceiveCallback, null); | ||
} | ||
|
||
public void SendShutdown(string cluster) | ||
{ | ||
Send(MessageShutdown, cluster); | ||
} | ||
|
||
#endregion | ||
|
||
#region Private Methods (Callbacks) | ||
|
||
private void Send(string message, string body) | ||
{ | ||
var data = Encoding.ASCII.GetBytes($"{message}|{body}"); | ||
_socket.BeginSend(data, data.Length, ServiceEndpoint, SendCallback, null); | ||
} | ||
|
||
private void SendCallback(IAsyncResult ar) | ||
{ | ||
_socket.EndSend(ar); | ||
} | ||
|
||
private void ReceiveCallback(IAsyncResult ar) | ||
{ | ||
var epFrom = new IPEndPoint(IPAddress.Any, ServicePort); | ||
var bytes = _socket.EndReceive(ar, ref epFrom); | ||
var message = Encoding.ASCII.GetString(bytes); | ||
ProcessMessage(message, epFrom.Address); | ||
|
||
_socket.BeginReceive(ReceiveCallback, null); | ||
} | ||
|
||
private void ProcessMessage(string message, IPAddress server) | ||
{ | ||
if (!message.StartsWith(MessageShutdown)) return; | ||
var cluster = message.Split('|')[1]; | ||
ShutdownRequested?.Invoke(this, new ShutdownRequestedEventArgs { Address = server, Clusster = cluster }); | ||
} | ||
|
||
#endregion | ||
|
||
#region IDisposable | ||
|
||
public void Dispose() | ||
{ | ||
((IDisposable)_socket)?.Dispose(); | ||
((IDisposable)_uploadStream)?.Dispose(); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters