立即停止SFML TcpListener

时间:2016-08-12 00:23:58

标签: c++ multithreading networking tcp sfml

在主线程上使用SFML public class MainActivity extends AppCompatActivity { int ts; TextView score; Button submit; RadioGroup grp1, grp2, grp3; RadioButton bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); score = (TextView) findViewById(R.id.score); submit = (Button) findViewById(R.id.submit); grp1 = (RadioGroup) findViewById(R.id.grp1); grp2 = (RadioGroup) findViewById(R.id.grp2); grp3 = (RadioGroup) findViewById(R.id.grp3); bt1 =(RadioButton) findViewById(R.id.radioButton); bt2 =(RadioButton) findViewById(R.id.radioButton2); bt3 =(RadioButton) findViewById(R.id.radioButton3); bt4 =(RadioButton) findViewById(R.id.radioButton4); bt5 =(RadioButton) findViewById(R.id.radioButton5); bt6 =(RadioButton) findViewById(R.id.radioButton6); bt7 =(RadioButton) findViewById(R.id.radioButton7); bt8 =(RadioButton) findViewById(R.id.radioButton8); submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { grp1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { View Button = grp1.findViewById(checkedId); int index = grp1.indexOfChild(Button); switch (index) { case 0: break; case 1: break; case 2: break; case 3: ts++; break; } } }); grp2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { View Button = grp2.findViewById(checkedId); int index = grp2.indexOfChild(Button); switch (index) { case 0: break; case 1: break; case 2: break; case 3: ts++; break; } } }); grp3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { View Button = grp3.findViewById(checkedId); int index = grp3.indexOfChild(Button); switch (index) { case 0: ts++; break; case 1: break; case 2: break; case 3: break; } } }); calculate(); } }); } private void calculate (){ score.setText(""+ts); } } 时,如何立即停止TcpListener功能?例如,如果我希望能够收到TcpListener::accept()并清理一下。为此,我需要能够使SIGINT函数返回Disconnected或Error。

1 个答案:

答案 0 :(得分:0)

感谢Sean Cline建议non-blocking modeselectors

作为参考,SFML / Networking中的函数(我不确定它的其他组件)默认是阻塞的,这意味着它们在完成其目的之前不会返回(在我的情况下接受TCP连接)。通过使用non-blocking mode,函数将立即返回,返回其状态。对于TcpListener::accept(),如果有连接被接受并且它已成功完成任务,它将返回sf::Socket::Status::Done。否则,它可能会返回sf::Socket::Status::Disconnectedsf::Socket::Status::NotReady

对于selectors,它们允许您在单个线程上管理多个套接字,并且在您不知道哪个将首先发送数据时特别有用。