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); }
public Boolean SendRequest(Byte[] Buffer) { if (ClientSocket == null) { return(false); } if (Connected == false) { return(false); } try { //A first chance exception of type 'System.InvalidOperationException' occurred in System.dll //{"Cannot block a call on this socket while an earlier asynchronous call is in progress."} ClientSocket.Send(Buffer, SocketFlags.None); } catch (SocketException e) { ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); args.ConnectionID = m_ID; args.ErrorCode = e.ErrorCode; args.ErrorMessage = e.Message; OnError(args); Disconnect(); return(false); } catch (Exception) { Disconnect(); return(false); } return(true); }
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; } }
private void ConnectCallback(IAsyncResult ar) { StateObject state = (StateObject)ar.AsyncState; Socket client = state.WorkSocket; m_Connecting = false; //ensure current client connection is the same as //the one contained in the callback object if (client != ClientSocket) { return; } try { client.EndConnect(ar); } 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; } if (client != ClientSocket) { return; } if (client.Connected == true) { ConnectionStatusChangeEventArgs StatusArgs = new ConnectionStatusChangeEventArgs(); StatusArgs.ConnectionID = m_ID; StatusArgs.State = ConnectionStatus.CONNECTED; OnStatusChange(StatusArgs); if (client != ClientSocket) { Disconnect(); return; } if (ClientSocket == null) { return; } if (client.Connected == false) { Disconnect(); return; } 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); return; } catch (Exception) { return; } } }
public Boolean Disconnect() { m_Connecting = false; //if socket is created if (ClientSocket != null) { //if socket is connected, disconnect if (ClientSocket.Connected == true) { try { //Turn off Sending ClientSocket.Shutdown(SocketShutdown.Send); //ClientSocket.Disconnect(True) //wait for socket to be empty Byte[] b = new Byte[256]; while (ClientSocket.Receive(b, 256, SocketFlags.None) > 0) { System.Threading.Thread.Sleep(1); } //Shutdown the Recieve Socket! if (ClientSocket != null) { ClientSocket.Shutdown(SocketShutdown.Receive); } } catch (SocketException e) { ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); args.ConnectionID = m_ID; args.ErrorCode = e.ErrorCode; args.ErrorMessage = e.Message; OnError(args); } catch (Exception) { /* We are trying to catch the NullReferenceException * but we still want to trigger the ConnectionStatusChangeEventArgs * so allow the error to fall through */ } if (ClientSocket != null) { try { ClientSocket.Disconnect(false); } catch (SocketException e) { ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); args.ConnectionID = m_ID; args.ErrorCode = e.ErrorCode; args.ErrorMessage = e.Message; OnError(args); } catch (Exception) { /* We are trying to catch the NullReferenceException * but we still want to trigger the ConnectionStatusChangeEventArgs * so allow the error to fall through */ } } } //close socket object if (ClientSocket != null) { ClientSocket.Close(1); ClientSocket = null; } ConnectionStatusChangeEventArgs StatusArgs = new ConnectionStatusChangeEventArgs(); StatusArgs.ConnectionID = m_ID; StatusArgs.State = ConnectionStatus.DISCONNECTED; OnStatusChange(StatusArgs); } Address = ""; Port = 0; return(true); }
public Boolean Connect(int Port, string Address) { if (Address == null) { return(false); } if ((Port == 0) || (Address.Trim() == "")) { return(false); } //already connected if (Connected) { return(false); } //already in async connect if (m_Connecting == true) { return(true); } //trying to connect when already connected, return true //if (ClientSocket != null) // if (ClientSocket.Connected == true) // return true; this.Address = Address; this.Port = Port; //create new connection ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); StateObject state = new StateObject(); state.WorkSocket = ClientSocket; m_Connecting = true; ConnectionStatusChangeEventArgs StatusArgs = new ConnectionStatusChangeEventArgs(); StatusArgs.ConnectionID = m_ID; StatusArgs.State = ConnectionStatus.CONNECTING; OnStatusChange(StatusArgs); try { ClientSocket.BeginConnect(Address, Port, new AsyncCallback(ConnectCallback), state); } catch (SocketException e) { /* Generate Event */ ConnectionErrorEventArgs args = new ConnectionErrorEventArgs(); args.ConnectionID = m_ID; args.ErrorCode = e.ErrorCode; args.ErrorMessage = e.Message; OnError(args); Disconnect(); return(false); } catch (Exception) { Disconnect(); return(false); } return(true); }