1
0
Fork 0
This repository has been archived on 2019-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Galactic_Colors_Control/Galactic Colors Control/Program.cs

312 lines
9.8 KiB
C#
Raw Normal View History

2016-10-30 14:15:27 +00:00
using Galactic_Colors_Control_Common;
using System;
2016-10-17 11:09:45 +00:00
using System.Collections.Generic;
2016-10-09 12:22:40 +00:00
using System.IO;
2016-10-09 12:18:14 +00:00
using System.Linq;
2016-10-09 12:22:40 +00:00
using System.Net;
using System.Net.NetworkInformation;
2016-10-09 12:22:40 +00:00
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
2016-10-09 12:18:14 +00:00
using System.Text;
2016-10-09 12:22:40 +00:00
using System.Threading;
2016-10-09 12:18:14 +00:00
2016-10-17 11:09:45 +00:00
namespace Galactic_Colors_Control
2016-10-09 12:18:14 +00:00
{
2016-10-17 11:09:45 +00:00
public class Client
2016-10-09 12:18:14 +00:00
{
2016-10-17 11:09:45 +00:00
private Socket ClientSocket = new Socket
2016-10-09 12:22:40 +00:00
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2016-10-17 11:09:45 +00:00
public int PORT = 0;
private int _errorCount = 0;
private bool _run = true;
public string IP = null;
public bool isRunning { get { return _run; } }
2016-10-09 12:22:40 +00:00
2016-10-17 11:09:45 +00:00
public List<string> Output = new List<string>();
private Thread RecieveThread;
public void ResetHost()
2016-10-09 12:22:40 +00:00
{
2016-10-17 11:09:45 +00:00
IP = null;
PORT = 0;
2016-10-09 12:22:40 +00:00
}
2016-10-17 11:09:45 +00:00
public string ValidateHost(string text)
2016-10-09 12:22:40 +00:00
{
2016-10-17 11:09:45 +00:00
if (text == null) { text = ""; }
string[] parts = text.Split(new char[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 0)
{
parts = new string[] { "" };
PORT = 25001;
}
else
2016-10-09 12:22:40 +00:00
{
2016-10-17 11:09:45 +00:00
if (parts.Length > 1)
2016-10-09 12:22:40 +00:00
{
2016-10-17 11:09:45 +00:00
if (!int.TryParse(parts[1], out PORT)) { PORT = 0; }
if (PORT < 0 || PORT > 65535) { PORT = 0; }
2016-10-09 12:22:40 +00:00
}
else
{
2016-10-17 11:09:45 +00:00
PORT = 25001;
2016-10-09 12:22:40 +00:00
}
2016-10-17 11:09:45 +00:00
}
if (PORT != 0)
{
try
2016-10-09 12:22:40 +00:00
{
2016-10-17 11:09:45 +00:00
IPHostEntry ipHostEntry = Dns.GetHostEntry(parts[0]);
IPAddress host = ipHostEntry.AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork);
IP = host.ToString();
return IP + ":" + PORT;
2016-10-09 12:22:40 +00:00
}
2016-10-17 11:09:45 +00:00
catch (Exception e)
2016-10-09 12:22:40 +00:00
{
2016-10-17 11:09:45 +00:00
Output.Add(e.Message);
PORT = 0;
return null;
2016-10-09 12:22:40 +00:00
}
}
2016-10-17 11:09:45 +00:00
else
{
Output.Add("Incorrect port");
return null;
}
}
/// <summary>
/// Set IP and PORT before
/// </summary>
/// <returns>Connection succes</returns>
public bool ConnectHost()
{
int attempts = 0;
2016-10-09 12:22:40 +00:00
while (!ClientSocket.Connected && attempts < 5)
{
try
{
attempts++;
2016-10-17 11:09:45 +00:00
Output.Add("Connection attempt " + attempts);
2016-10-09 12:22:40 +00:00
ClientSocket.Connect(IP, PORT);
}
catch (SocketException)
{
2016-10-17 11:09:45 +00:00
Output.Clear();
2016-10-09 12:22:40 +00:00
}
}
if (attempts < 5)
{
2016-10-17 11:09:45 +00:00
Output.Clear();
Output.Add("Connected to " + IP.ToString());
_run = true;
RecieveThread = new Thread(ReceiveLoop);
RecieveThread.Start();
return true;
2016-10-09 12:22:40 +00:00
}
else
{
2016-10-17 11:09:45 +00:00
Output.Clear();
Output.Add("Can't connected to " + IP.ToString());
ResetSocket();
return false;
2016-10-09 12:22:40 +00:00
}
}
2016-10-17 11:09:45 +00:00
private void ResetSocket()
2016-10-09 12:22:40 +00:00
{
2016-10-17 11:09:45 +00:00
ClientSocket.Close();
ClientSocket = new Socket
(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2016-10-09 12:22:40 +00:00
}
/// <summary>
/// Close socket and exit program.
/// </summary>
2016-10-17 11:09:45 +00:00
public void ExitHost()
2016-10-09 12:22:40 +00:00
{
2016-10-30 14:15:27 +00:00
Send("/exit", Common.dataType.message); // Tell the server we are exiting
2016-10-17 11:09:45 +00:00
_run = false;
RecieveThread.Join();
2016-10-09 12:22:40 +00:00
ClientSocket.Shutdown(SocketShutdown.Both);
ClientSocket.Close();
2016-10-17 11:09:45 +00:00
Output.Add("Bye");
ResetHost();
2016-10-09 12:22:40 +00:00
}
2016-10-17 11:09:45 +00:00
public void SendRequest(string request)
2016-10-09 12:22:40 +00:00
{
switch (request.ToLower())
{
case "/exit":
2016-10-17 11:09:45 +00:00
ExitHost();
break;
case "/ping":
2016-10-17 11:09:45 +00:00
PingHost();
break;
2016-10-20 21:09:48 +00:00
case "/clear":
Output.Add("/clear");
break;
default:
2016-10-30 14:15:27 +00:00
Send(request, Common.dataType.message);
break;
}
}
2016-10-17 11:09:45 +00:00
private void PingHost()
{
Ping p = new Ping();
PingReply r;
r = p.Send(IP);
2016-10-09 12:22:40 +00:00
if (r.Status == IPStatus.Success)
{
2016-10-17 11:09:45 +00:00
Output.Add(r.RoundtripTime.ToString() + " ms.");
}
else
2016-10-09 12:22:40 +00:00
{
2016-10-17 11:09:45 +00:00
Output.Add("Time out");
2016-10-09 12:22:40 +00:00
}
}
2016-10-30 14:15:27 +00:00
private void Send(object data, Common.dataType dtype)
2016-10-09 12:18:14 +00:00
{
2016-10-09 12:22:40 +00:00
byte[] type = new byte[4];
type = BitConverter.GetBytes((int)dtype);
byte[] bytes = null;
switch (dtype)
{
2016-10-30 14:15:27 +00:00
case Common.dataType.message:
2016-10-09 12:22:40 +00:00
bytes = Encoding.ASCII.GetBytes((string)data);
break;
2016-10-30 14:15:27 +00:00
case Common.dataType.data:
2016-10-09 12:22:40 +00:00
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, data);
bytes = ms.ToArray();
}
break;
}
byte[] final = new byte[type.Length + bytes.Length];
type.CopyTo(final, 0);
bytes.CopyTo(final, type.Length);
try
{
ClientSocket.Send(final);
}
catch
{
2016-10-17 11:09:45 +00:00
Output.Add("Can't contact server : " + _errorCount);
2016-10-09 12:22:40 +00:00
_errorCount++;
}
if (_errorCount >= 5)
{
2016-10-17 11:09:45 +00:00
Output.Add("Kick : too_much_errors");
2016-10-09 12:22:40 +00:00
_run = false;
}
}
2016-10-17 11:09:45 +00:00
private void ReceiveLoop()
2016-10-09 12:22:40 +00:00
{
while (_run)
{
var buffer = new byte[2048];
int received = 0;
try
{
received = ClientSocket.Receive(buffer, SocketFlags.None);
}
catch
{
2016-10-17 11:09:45 +00:00
Output.Add("Server timeout");
2016-10-09 12:22:40 +00:00
}
if (received == 0) return;
_errorCount = 0;
var data = new byte[received];
Array.Copy(buffer, data, received);
byte[] type = new byte[4];
type = data.Take(4).ToArray();
type.Reverse();
2016-10-30 14:15:27 +00:00
Common.dataType dtype = (Common.dataType)BitConverter.ToInt32(type, 0);
2016-10-09 12:22:40 +00:00
byte[] bytes = null;
bytes = data.Skip(4).ToArray();
switch (dtype)
{
2016-10-30 14:15:27 +00:00
case Common.dataType.message:
2016-10-09 12:22:40 +00:00
string text = Encoding.ASCII.GetString(bytes);
if (text[0] == '/')
{
text = text.Substring(1);
text = text.ToLower();
string[] array = text.Split(new char[1] { ' ' }, 4, StringSplitOptions.RemoveEmptyEntries);
switch (array[0])
{
2016-10-17 11:09:45 +00:00
case "connected":
Output.Add("Identifiaction succes");
break;
case "allreadytaken":
Output.Add("Username Allready Taken");
break;
2016-10-09 12:22:40 +00:00
case "kick":
if (array.Length > 1)
{
2016-10-17 11:09:45 +00:00
Output.Add("Kick : " + array[1]);
2016-10-09 12:22:40 +00:00
}
else
{
2016-10-17 11:09:45 +00:00
Output.Add("Kick by server");
2016-10-09 12:22:40 +00:00
}
_run = false;
break;
2016-11-06 09:59:05 +00:00
case "party":
if (array[1] == "kick")
{
if (array.Length > 2)
{
Output.Add("Kick from party : " + array[2]);
}
else
{
Output.Add("Kick from party");
}
}
else
{
Output.Add("Unknown action from server");
}
break;
2016-10-09 12:22:40 +00:00
default:
2016-10-17 11:09:45 +00:00
Output.Add("Unknown action from server");
2016-10-09 12:22:40 +00:00
break;
}
}
else
{
2016-10-17 11:09:45 +00:00
Output.Add(text);
2016-10-09 12:22:40 +00:00
}
break;
2016-10-30 14:15:27 +00:00
case Common.dataType.data:
2016-10-09 12:22:40 +00:00
Console.WriteLine("data");
break;
}
Thread.Sleep(200);
}
2016-10-17 11:09:45 +00:00
Output.Add("/*exit*/");
2016-10-09 12:18:14 +00:00
}
}
2016-10-09 12:22:40 +00:00
}