Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public bool StartListening(string ipAddress, int port)
- {
- Task.Run(() =>
- {
- _canceller = new CancellationTokenSource();
- _backingTcpListener = new TcpListener(IPAddress.Parse(ipAddress), port);
- _backingTcpListener.Start();
- WaitForConnections(_canceller.Token);
- });
- return true;
- }
- private void WaitForConnections(CancellationToken cancelToken)
- {
- Task.Factory.StartNew(async () =>
- {
- while (!cancelToken.IsCancellationRequested)
- {
- var inner = await Task.Run(() =>
- {
- try
- {
- return _backingTcpListener.AcceptTcpClient();
- }
- catch (SocketException)
- {
- // this will be caused by StopListeningAsync
- return null;
- }
- }, cancelToken);
- if (inner == null)
- {
- // null will be returned when the SocketException is caught above
- return;
- }
- var outer = new TcpSocketClient(inner, _bufferSize);
- var args = new TcpSocketConnectionReceivedEventArgs(outer);
- ConnectionReceived?.Invoke(args);
- }
- }, cancelToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement