Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public ITCPClient AcceptTCPClient()
        {
            if (IsDisposed)
            {
                return(null);
            }

            return(new DesktopTCPClient(Listener.Accept()));
        }
Example #2
0
        protected void Listen()
        {
            while (!Closed)
            {
                Socket socket = Listener.Accept();

                var conn = this.AddConnection(socket);
            }
        }
        private void Listening()
        {
            Socket handler = null;

            try
            {
                handler = Listener.Accept();

                if (!Connected)
                {
                    OnConnect?.Invoke(this, null);
                    Connected = true;
                }

                while (ListenerEnabled)
                {
                    byte[] buffer = new byte[MessageSize];

                    int bytesRead = handler.Receive(buffer, 0, MessageSize, 0);

                    ReadBytes(bytesRead, buffer);

                    if (this.MessageToSend != null)
                    {
                        Send(handler, this.MessageToSend);
                    }
                }
            }
            catch (Exception e)
            {
                OnError?.Invoke(this, new ErrorEventArgs(ErrorType.Generic, e));
            }
            finally
            {
                if (handler != null)
                {
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();

                    Listener.Close();
                }

                if (Connected)
                {
                    OnDisconnect?.Invoke(this, null);
                    Connected = false;
                }
            }
        }
Example #4
0
        private void Task()
        {
            Listener.Listen(100);

            while (!Cancel)
            {
                if (Pause)
                {
                    Thread.Sleep(2000);
                    continue;
                }

                var sock = Listener.Accept();
                new Thread(() => HandleClient(new Client {
                    Sock = sock
                })).Start();
            }
        }
Example #5
0
            public void Init()
            {
                if (RetryTemplate != null && ErrorChannel != null)
                {
                    throw new InvalidOperationException(
                              "Cannot have an 'errorChannel' property when a 'RetryTemplate' is "
                              + "provided; use an 'ErrorMessageSendingRecoverer' in the 'recoveryCallback' property to "
                              + "send an error message when retries are exhausted");
                }

                var messageListener = new Listener(this);

                if (RetryTemplate != null)
                {
                    RetryTemplate.RegisterListener(messageListener);
                }

                _messageListenerContainer.MessageListener = (m) => messageListener.Accept(m);
            }
Example #6
0
        /**
         * Connects to the client on the specified port.
         * Displays data sent from client.
         *
         * @return void
         */
        public void ExecuteServer()
        {
            //Create a socket to establish connection to client
            ConnectSocket(11111);

            while (true)
            {
                Console.WriteLine("Awaiting connection from client ... ");

                // Suspend while waiting for incoming connection Using Accept()
                ClientSocket = Listener.Accept();

                //Display connected status message
                Console.WriteLine("Connected\n");

                //Proceed to the rest of the program once connection has been established
                break;
            }
        }
Example #7
0
            public void Check()
            {
                while (Listener.Pending())
                {
                    var client = Listener.Accept();

                    Player player = Server.Players.GetFreePlayer();
                    player.Connect(client);
                    player.State = PlayerState.Connecting;
                }

                foreach (var c in Server.Players)
                {
                    c.Check();

                    if (c.Tcp.BatchStream.BaseStream.Length != 0)
                    {
                        var arr = ((MemoryStream)c.Tcp.BatchStream.BaseStream).ToArray();
                        c.Tcp.NetStreamBinary.Write(arr);

                        c.Udp.Stream.BaseStream.SetLength(0);
                    }
                }
            }
