protected void SendCommand(Socket socket, string command, CommandSentEventHandler callback, bool closeConnection = false) { try { Wait(); var byteData = DongleEncoding.Default.GetBytes(command); socket.BeginSend(byteData, 0, byteData.Length, 0, asyncResult => { int bytesSent; try { bytesSent = socket.EndSend(asyncResult); } catch (Exception exception) { if (OnErrorOcurred != null) { OnErrorOcurred(exception); } if (OnSocketDisconnected != null) { OnSocketDisconnected(socket); } return; } IsSending = false; if (callback != null) { callback(command, bytesSent); } if (closeConnection) { try { socket.Disconnect(false); if (OnSocketDisconnected != null) { OnSocketDisconnected(socket); } } catch (Exception exception) { if (OnErrorOcurred != null) { OnErrorOcurred(exception); } } } }, null); } catch (Exception) { if (OnSocketDisconnected != null) { OnSocketDisconnected(socket); } } }
public void Connect() { CommandSent += new CommandSentEventHandler(client_CommandSent); CommandSendingFailed += new CommandSendingFailedEventHandler(client_CommandSendingFailed); CommandReceivingFailed += new CommandReceivingFailedEventHandler(client_CommandReceivingFailed); ServerDisconnected += new ServerDisconnectedEventHandler(client_ServerDisconnected); DisconnectedFromServer += new DisconnectedEventHandler(client_DisconnectedFromServer); ConnectingSuccessed += new ConnectingSuccessedEventHandler(client_ConnectingSuccessed); ConnectingFailed += new ConnectingFailedEventHandler(client_ConnectingFailed); NetworkDead += new NetworkDeadEventHandler(client_NetworkDead); NetworkAlived += new NetworkAlivedEventHandler(client_NetworkAlived); ConnectToServer(); }
/// <summary> /// Handle a keypress /// </summary> /// <param name="sender">event sender</param> /// <param name="e">event parameters</param> private void HandleKeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '\r') { CommandSentEventHandler handler = CommandSent; if (handler != null) { handler(Text); if (AutoCompletion) { if (AutoCompleteCustomSource != null) { AutoCompleteCustomSource.Add(Text); } } } Text = ""; e.Handled = true; } }