void m_Connection_Error(object sender, ConnectionErrorEventArgs e) { var args = new ClientInterfaceResponseStringEventArgs(); args.Text = e.ErrorMessage; OnResponseLog(args); }
protected virtual void OnError(ConnectionErrorEventArgs e) { var handler = Error; if (handler != null) { handler(this, e); } }
void m_Connection_Error(object sender, ConnectionErrorEventArgs e) { //Log.WriteLog(Log.LogLevelType.Comm, "CommandInterface::Error(): " + // "[" + m_Connection.Address + ":" + m_Connection.Port + "] " + // e.ErrorMessage); var ErrorEventArgs = new CommandInterfaceResponseErrorEventArgs(); ErrorEventArgs.ErrorText = e.ErrorMessage; OnResponseError(ErrorEventArgs); }
private void ReceiveCallback(IAsyncResult ar) { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket client = state.WorkSocket; SocketError error; int bytesRead; //ensure current client connection is the same as //the one contained in the callback object if (client != ClientSocket) { return; } if (ClientSocket == null) { return; } if (Connected == false) { return; } System.Threading.Monitor.Enter(countLock); //Read data from the remote device. try { bytesRead = client.EndReceive(ar, out error); } catch (SocketException e) { System.Threading.Monitor.Exit(countLock); ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); args.ConnectionID = m_ID; args.ErrorCode = e.ErrorCode; args.ErrorMessage = e.Message; OnError(args); Disconnect(); return; } catch (Exception) { Disconnect(); return; } if ((bytesRead == 0) || (error != SocketError.Success)) { System.Threading.Monitor.Exit(countLock); if (error != SocketError.Success) { ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); args.ConnectionID = m_ID; args.ErrorCode = (int)error; args.ErrorMessage = "Disconnect: Bytes = " + bytesRead.ToString() + ", Error = " + error.ToString(); OnError(args); } Disconnect(); return; } Byte[] buffer = null; if (bytesRead > 0) { buffer = new Byte[bytesRead]; Array.Copy(state.buffer, buffer, bytesRead); } System.Threading.Monitor.Exit(countLock); if (bytesRead > 0) { ConnectionReceiveDataEventArgs RecvArgs = new ConnectionReceiveDataEventArgs(); RecvArgs.ConnectionID = m_ID; RecvArgs.Buffer = buffer; RecvArgs.Length = bytesRead; OnReceiveData(RecvArgs); } //invoke could trigger disconnect (or disconnect via send()), //which means our socket could be invalid if (client != ClientSocket) { return; } if (ClientSocket == null) { return; } if (Connected == false) { return; } //Get the rest of the data. try { client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (SocketException e) { // ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); // args.ConnectionID = m_ID; // args.ErrorCode = e.ErrorCode; // args.ErrorMessage = e.Message; // OnError(args); Disconnect(); return; } catch (Exception) { Disconnect(); return; } }