private void StartListening() { try { IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(connectionParameter.Address), connectionParameter.TcpPort); listener = new TcpListener(endPoint); listener.Start(); listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), this); } catch (SocketException se) { Trace.TraceError("SocketException in StartListening(). Stack trace:\n{0}\n", se.StackTrace); ClientErrorEventArgs args = new ClientErrorEventArgs() { ErrorCode = ClientErrorEventArgs.CONNECTION_ERROR }; socketBroadcast.Close(); workerBroadcast.CancelAsync(); connection.ClientErrorMessageReceived(args); connection.CloseConnectionMessageRecived(); } }
void controlConnectionListener_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { //Interrompo gli altri thread 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 ACTIVE_RESULT: //cambio lo stato in connesso connection.State = NextState(State.ACTIVE); connection.ActiveClientMessageRecived(); break; case CLOSE_RESULT: //passo allo stato disconnected innerState = InnerState.STOP_BY_CONTROLLER; connection.State = NextState(State.DISCONNECTED); connection.DisconnectClientMessageRecived(); if (tcpControlConnection != null) { stream.Close(); tcpControlConnection.Close(); } break; case EXTERNAL_CLOSE_RESULT: //Questo caso non dovrebbe accadere 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; } } }