void controlConnectionListener_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { //Interrompo gli altri thread StopMouseListening(); StopClipboardListening(); ClientErrorEventArgs args = null; switch (innerState) { case InnerState.STOP_BY_NETWORK_ERROR: connection.State = null; args = new ClientErrorEventArgs() { ErrorCode = ClientErrorEventArgs.NETWORK_ERROR }; connection.ClientErrorMessageReceived(args); if (tcpControlConnection != null) { stream.Close(); tcpControlConnection.Close(); } connection.CloseConnectionMessageRecived(); return; case InnerState.STOP_BY_ERROR: connection.State = null; args = new ClientErrorEventArgs() { ErrorCode = ClientErrorEventArgs.CLIENT_ERROR }; connection.ClientErrorMessageReceived(args); if (tcpControlConnection != null) { stream.Close(); tcpControlConnection.Close(); } connection.CloseConnectionMessageRecived(); return; case InnerState.STOP_BY_USER: connection.State = null; if (tcpControlConnection != null) { stream.Close(); tcpControlConnection.Close(); } connection.CloseConnectionMessageRecived(); return; } if (e.Error == null && e.Result != null) { string result = (string)e.Result; switch (result) { case DEACTIVE_RESULT: //cambio lo stato in connesso innerState = InnerState.STOP_BY_CONTROLLER; connection.State = NextState(State.CONNECTED); connection.ConnectClientMessageRecived(null, 0, 0); forceButtonUp(); break; case CLOSE_RESULT: //passo allo stato disconnected innerState = InnerState.STOP_BY_CONTROLLER; connection.State = NextState(State.DISCONNECTED); connection.DisconnectClientMessageRecived(); forceButtonUp(); if (tcpControlConnection != null) { stream.Close(); tcpControlConnection.Close(); } break; case EXTERNAL_CLOSE_RESULT: try { if (stream != null) { WriteMessage(stream, TipoComandoBytes.CLOSE_CONNECTION, 0, COMMAND_LENGHT); } } catch (Exception ex) { Trace.TraceError("Exception in controlConnectionListener_RunWorkerComplited(). Stack trace:\n{0}\n", ex.StackTrace); if (!(ex is NetworkException || ex is IOException || ex is SocketException)) { throw; } } finally { if (tcpControlConnection != null) { stream.Close(); tcpControlConnection.Close(); } connection.CloseConnectionMessageRecived(); } break; case NETWORK_ERROR_RESULT: innerState = InnerState.STOP_BY_NETWORK_ERROR; connection.State = null; args = new ClientErrorEventArgs() { ErrorCode = ClientErrorEventArgs.NETWORK_ERROR }; connection.ClientErrorMessageReceived(args); if (tcpControlConnection != null) { stream.Close(); tcpControlConnection.Close(); } connection.CloseConnectionMessageRecived(); break; case CLIENT_ERROR_RESULT: innerState = InnerState.STOP_BY_ERROR; connection.State = null; args = new ClientErrorEventArgs() { ErrorCode = ClientErrorEventArgs.CLIENT_ERROR }; connection.ClientErrorMessageReceived(args); if (tcpControlConnection != null) { stream.Close(); tcpControlConnection.Close(); } connection.CloseConnectionMessageRecived(); break; case GENERIC_ERROR_RESULT: innerState = InnerState.STOP_BY_ERROR; connection.State = null; args = new ClientErrorEventArgs() { ErrorCode = ClientErrorEventArgs.GENERIC_ERROR }; connection.ClientErrorMessageReceived(args); if (tcpControlConnection != null) { stream.Close(); tcpControlConnection.Close(); } connection.CloseConnectionMessageRecived(); break; } } }
private void controlConnectionListener_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error == null && e.Result != null) { ClientErrorEventArgs args = null; string result = (string)e.Result; if (result.Equals(CONNECTED_RESULT)) { //Passo allo stato connesso IConnectionState state = NextState(State.CONNECTED); connection.State = state; //chiudo il socket del broadcast xk deve essere socketBroadcast.Close(); workerBroadcast.CancelAsync(); connection.ConnectClientMessageRecived(((IPEndPoint)tcpControlConnection.Client.RemoteEndPoint).Address.ToString(), width, height); Trace.TraceInformation("Connesso."); } else { //Altri casi //Chiudo la connessione tcpControlConnection.GetStream().Close(); tcpControlConnection.Close(); tcpControlConnection = null; switch (result) { case STOPPED_RESULT: connection.CloseConnectionMessageRecived(); break; case NETWORK_ERROR_RESULT: args = new ClientErrorEventArgs() { ErrorCode = ClientErrorEventArgs.NETWORK_ERROR }; connection.ClientErrorMessageReceived(args); connection.CloseConnectionMessageRecived(); break; case CLIENT_ERROR_RESULT: args = new ClientErrorEventArgs() { ErrorCode = ClientErrorEventArgs.CLIENT_ERROR }; connection.ClientErrorMessageReceived(args); connection.CloseConnectionMessageRecived(); break; case GENERIC_ERROR_RESULT: args = new ClientErrorEventArgs() { ErrorCode = ClientErrorEventArgs.GENERIC_ERROR }; connection.ClientErrorMessageReceived(args); connection.CloseConnectionMessageRecived(); break; case LOGIN_ERROR_RESULT: listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), this); return; } } listener.Stop(); } }