/// <summary> /// Invoked when a client attempts to connect to this server. /// </summary> /// <param name="asyn">Asyncronous state.</param> private void OnClientConnect(IAsyncResult asyn) { if (_socket == null || (_socket.Connected == false && _isListening == false && _isConnected == true)) return; lock (_clientList) { try { Socket clientSocket = _socket.EndAccept(asyn); NetworkConnection clientConnection = new NetworkConnection(clientSocket); // To many clients? if (_clientList.Count >= NetworkManager.MaximumClients) { DebugLogger.WriteLog("Denied connection from " + clientSocket.RemoteEndPoint.ToString() + ", maximum clients already connected."); NetworkPacket banPacket = new NetworkPacket(); banPacket.ID = "CONNECTION_RESULT".GetHashCode(); banPacket[0] = (byte)2; banPacket[1] = (int)0; clientConnection.SendPacket(banPacket); clientSocket.Close(); return; } // Is this IP banned? bool banned = false; string currentIP = clientSocket.RemoteEndPoint.ToString(); if (currentIP.IndexOf(":") >= 0) currentIP = currentIP.Substring(0, currentIP.IndexOf(":")); foreach (NetworkBan ban in NetworkManager.BansList) { if (ban.IsIPBanned(currentIP.ToLower())) { banned = true; break; } } if (banned == true) { DebugLogger.WriteLog("Denied connection from " + clientSocket.RemoteEndPoint.ToString() + ", IP address is banned."); NetworkPacket banPacket = new NetworkPacket(); banPacket.ID = "CONNECTION_RESULT".GetHashCode(); banPacket[0] = (byte)1; banPacket[1] = (int)0; clientConnection.SendPacket(banPacket); clientSocket.Close(); return; } // Work out a new unique ID for this client. int id = 0; while (true) { bool found = false; foreach (NetworkClient subclient in _clientList) if (subclient.ID == id) found = true; if (found == false) break; id++; } // Create a new client object to store details on this connection. NetworkClient client = new NetworkClient(); client.Connection = clientConnection; client.ID = id; _clientList.Add(client); // Ackowledge its existance. NetworkPacket helloPacket = new NetworkPacket(); helloPacket.ID = "CONNECTION_RESULT".GetHashCode(); helloPacket[0] = (byte)0; helloPacket[1] = id; client.Connection.SendPacket(helloPacket); // Log this connection. DebugLogger.WriteLog("Accepted connection from " + clientSocket.RemoteEndPoint.ToString() + ", connection assigned id " + id); // Throw an event. if (ClientConnected != null) ClientConnected(this, client); // Go back to waiting for client connections. _socket.BeginAccept(new AsyncCallback(OnClientConnect), null); } catch (Exception) { return; } } }
public NetworkClientScriptObject(NetworkClient client) { _nativeObject = client; }
/// <summary> /// Invoked when a client disconnected. /// </summary> /// <param name="sender">Connection that client disconnected from.</param> /// <param name="client">Client that disconnected.</param> private static void OnClientDisconnected(object sender, NetworkClient client) { if (ClientDisconnected != null) ClientDisconnected(sender, client); }
/// <summary> /// Invoked when a client disconnects from the network manager. /// </summary> /// <param name="sender">Connection that client disconnected from.</param> /// <param name="client">Client that disconnected.</param> void ClientDisconnected(object sender, NetworkClient client) { foreach (ScriptProcess process in VirtualMachine.GlobalInstance.Processes) { NetworkClientScriptObject clientSO = new NetworkClientScriptObject(client); NetworkConnectionScriptObject connectionSO = new NetworkConnectionScriptObject(sender as NetworkConnection); foreach (ScriptThread thread in process.Threads) { thread.PassParameter(connectionSO); thread.PassParameter(clientSO); thread.InvokeFunction("OnClientDisconnected", true, false); } } }