/// <summary> /// Close the connection with the client /// Call 'connectionClosed' of your application that extends the DKService when the connection is closed successfully /// </summary> public void Close() { // one thread per time, prevent two or more thread call Close() in same time and generate duplicate notifications lock (ThreadLocker.sync("Client::Close")) { if (!Connected) { return; } try { // sets connected false Connected = false; // remove the client from signal ClientSignal.Remove(Id); // close the socket _client.Close(); } catch (Exception ex) { Log.Write("Error when disconnect client " + ex.Message + " - " + ex.StackTrace, LogLevel.ERROR); } finally { // if the socket have a type and defined, notification the application is desconnected if (socketLayer != SocketLayer.undefined) { Application.send(ApplicationSend.connectionClosed, new object[] { this }); socketLayer = SocketLayer.undefined; } } } }
/// <summary> /// Sends data to client, if exists peding data in sending status /// the actual data param enter in the queue /// </summary> /// <param name="packet">array of bytes to send</param> public void Send(byte[] packet) { if (packet.Length == 0 || packet == null) { return; } // one thread per time lock (ThreadLocker.sync("DataTransport::Send")) { // test the packet is a fix thread concorrent hack work if (packet.Length != 1 || packet[0] != 9) { // not packet thread concorrent // add the data in the queue queueData.Enqueue(packet); } // if asynSending is false and have data to send if (!asynSending && queueData.Count > 0) { // we set to true to say that we are working with sending asynSending = true; // start sending the packets BeginSend(queueData.Dequeue()); } } }