/// <summary> /// /// </summary> public async Task <HorseServerSocket> Connected(IHorseServer server, IConnectionInfo connection, ConnectionData data) { string clientId; bool found = data.Properties.TryGetValue(HorseHeaders.CLIENT_ID, out clientId); if (!found || string.IsNullOrEmpty(clientId)) { clientId = _server.Server.ClientIdGenerator.Create(); } //if another client with same unique id is online, do not accept new client MqClient foundClient = _server.Clients.Find(x => x.UniqueId == clientId); if (foundClient != null) { await connection.Socket.SendAsync(HmqWriter.Create(MessageBuilder.Busy())); return(null); } //creates new node client object MqClient client = new MqClient(_server.Server, connection); client.Data = data; client.UniqueId = clientId.Trim(); client.Token = data.Properties.GetStringValue(HorseHeaders.CLIENT_TOKEN); client.Name = data.Properties.GetStringValue(HorseHeaders.CLIENT_NAME); client.Type = data.Properties.GetStringValue(HorseHeaders.CLIENT_TYPE); if (_server.Authenticator != null) { bool accepted = await _server.Authenticator.Authenticate(_server, client); if (!accepted) { return(null); } } client.RemoteHost = client.Info.Client.Client.RemoteEndPoint.ToString()?.Split(':')[0]; _server.Clients.Add(client); await client.SendAsync(MessageBuilder.Accepted(client.UniqueId)); return(client); }
/// <summary> /// Called when a new client is connected via TMQ protocol /// </summary> public async Task <TmqServerSocket> Connected(ITwinoServer server, IConnectionInfo connection, ConnectionData data) { string clientId; bool found = data.Properties.TryGetValue(TmqHeaders.CLIENT_ID, out clientId); if (!found || string.IsNullOrEmpty(clientId)) { clientId = _server.ClientIdGenerator.Create(); } //if another client with same unique id is online, do not accept new client MqClient foundClient = _server.FindClient(clientId); if (foundClient != null) { await connection.Socket.SendAsync(await _writer.Create(MessageBuilder.Busy())); return(null); } //creates new mq client object MqClient client = new MqClient(_server, connection, _server.MessageIdGenerator, _server.Options.UseMessageId); client.Data = data; client.UniqueId = clientId.Trim(); client.Token = data.Properties.GetStringValue(TmqHeaders.CLIENT_TOKEN); client.Name = data.Properties.GetStringValue(TmqHeaders.CLIENT_NAME); client.Type = data.Properties.GetStringValue(TmqHeaders.CLIENT_TYPE); //connecting client is a MQ server string serverValue = data.Properties.GetStringValue(TmqHeaders.TWINO_MQ_SERVER); if (!string.IsNullOrEmpty(serverValue) && (serverValue.Equals("1") || serverValue.Equals("true", StringComparison.InvariantCultureIgnoreCase))) { if (_server.ServerAuthenticator == null) { return(null); } bool accepted = await _server.ServerAuthenticator.Authenticate(_server, client); if (!accepted) { return(null); } client.IsInstanceServer = true; _server.SlaveInstances.Add(new SlaveInstance { ConnectedDate = DateTime.UtcNow, Client = client, RemoteHost = client.Info.Client.Client.RemoteEndPoint.ToString().Split(':')[0] }); await client.SendAsync(MessageBuilder.Accepted(client.UniqueId)); } //connecting client is a producer/consumer client else { //authenticates client if (_server.Authenticator != null) { client.IsAuthenticated = await _server.Authenticator.Authenticate(_server, client); if (!client.IsAuthenticated) { await client.SendAsync(MessageBuilder.Unauthorized()); return(null); } } //client authenticated, add it into the connected clients list _server.AddClient(client); //send response message to the client, client should check unique id, //if client's unique id isn't permitted, server will create new id for client and send it as response await client.SendAsync(MessageBuilder.Accepted(client.UniqueId)); if (_server.ClientHandler != null) { await _server.ClientHandler.Connected(_server, client); } } return(client); }
/// <summary> /// Called when a new client is connected via HMQ protocol /// </summary> public async Task <HorseServerSocket> Connected(IHorseServer server, IConnectionInfo connection, ConnectionData data) { string clientId; bool found = data.Properties.TryGetValue(HorseHeaders.CLIENT_ID, out clientId); if (!found || string.IsNullOrEmpty(clientId)) { clientId = _server.ClientIdGenerator.Create(); } //if another client with same unique id is online, do not accept new client MqClient foundClient = _server.FindClient(clientId); if (foundClient != null) { await connection.Socket.SendAsync(HmqWriter.Create(MessageBuilder.Busy())); return(null); } if (_server.Options.ClientLimit > 0 && _server.GetOnlineClients() >= _server.Options.ClientLimit) { return(null); } //creates new mq client object MqClient client = new MqClient(_server, connection, _server.MessageIdGenerator, _server.Options.UseMessageId); client.Data = data; client.UniqueId = clientId.Trim(); client.Token = data.Properties.GetStringValue(HorseHeaders.CLIENT_TOKEN); client.Name = data.Properties.GetStringValue(HorseHeaders.CLIENT_NAME); client.Type = data.Properties.GetStringValue(HorseHeaders.CLIENT_TYPE); //authenticates client foreach (IClientAuthenticator authenticator in _server.Authenticators) { client.IsAuthenticated = await authenticator.Authenticate(_server, client); if (!client.IsAuthenticated) { await client.SendAsync(MessageBuilder.Unauthorized()); return(null); } } //client authenticated, add it into the connected clients list _server.AddClient(client); //send response message to the client, client should check unique id, //if client's unique id isn't permitted, server will create new id for client and send it as response await client.SendAsync(MessageBuilder.Accepted(client.UniqueId)); foreach (IClientHandler handler in _server.ClientHandlers) { await handler.Connected(_server, client); } return(client); }