public bool Remove(string connectionId) { lock (_syncObj) { var isRemoved = false; var _database = GetDatabase(); var clientValue = _database.HashGet(_clientStoreKey, connectionId); if (clientValue.IsNullOrEmpty) { return(isRemoved); } _database.HashDelete(_clientStoreKey, connectionId); isRemoved = true; var client = JsonConvert.DeserializeObject <OnlineClient>(clientValue); if (isRemoved) { var user = client.ToUserIdentifierOrNull(); if (user != null && !IsUserOnline(user)) { UserDisconnected.InvokeSafely(this, new OnlineUserEventArgs(user, client)); } ClientDisconnected.InvokeSafely(this, new OnlineClientEventArgs(client)); } return(isRemoved); } }
private void DisconnectCommand(NetworkMessage message) { if (message.TryGetObject <string>(out string disconnectedUser)) { UserDisconnected?.Invoke(disconnectedUser); } }
private void OnClientDisconnected(IConnection sender) { TcpConnection removed; connections.TryRemove(sender.Id, out removed); UserDisconnected?.Invoke(sender); }
public void Kick(GameUser user) { user.Handler.Shutdown(SocketShutdown.Both); user.Handler.Close(); UserDisconnected?.Invoke(this, new GameEventArgs(user, string.Empty)); pool.Remove(user); }
private void OnUserDisconnected(User user) { if (UserDisconnected == null) { return; } UserDisconnected.Invoke(user); }
public void DisconnectClient(DirectClientProvider client) { Clients.Remove(client); UserDisconnected?.Invoke(this, new ProviderUserEventArgs() { UserKey = client, }); }
public WebSocketNetworkClient(IWebSocketConnection _socket) { this.socket = _socket; this.socket.OnMessage += message => DataReceived?.Invoke(Encoding.UTF8.GetBytes(message)); this.socket.OnBinary += message => DataReceived?.Invoke(message); this.socket.OnClose += () => UserDisconnected?.Invoke(); ((INetworkClient)this).OnUserConnected(); }
public void Remove(Worker worker) { lock (workersLock) { workers.Remove(worker); UserDisconnected?.Invoke(worker.Username); } }
public void Disconnect() { if (IsConnected) { IsConnected = false; ActiveServer.DisconnectClient(this); UserDisconnected?.Invoke(this, new ProviderUserEventArgs()); } }
void TasClient_UserRemoved(object sender, UserDisconnected e) { var userName = e.Name; if (PlayerListItems.Any(u => u.UserName == userName)) { AddLine(new LeaveLine(userName, string.Format("User has disconnected ({0}).", e.Reason))); } }
void TasClient_UserRemoved(object sender, UserDisconnected e) { var userName = e.Name; var pmControl = GetPrivateMessageControl(userName); if (pmControl != null) { toolTabs.SetIcon(userName, ZklResources.grayuser, true); } }
void TasClient_UserRemoved(object sender, UserDisconnected e) { var userName = e.Name; if (userName != UserName) { return; } AddLine(new LeaveLine(userName)); }
public void DisconnectUser(object userKey) { Clients.Remove(userKey as DirectClientProvider); (userKey as DirectClientProvider).DisconnectFrom(this); UserDisconnected?.Invoke(this, new ProviderUserEventArgs() { UserKey = userKey, }); }
public void Disconnect() { if (Connection != null) { Connection.Shutdown(SocketShutdown.Both); Connection.Close(); Connection = null; UserDisconnected?.Invoke(this, new ProviderUserEventArgs()); } }
private void NotifierOnUserDisconnected(object sender, NotifierEventArgs <User> notifierEventArgs) { if (notifierEventArgs?.Payload != null) { var user = notifierEventArgs.Payload; Friends.Remove(user); UserDisconnected?.Invoke(this, notifierEventArgs); } }
public override void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) { NebulaUser user = GetUser(peer.Id); if (user != null) { ConnectedUsers.Remove(user.Id); } UserDisconnected?.Invoke(this, new UserDisconnectedEventArgs(user, disconnectInfo)); WriteLine($"Client Disconnected '{peer.EndPoint.Address}:{peer.EndPoint.Port}'. Reason: {disconnectInfo.Reason} ({disconnectInfo.SocketErrorCode})", ConsoleColor.Red); }
public void UpdateServer() { NetIncomingMessage inc = null; while (server != null && (inc = server.ReadMessage()) != null) { switch (inc.MessageType) { case NetIncomingMessageType.StatusChanged: Console.WriteLine(inc.SenderEndPoint.ToString() + " Status: " + inc.SenderConnection.Status.ToString()); switch (inc.SenderConnection.Status) { case NetConnectionStatus.Connected: if ((connections.Count >= maxConnections && maxConnections > 0) || AcceptIncomingConnections == false) { inc.SenderConnection.Disconnect("Server full"); } else { connections.Add(inc.SenderConnection); UserConnected?.Invoke(this, new ProviderUserEventArgs() { UserKey = inc.SenderConnection, }); } break; case NetConnectionStatus.Disconnected: connections.Remove(inc.SenderConnection); UserDisconnected?.Invoke(this, new ProviderUserEventArgs() { UserKey = inc.SenderConnection, }); break; } break; case NetIncomingMessageType.Data: var bytes = inc.ReadBytes(inc.LengthBytes); DataReceived?.Invoke(this, new ProviderDataEventArgs(inc.SenderConnection, true, new DataBuffer(bytes), inc.LengthBytes)); break; case NetIncomingMessageType.Error: case NetIncomingMessageType.ErrorMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.VerboseDebugMessage: Console.WriteLine(DateTime.Now.ToShortTimeString() + "Lidgren Server" + inc.ReadString()); break; } } }
/// <summary> /// Trace the event that a client disconnected. /// </summary> /// <param name="remoteAddress">The client that disconnected.</param> internal void TraceUserDisconnection(IPEndPoint remoteAddress) { Task.Run(() => { lock (ConnectedUsersSyncRoot) connectedUsers.Remove(remoteAddress); try { UserDisconnected?.Invoke(remoteAddress); } catch { } }); }
public void StopServer() { if (server != null) { foreach (var connection in connections) { UserDisconnected?.Invoke(this, new ProviderUserEventArgs() { UserKey = connection, }); } server.Shutdown("Server shutting down"); IsActive = false; server = null; } }
/// <summary> /// 移除Client /// </summary> /// <param name="connectionId"></param> /// <returns></returns> public bool Remove(string connectionId) { lock (_syncObj) { var _database = GetDatabase(); var clientValue = _database.HashGet(_clientStoreKey, connectionId); if (clientValue.IsNullOrEmpty) { return(true); } var client = JsonConvert.DeserializeObject <OnlineClient>(clientValue); var user = client.ToUserIdentifierOrNull(); if (user != null) { //从_userStoreKey中移除一个client var userClientsValue = _database.HashGet(_userStoreKey, user.ToUserIdentifierString()); if (userClientsValue.HasValue) { var userClients = JsonConvert.DeserializeObject <List <string> >(userClientsValue); userClients.Remove(connectionId); if (userClients.Count > 0) { //更新 _database.HashSet(_userStoreKey, new HashEntry[] { new HashEntry(user.ToUserIdentifierString(), userClients.ToJsonString()) }); } else { //删除 _database.HashDelete(_userStoreKey, user.ToUserIdentifierString()); } } _database.HashDelete(_clientStoreKey, connectionId); if (!IsUserOnline(user)) { UserDisconnected.InvokeSafely(this, new OnlineUserEventArgs(user, client)); } } ClientDisconnected.InvokeSafely(this, new OnlineClientEventArgs(client)); return(true); } }
private void OnDisconnected(string socketId) { if (host.SocketId == socketId) { host.IsConnected = false; UserDisconnected?.Invoke(host); //SendAllMessage($"user : {host.Name} has disconnected"); } foreach (var user in users) { if (user.SocketId == socketId) { user.IsConnected = false; UserDisconnected?.Invoke(user); //SendAllMessage($"user : {user.Name} has disconnected"); } } }
public void Dispose() { if (client != null) { byte[] arr = new byte[1] { 0 }; try { SendMessage(new DataBuffer(arr)); } catch { } client.Close(); UserDisconnected?.Invoke(this, new ProviderUserEventArgs()); } IsConnected = false; client = null; }
public async Task ConnectAsync() { _connection = new HubConnection(url); _hubProxy = _connection.CreateHubProxy("OrderBook"); _hubProxy.On <User>("UserLogin", (u) => UserLoggedIn?.Invoke(u)); _hubProxy.On <string>("UserLogout", (n) => UserLoggedOut?.Invoke(n)); _hubProxy.On <string>("UserDisconnection", (n) => UserDisconnected?.Invoke(n)); _hubProxy.On <string>("UserReconnection", (n) => UserReconnected?.Invoke(n)); _hubProxy.On <IEnumerable <Order> >("BroadcastOrders", (n) => ReceiveNewOrder?.Invoke(n)); _hubProxy.On <string, MessageType>("NotifyUser", (n, m) => ReceiveNotification?.Invoke(n, m)); _hubProxy.On <string>("SendBidDepth", (x) => ReceiveBidDepth?.Invoke(x)); _hubProxy.On <string>("SendAskDepth", (x => ReceiveAskDepth?.Invoke(x))); _connection.Reconnecting += Reconnecting; _connection.Reconnected += Reconnected; _connection.Closed += Disconnected; ServicePointManager.DefaultConnectionLimit = 10; await _connection.Start(); }
public bool Remove(string connectionId) { lock (_syncObj) { IOnlineClient client; var isRemoved = _clients.TryRemove(connectionId, out client); if (isRemoved) { var user = client.ToUserIdentifierOrNull(); if (user != null && !this.IsOnline(user)) { UserDisconnected.InvokeSafely(this, new OnlineUserEventArgs(user, client)); } ClientDisconnected.InvokeSafely(this, new OnlineClientEventArgs(client)); } return(isRemoved); } }
public virtual bool Remove(string connectionId) { lock (SyncObj) { IOnlineClient client; var isRemoved = Clients.TryRemove(connectionId, out client); if (isRemoved) { var user = client.ToUserIdentifierOrNull(); if (user != null && !this.IsOnline(user)) { UserDisconnected.InvokeSafely(this, new OnlineUserEventArgs(user, client)); } ClientDisconnected.InvokeSafely(this, new OnlineClientEventArgs(client)); } _cacheManager.GetCache <string, IOnlineClient>(nameof(IOnlineClient)).Remove(connectionId); return(isRemoved); } }
public virtual bool Remove(string connectionId) { lock (SyncObj) { var result = Store.TryRemove(connectionId, out IOnlineClient client); if (result) { if (UserDisconnected != null) { var context = client.ToClientContextOrNull(); if (context != null && !this.IsOnline(context)) { UserDisconnected.Invoke(this, new OnlineUserEventArgs(context, client)); } } ClientDisconnected?.Invoke(this, new OnlineClientEventArgs(client)); } return(result); } }
public void DisconnectUser(object userKey) { var tcpClient = userKey as TcpClient; if (tcpClient != null && connected.Contains(tcpClient)) { connected.Remove(tcpClient); byte[] arr = new byte[1] { 0 }; try { SendMessage(tcpClient, new DataBuffer(arr)); } catch { } UserDisconnected?.Invoke(this, new ProviderUserEventArgs() { UserKey = tcpClient, }); tcpClient.Close(); clientList.Remove(tcpClient); } }
public void UpdateClient() { NetIncomingMessage inc = null; while (client != null && (inc = client.ReadMessage()) != null) { switch (inc.MessageType) { case NetIncomingMessageType.StatusChanged: switch (inc.SenderConnection.Status) { case NetConnectionStatus.Connected: UserConnected?.Invoke(this, new ProviderUserEventArgs()); break; case NetConnectionStatus.Disconnected: UserDisconnected?.Invoke(this, new ProviderUserEventArgs()); break; } break; case NetIncomingMessageType.Data: var bytes = inc.ReadBytes(inc.LengthBytes); DataReceived?.Invoke(this, new ProviderDataEventArgs(null, true, new DataBuffer(bytes), inc.LengthBytes)); break; case NetIncomingMessageType.Error: case NetIncomingMessageType.ErrorMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.VerboseDebugMessage: Console.WriteLine(DateTime.Now.ToShortTimeString() + "Lidgren Client" + inc.ReadString()); break; } } }
public void Init() { if (_isInitialized) { return; } _isInitialized = true; _hubConnection = new HubConnection(Secrets.GoHubUrl); _goHub = _hubConnection.CreateHubProxy("GoHub"); _goHub.On(nameof(UserConnected), (UserPrefs user) => UserConnected?.Invoke(user)); _goHub.On(nameof(UserDisconnected), (string user) => UserDisconnected?.Invoke(user)); _goHub.On(nameof(LobbyPost), (Post post) => LobbyPost?.Invoke(post)); _goHub.On(nameof(GamePost), (Post post) => GamePost?.Invoke(post)); _goHub.On(nameof(GameRequested), (string name) => GameRequested?.Invoke(name)); _goHub.On(nameof(GameRequestDeclined), (string name) => GameRequestDeclined?.Invoke(name)); _goHub.On(nameof(GameRequestCancelled), (string name) => GameRequestCancelled?.Invoke(name)); _goHub.On(nameof(GameRequestAccepted), (string name) => GameRequestAccepted?.Invoke(name)); _goHub.On(nameof(GameAborted), (string name) => GameAborted?.Invoke(name)); }
/// <summary> /// Read and prosess data from stream /// </summary> private void ClientStreamReadData() { try { using (Stream stream = _server.GetStream()) { BinaryReader streamReader = new BinaryReader(stream); while (true) { PacketType packetType = (PacketType)streamReader.ReadInt32(); switch (packetType) { case PacketType.Negotiation: NegotiationPacket negotiationPacket = new NegotiationPacket(stream); negotiationPacket.Receive(); Login = negotiationPacket.Login; _loginEvents[0].Set(); _registerEvents[0].Set(); lock (_rooms) { Room mainRoom = new Room(Room.MainRoomId, Room.MainRoomName, new HashSet <string>()); _rooms.Add(Room.MainRoomId, mainRoom); } _context.Post(p => NegotiationCompleted?.Invoke(this, EventArgs.Empty), null); break; case PacketType.SimpleMessage: SimpleMessagePacket simpleMessagePacket = new SimpleMessagePacket(stream); simpleMessagePacket.Receive(); OnSimpleMessageReceived(simpleMessagePacket); break; case PacketType.Error: ErrorPacket errorPacket = new ErrorPacket(stream); errorPacket.Receive(); _context.Post(p => ErrorOccurred?.Invoke(this, new ErrorOccurredEventHandlerArgs(errorPacket.ErrorMessage, errorPacket.ErrorType)), null); break; case PacketType.UserConnect: UserConnectPacket userConnectPacket = new UserConnectPacket(stream); userConnectPacket.Receive(); lock (_users) { _users[userConnectPacket.UserLogin] = userConnectPacket.UserInfo; } _context.Post(p => UserConnected?.Invoke(this, new UserConnectedEventHandlerArgs(userConnectPacket.UserLogin, userConnectPacket.UserInfo)), null); break; case PacketType.UserDisconnect: UserDisconnectPacket userDisconnectPacket = new UserDisconnectPacket(stream); userDisconnectPacket.Receive(); lock (_users) { _users.Remove(userDisconnectPacket.UserLogin); } _context.Post(p => UserDisconnected?.Invoke(this, new UserDisconnectedEventHandlerArgs(userDisconnectPacket.UserLogin)), null); break; case PacketType.CreateNewRoom: CreateNewRoomPacket createNewRoomPacket = new CreateNewRoomPacket(stream); createNewRoomPacket.Receive(); lock (_rooms) { _rooms.Add(createNewRoomPacket.Room.Id, createNewRoomPacket.Room); } _context.Post(p => NewRoomCreated?.Invoke(this, new NewRoomCreatedEventHandlerArgs(createNewRoomPacket.Room)), null); break; case PacketType.AddUserToRoom: AddUsersToRoomPacket addUsersToRoomPacket = new AddUsersToRoomPacket(stream); addUsersToRoomPacket.Receive(); lock (_rooms) { foreach (string user in addUsersToRoomPacket.Users) { _rooms[addUsersToRoomPacket.RoomId].Members.Add(user); } } _context.Post(p => RoomsUpdated?.Invoke(this, new RoomsUpdatedEventHandlerArgs()), null); break; case PacketType.RemoveUserFromRoom: RemoveUserFromRoomPacket removeUserFromRoomPacket = new RemoveUserFromRoomPacket(stream); removeUserFromRoomPacket.Receive(); lock (_rooms) { Room room; if (_rooms.TryGetValue(removeUserFromRoomPacket.RoomId, out room)) { room.Members.Remove(removeUserFromRoomPacket.User); } } _context.Post(p => RoomsUpdated?.Invoke(this, new RoomsUpdatedEventHandlerArgs()), null); break; } } } } catch (Exception) { _server.Close(); _server.Client.Dispose(); } }