예제 #1
0
        ///<summary>Called when there's an incoming client connection waiting to be accepted.</summary>
        ///<param name="ar">The result of the asynchronous operation.</param>
        public override void OnAccept(IAsyncResult ar)
        {
            try
            {
                Socket NewSocket = ListenSocket.EndAccept(ar);
                try
                {
                    //Restart Listening
                    ListenSocket.BeginAccept(new AsyncCallback(OnAccept), ListenSocket);
                }
                catch
                {
                    Dispose();
                }
                if (NewSocket != null)
                {
                    HttpClient NewClient = new HttpClient(NewSocket, RemoveClient);
                    AddClient(NewClient);
                    NewClient.StartHandshake();

                    if (OnClientAdded != null)
                    {
                        OnClientAdded.Invoke(NewClient);
                    }
                }
            }
            catch
            {
            }
        }
예제 #2
0
        public async Task <uint> PostClient([FromBody] Client item)
        {
            _context.Clients.Add(item);
            await _context.SaveChangesAsync();

            CreatedAtAction(nameof(GetClient), new { item.id }, item);
            OnClientAdded?.Invoke(item);

            return(item.id);
        }
예제 #3
0
 /// <summary>
 /// Adds a client to the session.
 /// Returns null if the identifier is being used.
 /// </summary>
 /// <param name="identifier"></param>
 /// <param name="client"></param>
 /// <returns></returns>
 public virtual URollbackClient AddClient(int identifier, URollbackClient client)
 {
     if (clients.ContainsKey(identifier))
     {
         return(null);
     }
     clients.Add(identifier, client);
     OnClientAdded?.Invoke(identifier);
     return(clients[identifier]);
 }
        public void AddClient(Client client)
        {
            client.StartThreads();

            Clients.TryAdd(client.SocketId, client);

            // Process client connected events.

            OnClientAdded?.Invoke(client);
        }
예제 #5
0
파일: Client.cs 프로젝트: wuliuquan/P2Pchat
        private void ProcessItem(IP2PBase Item, IPEndPoint EP = null)
        {
            if (Item.GetType() == typeof(Message))
            {
                Message    m  = (Message)Item;
                ClientInfo CI = Clients.FirstOrDefault(x => x.ID == Item.ID);

                if (m.ID == 0)
                {
                    if (OnResultsUpdate != null)
                    {
                        OnResultsUpdate.Invoke(this, m.From + ": " + m.Content);
                    }
                }

                if (m.ID != 0 & EP != null & CI != null)
                {
                    if (OnMessageReceived != null)
                    {
                        OnMessageReceived.Invoke(EP, new MessageReceivedEventArgs(CI, m, EP));
                    }
                }
            }
            else if (Item.GetType() == typeof(ClientInfo))
            {
                ClientInfo CI = Clients.FirstOrDefault(x => x.ID == Item.ID);

                if (CI == null)
                {
                    Clients.Add((ClientInfo)Item);

                    if (OnClientAdded != null)
                    {
                        OnClientAdded.Invoke(this, (ClientInfo)Item);
                    }
                }
                else
                {
                    CI.Update((ClientInfo)Item);

                    if (OnClientUpdated != null)
                    {
                        OnClientUpdated.Invoke(this, (ClientInfo)Item);
                    }
                }
            }
            else if (Item.GetType() == typeof(Notification))
            {
                Notification N = (Notification)Item;

                if (N.Type == NotificationsTypes.Disconnected)
                {
                    ClientInfo CI = Clients.FirstOrDefault(x => x.ID == long.Parse(N.Tag.ToString()));

                    if (CI != null)
                    {
                        if (OnClientRemoved != null)
                        {
                            OnClientRemoved.Invoke(this, CI);
                        }

                        Clients.Remove(CI);
                    }
                }
                else if (N.Type == NotificationsTypes.ServerShutdown)
                {
                    if (OnResultsUpdate != null)
                    {
                        OnResultsUpdate.Invoke(this, "Server shutting down.");
                    }

                    ConnectOrDisconnect();
                }
            }
            else if (Item.GetType() == typeof(Req))
            {
                Req R = (Req)Item;

                ClientInfo CI = Clients.FirstOrDefault(x => x.ID == R.ID);

                if (CI != null)
                {
                    if (OnResultsUpdate != null)
                    {
                        OnResultsUpdate.Invoke(this, "Received Connection Request from: " + CI.ToString());
                    }

                    IPEndPoint ResponsiveEP = FindReachableEndpoint(CI);

                    if (ResponsiveEP != null)
                    {
                        if (OnResultsUpdate != null)
                        {
                            OnResultsUpdate.Invoke(this, "Connection Successfull to: " + ResponsiveEP.ToString());
                        }

                        if (OnClientConnection != null)
                        {
                            OnClientConnection.Invoke(CI, ResponsiveEP);
                        }

                        if (OnClientUpdated != null)
                        {
                            OnClientUpdated.Invoke(this, CI);
                        }
                    }
                }
            }
            else if (Item.GetType() == typeof(Ack))
            {
                Ack A = (Ack)Item;

                if (A.Responce)
                {
                    AckResponces.Add(A);
                }
                else
                {
                    ClientInfo CI = Clients.FirstOrDefault(x => x.ID == A.ID);

                    if (CI.ExternalEndpoint.Address.Equals(EP.Address) & CI.ExternalEndpoint.Port != EP.Port)
                    {
                        if (OnResultsUpdate != null)
                        {
                            OnResultsUpdate.Invoke(this, "Received Ack on Different Port (" + EP.Port + "). Updating ...");
                        }

                        CI.ExternalEndpoint.Port = EP.Port;

                        if (OnClientUpdated != null)
                        {
                            OnClientUpdated.Invoke(this, CI);
                        }
                    }

                    List <string> IPs = new List <string>();
                    CI.InternalAddresses.ForEach(new Action <IPAddress>(delegate(IPAddress IP) { IPs.Add(IP.ToString()); }));

                    if (!CI.ExternalEndpoint.Address.Equals(EP.Address) & !IPs.Contains(EP.Address.ToString()))
                    {
                        if (OnResultsUpdate != null)
                        {
                            OnResultsUpdate.Invoke(this, "Received Ack on New Address (" + EP.Address + "). Updating ...");
                        }

                        CI.InternalAddresses.Add(EP.Address);
                    }

                    A.Responce    = true;
                    A.RecipientID = LocalClientInfo.ID;
                    SendMessageUDP(A, EP);
                }
            }
        }
예제 #6
0
        public void AddClient(Client client)
        {
            mClients.Add(client);

            OnClientAdded?.Invoke(client);
        }