private byte[] getReceivedPacket(byte[] buffer, int size) { lock (ThreadLocker.sync("ClientListen::getReceivedPacket")) { // security of return if the size received is zero if (size == 0) { return(new byte[1]); } byte[] pack = new byte[size]; Array.Copy(buffer, pack, size); return(pack); } }
/// <summary> /// Update the time of this client to identify is alive /// </summary> /// <param name="client">DarkKnight.Network.Client object</param> public static void udpate(Client client) { // we garanted one thread per time lock (ThreadLocker.sync("ClientSignal::update")) { if (!_clientSignal.ContainsKey(client.Id)) { _clientSignal[client.Id] = new ClientSignal(client); } } try { _clientSignal[client.Id].time = DateTime.Now.AddMilliseconds(2400); } catch { // the client id not found in Dictionary, is okay } }
/// <summary> /// This method is called when new client connecting to socket /// </summary> /// <param name="Result"></param> private void acceptConnection(IAsyncResult Result) { // restore the socket object Socket server = (Socket)Result.AsyncState; try { // get socket of client connected Socket client = server.EndAccept(Result); // one thread per time lock (ThreadLocker.sync("ServerListen::acceptConnection")) { nextClientId++; } if (UnauthenticatedTable.get(((IPEndPoint)client.RemoteEndPoint).Address.ToString()) >= ServerController.config.MaxUnauthenticatedAccept) { client.Close(); } else { // we add the new connected client to the listener channel new ClientListen(client, nextClientId); } } catch (Exception ex) { DarkKnight.Utils.Log.Write("A new client connected generate a error and not accepted: \n" + ex.Message + " - " + ex.StackTrace, Utils.LogLevel.ERROR); } finally { // release to accept more connections asynchronous server.BeginAccept(new AsyncCallback(acceptConnection), server); } }