Example #8
0
 /// <summary>
 /// This method calls Open if it was not called already by the
 /// application, and then enters an endless loop where it
 /// repeatedly waits for incoming requests, accepts them, and
 /// performs the necessary processing for handling the request.
 /// </summary>
 public void Run()
 {
     if (!IsOpen)
     {
         Open();
     }
     while (IsOpen)
     {
         Stream connection = null;
         // wait for next request
         while (connection == null)
         {
             try
             {
                 connection = Listener.Accept();
                 Contract.Assert(connection != null);
             }
             catch (IOException e)
             {
                 Contract.Assert(connection == null);
                 // possibly device was disconnected
                 Debug.Print("IO error in Accept:\r\n" + e.Message);
                 if (Diagnostics != null)
                 {
                     Diagnostics.AcceptErrors = Diagnostics.AcceptErrors + 1;
                 }
                 Thread.Sleep(500);     // TODO does it make sense like this?
             }
             catch (SocketException e)
             {
                 Contract.Assert(connection == null);
                 // possibly Internet was disconnected
                 Debug.Print("Socket error in Accept:\r\n" + e.Message);
                 if (Diagnostics != null)
                 {
                     Diagnostics.AcceptErrors = Diagnostics.AcceptErrors + 1;
                 }
                 Thread.Sleep(500);     // TODO does it make sense like this?
             }
             catch (ContractViolation e)
             {
                 Contract.Assert(connection == null);
                 Debug.Print("bug in Accept:\r\n" + e);
                 if (Diagnostics != null)
                 {
                     Diagnostics.AcceptFailures = Diagnostics.AcceptFailures + 1;
                 }
                 Thread.Sleep(500);     // TODO does it make sense like this?
             }
             catch (Exception e)
             {
                 Contract.Assert(connection == null);
                 Debug.Print("exception in Accept:\r\n" + e);
                 if (Diagnostics != null)
                 {
                     Diagnostics.AcceptFailures = Diagnostics.AcceptFailures + 1;
                 }
                 Thread.Sleep(500);     // TODO does it make sense like this?
             }
         }
         // handle request
         var connectionClose = true;
         // ignore connectionClose, i.e., always
         // close connection after a request
         try
         {
             if (Diagnostics != null)
             {
                 Diagnostics.RequestsTotal = Diagnostics.RequestsTotal + 1;
             }
             ConsumeRequest(connection, Listener.ServiceRoot,
                            RelayDomain, RequestRouting,
                            ref connectionClose);
         }
         catch (IOException e)
         {
             // possibly device was disconnected, or host has sent no data (read timeout)
             Debug.Print("IO error in ConsumeRequest:\r\n" + e.Message);
             if (Diagnostics != null)
             {
                 Diagnostics.RequestHandlerErrors = Diagnostics.RequestHandlerErrors + 1;
             }
         }
         catch (SocketException e)
         {
             // possibly Internet was disconnected, or host has sent no data (read timeout)
             Debug.Print("Socket error in ConsumeRequest:\r\n" + e.ErrorCode);
             if (Diagnostics != null)
             {
                 Diagnostics.RequestHandlerErrors = Diagnostics.RequestHandlerErrors + 1;
             }
         }
         catch (ContractViolation e)
         {
             Debug.Print("bug in ConsumeRequest:\r\n" + e);
             if (Diagnostics != null)
             {
                 Diagnostics.RequestHandlerFailures = Diagnostics.RequestHandlerFailures + 1;
             }
         }
         //TODO remove comments
         //catch (Exception e)
         //{
         //    Debug.Print("exception in ConsumeRequest:\r\n" + e);
         //    if (Diagnostics != null)
         //    {
         //        Diagnostics.RequestHandlerFailures = Diagnostics.RequestHandlerFailures + 1;
         //    }
         //}
         connection.Close();
     }
 }
Example #9
0
 public void Start(int nofThreads)
 {
     Contract.Requires(nofThreads > 0);
     ThreadPool.Open(nofThreads);
     ThreadPool.Start();
     if (!IsOpen)
     {
         Open();
     }
     while (IsOpen)
     {
         // wait for next request
         Stream connection = null;
         try
         {
             Debug.Print("before accept");
             connection = Listener.Accept();
             Debug.Print("after accept");
             Contract.Ensures(connection != null);
         }
         catch (IOException e)
         {
             Contract.Assert(connection == null);
             // possibly device was disconnected
             Debug.Print("IO error in Accept:\r\n" + e.Message);
             if (Diagnostics != null)
             {
                 Diagnostics.AcceptErrors = Diagnostics.AcceptErrors + 1;
             }
             Thread.Sleep(500);     // TODO does it make sense like this?
         }
         catch (SocketException e)
         {
             Contract.Assert(connection == null);
             // possibly Internet was disconnected
             Debug.Print("Socket error in Accept:\r\n" + e.Message);
             if (Diagnostics != null)
             {
                 Diagnostics.AcceptErrors = Diagnostics.AcceptErrors + 1;
             }
             Thread.Sleep(500);     // TODO does it make sense like this?
         }
         catch (ContractViolation e)
         {
             Contract.Assert(connection == null);
             Debug.Print("bug in Accept:\r\n" + e);
             if (Diagnostics != null)
             {
                 Diagnostics.AcceptFailures = Diagnostics.AcceptFailures + 1;
             }
             Thread.Sleep(500);     // TODO does it make sense like this?
         }
         catch (Exception e)
         {
             Contract.Assert(connection == null);
             Debug.Print("exception in Accept:\r\n" + e);
             if (Diagnostics != null)
             {
                 Diagnostics.AcceptFailures = Diagnostics.AcceptFailures + 1;
             }
             Thread.Sleep(500);     // TODO does it make sense like this?
         }
         if (connection != null)
         {
             Debug.Print("connection was accepted");
             ThreadPool.QueueUserWorkItem(ProcessRequest, connection);
         }
     }
 }
Example #10
0
 public override R Accept <R, A>(Listener <R, A> listener, A data)
 {
     return(listener.Accept(this, data));
 }
Example #11
0
 public Addr Accept() => s_AcceptByRef?.Invoke(ref this) ?? s_AcceptByVal?.Invoke(this) ?? Listener?.Accept() ?? throw new PanicException(RuntimeErrorPanic.NilPointerDereference);