/* Подпрограмма проверки нового клиента */ private bool IsPossibleToConnectClient(CClient newClient, out MessageType errorType) { string messageForOthers; int numOfNewClient; if (IsServerClosed) { errorType = MessageType.ServerClosed; return(false); } if (AmoOfConnectedClients >= maxAmoOfConnectedClients) { errorType = MessageType.ServerOverloaded; return(false); } if (IsNameConflict(newClient.ClientName)) { errorType = MessageType.NameConflict; return(false); } AmoOfConnectedClients++; numOfNewClient = AmoOfConnectedClients - 1; clients.Add(newClient); messageForOthers = String.Format(MESSG_CLIENT_CONNECTED_TO_SERVER, clients[numOfNewClient].ClientName); UI.ShowText(messageForOthers, clients[numOfNewClient].ID); UI.AddClientToList(clients[numOfNewClient].ClientName, clients[numOfNewClient].ID); errorType = MessageType.ConnectionConfirmed; return(true); }
/* Подпрограмма отправки новому пользователю * таблицы цветов */ public void SendPalette(CClient receiver) { Color[] curPalette = CColorsLoader.GetInstance().GetCurrentPalette(); receiver.Writer.Write(curPalette.Length); for (int i = 0; i < curPalette.Length; i++) { receiver.Writer.Write(curPalette[i].R); receiver.Writer.Write(curPalette[i].G); receiver.Writer.Write(curPalette[i].B); } }
/* Подпрограмма отправки новому пользователю * списка всех клиентов сервера */ public void SendClientsList(CClient receiver) { receiver.Writer.Write((byte)(clients.Count - 1)); for (int i = 0; i < clients.Count; i++) { if (clients[i].ID != receiver.ID) { receiver.Writer.Write(clients[i].ID); receiver.Writer.Write(clients[i].ClientName); } } }
/* Подпрограмма для обработки запроса на подключение */ private void TryToConnectNewClient(Socket newClientSocket) { sbyte newClientID = idManager.GetID(); CClient newClient = new CClient(newClientSocket, this, newClientID, UI); newClient.GetClientName(); MessageType errorType; if (IsPossibleToConnectClient(newClient, out errorType)) { newClient.ConfirmConnection(); newClient.Start(); UI.UpdateServerInfo(); } else { newClient.RejectConnection(errorType); idManager.FreeID(newClientID); } }