Пример #1
0
        public static void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            AllDone.Set();

            // Get the socket that handles the client request.
            Socket listener = (Socket)ar.AsyncState;
            Socket handler  = listener.EndAccept(ar);

            // Create the state object.
            StateObject state = new StateObject();

            state.workSocket = handler;

            Client client = new Client(ClientController.GetClientListCount());

            client.ConnectionTime = DateTime.Now;
            client.IP             = Convert.ToString(IPAddress.Parse(((IPEndPoint)state.workSocket.RemoteEndPoint).Address.ToString()));
            client.Port           = ((IPEndPoint)state.workSocket.RemoteEndPoint).Port;
            ClientController.AddClient(client);

            IPEndPoint clientEndPoint = (IPEndPoint)state.workSocket.RemoteEndPoint;

            Sistem.printF(string.Format("Client Connected: {0}", clientEndPoint.Address.ToString()), ConsoleColor.Green);

            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
        }
Пример #2
0
        public void Listen()
        {
            try
            {
                var ipHostInfo = Dns.GetHostEntry(Config.Host);
                var ipAddress  = ipHostInfo.AddressList
                                 .First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
                var localEndPoint = new IPEndPoint(ipAddress, Config.Port);
                var listener      = new Socket(ipAddress.AddressFamily,
                                               SocketType.Stream, ProtocolType.Tcp);

                listener.Bind(localEndPoint);
                listener.Listen(100);
                while (true)
                {
                    AllDone.Reset();
                    listener.BeginAccept(
                        AcceptCallback,
                        listener);
                    AllDone.WaitOne();
                }
            }
            catch (Exception e)
            {
                Abort(null, e);
            }
        }
Пример #3
0
        public void AcceptCallback(IAsyncResult ar)
        {
            AllDone.Set();
            var listener = (Socket)ar.AsyncState;
            var handler  = listener.EndAccept(ar);
            var state    = new HubState(Config, Fs)
            {
                WorkSocket = handler
            };

            handler.BeginReceive(state.Buffer, 0, Config.BufferSize, 0,
                                 ReadCallback, state);
        }
Пример #4
0
        public async Task InvokeAllDone(object sender, int totalEpochs, float finalError)
        {
            if (task != null)
            {
                await task;
                task.Dispose();
            }

            task = Task.Run(() =>
                            AllDone?.Invoke(sender, new AllDoneArgs
            {
                TotalEpochs = totalEpochs,
                FinalError  = finalError
            })
                            );
        }
Пример #5
0
        public object Cmd(ProtoCommand cmd, string uri, long start, long len, string file = "")
        {
            if (file == "")
            {
                file = uri;
            }
            var state = new AgentState(Config, Fs)
            {
                FileName = file,
                Gram     = new ProtoGram(cmd, start, len, uri)
            };

            try
            {
                var ipHostInfo = Dns.GetHostEntry(Config.Host);
                var ipAddress  = ipHostInfo.AddressList
                                 .First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
                var remoteEp = new IPEndPoint(ipAddress, Config.Port);
                var client   = new Socket(ipAddress.AddressFamily,
                                          SocketType.Stream, ProtocolType.Tcp);
                state.WorkSocket = client;
                client.BeginConnect(remoteEp, ConnectCallback, state);
                ConnectDone.WaitOne();
                Send(state);
                SendDone.WaitOne();
                Receive(state);
                AllDone.WaitOne();
                if (state.Gram.Status == ProtoStatus.Success)
                {
                    return(Complete(state));
                }
                else
                {
                    throw new ApplicationException(state.Url);
                }
            }
            catch (Exception e)
            {
                Abort(state, e);
                return(null);
            }
        }
Пример #6
0
        protected override void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            AllDone.Set();

            // Get the socket that handles the client request.
            Socket listener = (Socket)ar.AsyncState;
            Socket handler  = listener.EndAccept(ar);

            // Create the ClientState object and begin recieving data
            ClientState client = new ClientState();

            client.WorkSocket = handler;

            // Recieve the upgrade request in an async recieve callback
            handler.BeginReceive(client.buffer, 0, ClientState.BufferSize,
                                 0, new AsyncCallback(Handshake), client);

            OnClientConnect(client);
        }
Пример #7
0
        public static void StartListening()
        {
            // Establish the local endpoint for the socket.
            // The DNS name of the computer
            // running the listener is "host.contoso.com".
            IPHostEntry ipHostInfo    = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ipAddress     = ipHostInfo.AddressList[0];
            IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, Port);

            // Create a TCP/IP socket.
            Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(100);

                Sistem.printF(Sistem.GetLogTag(Sistem.EnumLogTags.SERVER) + "Async Socket Server  ONLINE at " + ipAddress.ToString() + ":" + Port, ConsoleColor.Green);

                while (true)
                {
                    // Set the event to nonsignaled state.
                    AllDone.Reset();

                    // Start an asynchronous socket to listen for connections.
                    Sistem.printF(Sistem.GetLogTag(Sistem.EnumLogTags.SERVER) + "Waiting for a connection...", ConsoleColor.Gray);
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

                    // Wait until a connection is made before continuing.
                    AllDone.WaitOne();
                }
            }
            catch (Exception e)
            {
                Sistem.printF(e.ToString(), ConsoleColor.Red);
            }

            Sistem.printF("\nPress ENTER to continue...", ConsoleColor.Gray, false, false, true, true, true);
        }
Пример #8
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            var state = (AgentState)ar.AsyncState;

            try
            {
                var client    = state.WorkSocket;
                var bytesRead = client.EndReceive(ar);
                if (bytesRead > 0)
                {
                    state.Receive(bytesRead);
                    client.BeginReceive(state.Buffer, 0, Config.BufferSize, 0,
                                        ReceiveCallback, state);
                }
                else
                {
                    AllDone.Set();
                }
            }
            catch (Exception e)
            {
                Abort(state, e);
            }
        }
Пример #9
0
 public static void Terminate()
 {
     _running = false;
     AllDone.Set();
 }
Пример #10
0
 private void FireAllDone() => AllDone?.Invoke();