library update

This commit is contained in:
waltem01 2022-07-04 07:42:48 +02:00
parent 0b50826c0b
commit 7f813ed0a5
3 changed files with 65 additions and 24 deletions

4
.gitignore vendored
View File

@ -4,5 +4,5 @@ client/obj/*
server/bin/*
server/obj/*
SocketLib/obj/*
SocketLib/bin/*
obj/*
bin/*

View File

@ -6,34 +6,41 @@ using System.Threading;
using System.Collections;
namespace SocketLib;
public static class Server {
public static class Connection {
public static int port;
public static Socket self;
private static byte[] buffer;
private static string ipAddress;
public static string ipAddress;
private static int listeners = 0;
public static Action<Socket> disconnectMethod;
public static Action<Socket> connectMethod;
public static List<MessageEvent> messageEvents;
public static void Initialize() {
public static void Initialize(int _port=3000) {
// Initialize global variables
port = _port;
buffer = new byte[1024];
messageEvents = new List<MessageEvent>();
disconnectMethod = new Action<Socket>((other)=>{});
// Get computers' ipv4 address
ipAddress = "127.0.0.1";
try {
string hostName = Dns.GetHostName();
IPHostEntry entryList = Dns.GetHostEntry(hostName);
IPAddress ipv4Entry = entryList.AddressList[0];
IPAddress? ipv4Entry = null;
foreach (IPAddress entry in entryList.AddressList)
if (entry.AddressFamily == AddressFamily.InterNetwork) {
ipv4Entry = entry;
break;
}
ipAddress = ipv4Entry.ToString();
} catch (Exception) {}
}
public static void Start() {
public static void ServerStart() {
// Create a new socket server and listen for connections
self = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
self.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), 3000));
self.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), port));
self.Listen(10);
// Accept any client asynchronously
@ -48,6 +55,39 @@ public static class Server {
}
}
public static void ClientConnect(string ipAndPort) {
// Split ipAndPort into the ipAddress and Port
string ipAddr = ipAddress;
int _port = port;
if (ipAndPort != "") {
string[] splits = ipAndPort.Split(':');
ipAddr = splits[0];
_port = int.Parse(splits[1]);
}
// Create a new socket client and connect to a server
self = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
self.Connect(new IPEndPoint(IPAddress.Parse(ipAddr), _port));
self.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), self);
if (connectMethod != null)
connectMethod.Invoke(self);
}
public static void ClientListen() {
// Create a new socket server and listen for connections
Socket own = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
own.Bind(new IPEndPoint(IPAddress.Parse(ipAddress), port));
own.Listen(1);
// Accept any client, stop listening after first client is connected and return client
own.BeginAccept(new AsyncCallback(AcceptCallback), own);
// Wait for and close connection
while (self == null)
Thread.Sleep(250);
own.Close();
}
// End off the message send-callback
private static void SendCallback(IAsyncResult AR) {
Socket other = AR.AsyncState as Socket;
@ -71,12 +111,9 @@ public static class Server {
// Begin listening for more messages
other.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), other);
} catch (Exception) {
// Potentially detected Disconnect
if (other.Connected) {
// Close client connection
disconnectMethod.Invoke(other);
other.Close();
}
// Close client connection
disconnectMethod.Invoke(other);
other.Close();
}
}
@ -86,6 +123,10 @@ public static class Server {
Socket other = own.EndAccept(AR);
listeners--;
other.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), other);
if (self == null)
self = other;
if (connectMethod != null)
connectMethod.Invoke(other);
}
// Handle sending messages

View File

@ -10,22 +10,22 @@ namespace SocketServer {
static void Main(string[] args) {
// Initialize global variables and Server
lobbies = new List<Lobby>();
Server.Initialize();
Server.disconnectMethod = removeFromLobby;
Connection.Initialize();
Connection.disconnectMethod = removeFromLobby;
// Create onEvent methods
Server.messageEvents.Add(new MessageEvent("NewLobby", onNewLobby));
Server.messageEvents.Add(new MessageEvent("JoinLobby", onJoinLobby));
Server.messageEvents.Add(new MessageEvent("Help", onHelp));
Connection.messageEvents.Add(new MessageEvent("NewLobby", onNewLobby));
Connection.messageEvents.Add(new MessageEvent("JoinLobby", onJoinLobby));
Connection.messageEvents.Add(new MessageEvent("Help", onHelp));
// Start the Server itself
Server.Start();
Connection.ServerStart();
}
static void onHelp(Socket other) {
// Respond with a list of commands
string commands = "- Help\n- NewLobby\n- JoinLobby <LobbyCode>";
Server.Send(other, commands);
Connection.Send(other, commands);
}
static void onNewLobby(Socket other) {
@ -39,7 +39,7 @@ namespace SocketServer {
lobbyCode += (char)rng.Next(65, 91);
}
lobbies.Add(new Lobby(other, lobbyCode));
Server.Send(other, $"Created {lobbyCode}");
Connection.Send(other, $"Created {lobbyCode}");
}
static void onJoinLobby(Socket other, string lobbyCode) {
@ -58,7 +58,7 @@ namespace SocketServer {
// Handle feedback for Lobbies
if (response == "")
response = "No such lobby code.";
Server.Send(other, response);
Connection.Send(other, response);
}
static void removeFromLobby(Socket self) {