private void Listen(IPEndPoint localEndpoint)
        {
            _logger.Trace("Listen (sync) using local endpoint {0}", localEndpoint);

            // create a socket
            _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // bind and listen
            try
            {
                _listener.Bind(localEndpoint);
                _listener.Listen(1);

                // accept the next connection
                bool completesAsynchronously = _listener.AcceptAsync(_socketOperation);

                if (!completesAsynchronously)
                {
                    SocketAsyncEventArgs_Completed(_socketOperation.AcceptSocket, _socketOperation);
                }
            }
            catch (Exception ex)
            {
                // raise error event
                var handlers = Error;
                if (handlers != null)
                {
                    var args = new NetworkErrorEventArgs("The listen operation failed: " + ex.ToString(), null, ex);
                    handlers(this, args);
                }
            }
        }
Example #2
0
        private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            _logger.Trace("Received Completed event from socket async event args");

            // check for errors
            if (e.SocketError != SocketError.Success)
            {
                _logger.Trace("Raising event Error with {0}", e.SocketError);

                // raise error event
                var handlers = Error;
                if (handlers != null)
                {
                    var message = string.Format("The last socket operation ({0}) failed.", e.LastOperation);
                    var args = new NetworkErrorEventArgs(message, e.SocketError.ToString(), null);
                    handlers(this, args);
                }

                return;
            }

            // check what has been executed
            switch (e.LastOperation)
            {
                case SocketAsyncOperation.SendTo:
                    _logger.Trace("Raising event DataSent");

                    // raise event
                    var sentHandlers = DataSent;
                    if (sentHandlers != null)
                    {
                        sentHandlers(this, EventArgs.Empty);
                    }
                    break;
                case SocketAsyncOperation.ReceiveFrom:
                    _logger.Trace("Raising event DataReceived");

                    // copy the received amount of data to separate array
                    var receivedBytes = new byte[e.BytesTransferred];
                    Array.Copy(e.Buffer, 0, receivedBytes, 0, e.BytesTransferred);

                    // raise event
                    var receivedHandlers = DataReceived;
                    if (receivedHandlers != null)
                    {
                        receivedHandlers(this, new DataReceivedEventArgs(receivedBytes));
                    }
                    break;
            }
        }
Example #3
0
        private void Socket_Error(object sender, NetworkErrorEventArgs e)
        {
            _logger.Trace("Raising Error event with message: {0}", e.Message);

            // raise our own event
            var handlers = Error;
            if (handlers != null)
            {
                handlers(this, new NetworkErrorEventArgs(e.Message, e.ErrorCode, e.Error));
            }
        }
        private void RaiseErrorEvent(NetworkErrorEventArgs e)
        {
            _logger.Trace("Raising Error event with message: {0}", e.Message);

            // make sure we only raise events on the correct context
            _synchronizationContext.Post(o =>
                                             {
                                                 var handlers = Error;
                                                 if (handlers != null)
                                                 {
                                                     var exception = new PhoneControllerException("Networking error: " + e.Message, e.Error, e.ErrorCode);
                                                     handlers(this, new ErrorEventArgs(exception));
                                                 }
                                             }, null);
        }
        private void DataChannel_Error(object sender, NetworkErrorEventArgs e)
        {
            _logger.Trace("Received Error event from data channel");

            var state = State;
            if (state == PhoneControllerState.Closed || state == PhoneControllerState.Closing || state == PhoneControllerState.Error)
            {
                // ignore follow-up errors after we're closed or already in error state
                return;
            }

            State = PhoneControllerState.Error;
            RaiseErrorEvent(e);
        }
        private void BroadCaster_TimeoutElapsed(object sender, EventArgs e)
        {
            lock (_locker)
            {
                _logger.Trace("Received TimeoutElapsed event from broadcaster");

                var state = State;
                // broadcasting is also possible from the closed state (during re-initialization)
                // so we only check for the error state here
                if (state == PhoneControllerState.Error)
                {
                    // ignore follow-up errors after we're closed or already in error state
                    return;
                }

                State = PhoneControllerState.Error;

                // create new error event args
                var eventArgs = new NetworkErrorEventArgs("Timeout while broadcasting.", "TimeOut", null);
                RaiseErrorEvent(eventArgs);
            }
        }
        private void RaiseErrorEvent(NetworkErrorEventArgs e)
        {
            _logger.Trace("Raising event Error");

            _synchronizationContext.Post(o =>
                                             {
                                                 var handlers = Error;
                                                 if (handlers != null)
                                                 {
                                                     var exception = new PhoneControllerException("Networking error: " + e.Message, e.Error, e.ErrorCode);
                                                     handlers(this, new ErrorEventArgs(exception));
                                                 }
                                             }, null);
        }
Example #8
0
        private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            _logger.Trace("Received Completed event from socket event args");

            // check for errors
            if (e.SocketError != SocketError.Success)
            {
                _logger.Trace("Raising event Error with code {0}", e.SocketError);

                // raise error event
                var handlers = Error;
                if (handlers != null)
                {
                    var message = string.Format("The last socket operation ({0}) failed.", e.LastOperation);
                    var args = new NetworkErrorEventArgs(message, e.SocketError.ToString(), null);
                    handlers(this, args);
                }

                return;
            }

            // check what has been executed
            switch (e.LastOperation)
            {
                case SocketAsyncOperation.Connect:
                    // store connect socket
                    _currentSocket = e.ConnectSocket;

                    _logger.Trace("Raising event Connected");

                    // raise event
                    var connectedHandlers = Connected;
                    if (connectedHandlers != null)
                    {
                        connectedHandlers(this, EventArgs.Empty);
                    }
                    break;
                case SocketAsyncOperation.Send:
                    // if zero bytes were transferred, the remote endpoint has closed the connection
                    if (e.BytesTransferred == 0)
                    {
                        RaiseConnectionClosedEvent();
                        break;
                    }

                    _logger.Trace("Raising event DataSent");

                    // raise event
                    var sentHandlers = DataSent;
                    if (sentHandlers != null)
                    {
                        sentHandlers(this, EventArgs.Empty);
                    }
                    break;
                case SocketAsyncOperation.Receive:
                    // if zero bytes were transferred, the remote endpoint has closed the connection
                    if (e.BytesTransferred == 0)
                    {
                        RaiseConnectionClosedEvent();
                        break;
                    }

                    // copy the received amount of data to separate array
                    var receivedBytes = new byte[e.BytesTransferred];
                    Array.Copy(e.Buffer, 0, receivedBytes, 0, e.BytesTransferred);

                    _logger.Trace("Raising event DataReceived");

                    // raise event
                    var receivedHandlers = DataReceived;
                    if (receivedHandlers != null)
                    {
                        receivedHandlers(this, new DataReceivedEventArgs(receivedBytes));
                    }
                    break;
                default:
                    OnUnknownOperationCompleted(sender, e);
                    break;
            }
        }