public void OnReading() { if (receiver.Connected) { var stream = receiver.GetStream(); try { Utils.ReadBytes(stream, delegate(byte[] message) { OnMessage?.Invoke(this, message); OnReading(); stream.Flush(); }); } catch (Exception e) { ConsoleMessenger.Log(ConsoleMessenger.Prefix.Error, "Close connection, because - " + e.Message); Close(); } } else { Close(); } }
private void OnClientAccepted(IAsyncResult ar) { try { var connected = listener.EndAcceptTcpClient(ar); var client = new ClientManager(connected, this); client.OnMessage += OnMessage; lock (connectedClients) { connectedClients.Add(client); client.OnReading(); ConsoleMessenger.Log(ConsoleMessenger.Prefix.Message, "Added new client"); } } catch (Exception e) { if (e.GetType() == typeof(SocketException) && ((SocketException)e).ErrorCode == 10004) { ConsoleMessenger.Log(ConsoleMessenger.Prefix.Error, e.Message); } else { throw; } } }
private Server() { if (Instance != null) { ConsoleMessenger.Log(ConsoleMessenger.Prefix.Error, "Instance Already Created"); return; } ConsoleMessenger.Log(ConsoleMessenger.Prefix.Message, "Server Started"); DbCon = DbConnection.Instance(); serverThread = new Thread(FactoryTcpListener) { Priority = ThreadPriority.AboveNormal }; broadcastThread = new Thread(delegate() { foreach (var seconds in BroadCastAboutServer()) { Thread.Sleep(seconds * 1000); } }); serverThread.Start(); broadcastThread.Start(); }
private void InitClientsThreads() { writingThread = new Thread(delegate() { while (true) { try { lock (connectedClients) { foreach (var client in connectedClients) { client.OnSending(); } } } catch (Exception e) { ConsoleMessenger.Log(ConsoleMessenger.Prefix.Error, e.Message); Thread.Sleep(100); } Thread.Sleep(50); } }); writingThread.Start(); }
private void OnSaveRequest(ClientManager clientManager, Message message) { var texts = Encoding.Unicode.GetString(message.Value).Split(';'); try { DbConnection.Instance().SaveRequest(texts[0], texts[1], DateTime.Now, clientManager.User); } catch (Exception e) { ConsoleMessenger.Log(ConsoleMessenger.Prefix.Error, "Couldn't save changes\n" + e.Message); clientManager.SendMessage(MessageFactory(MessageType.SaveUnsuccessful, null)); return; } ConsoleMessenger.Log(ConsoleMessenger.Prefix.Message, "Saved new request"); clientManager.SendMessage(MessageFactory(MessageType.SaveSuccessful, null)); }
public void Destroy() { lock (connectedClients) { connectedClients.ForEach(delegate(ClientManager client) { ConsoleMessenger.Log(ConsoleMessenger.Prefix.Message, "Client disconnected"); listener.BeginAcceptTcpClient(OnClientAccepted, null); client.Close(); }); connectedClients.Clear(); } listener.Stop(); serverThread.Abort(); writingThread?.Abort(); broadcastThread.Abort(); Instance = null; }
private void FactoryTcpListener() { try { listener = new TcpListener(IPAddress.Any, ServerNormalPort); listener.Start(MaxConnection); for (var i = 0; i < MaxConnection; i++) { listener.BeginAcceptTcpClient(OnClientAccepted, null); } InitClientsThreads(); } catch (Exception e) { ConsoleMessenger.Log(ConsoleMessenger.Prefix.Error, e.Message); } }
private void OnLoginRequest(ClientManager clientManager, Message message) { var data = Encoding.ASCII.GetString(message.Value).Split(';'); var login = data[0]; var password = data[1]; byte[] requests; requests = DbConnection.Instance().GetRequests(login, password); if (requests != null) { clientManager.User = login; ConsoleMessenger.Log(ConsoleMessenger.Prefix.Message, "Logged in " + login); clientManager.SendMessage(MessageFactory(MessageType.LoginSuccessful, requests)); } else { clientManager.SendMessage(MessageFactory(MessageType.LoginUnsuccessful, null)); } }
public void OnSending() { if (!receiver.Connected || Queue.Count <= 0) { return; } try { var stream = receiver.GetStream(); var bytes = Utils.GenerateBytes(Queue.Dequeue()); stream.Write(bytes, 0, bytes.Length); stream.Flush(); } catch (SocketException e) { ConsoleMessenger.Log(ConsoleMessenger.Prefix.Error, "Client was disconnected - " + e.Message); Close(); } }
private void ServerOnMessage(ClientManager clientManager, byte[] data) { ConsoleMessenger.Log(ConsoleMessenger.Prefix.Message, "Received a request"); var message = Message.ToMessage(data); switch (message.Type) { case MessageType.LoginRequest: OnLoginRequest(clientManager, message); break; case MessageType.SignUpRequest: OnSignUpRequest(clientManager, message); break; case MessageType.SaveRequest: OnSaveRequest(clientManager, message); break; default: ConsoleMessenger.Log(ConsoleMessenger.Prefix.Error, "No implementation for case " + message.Type); break; } }
private DbConnection() { Database.EnsureCreated(); var evolve = new Evolve.Evolve(new SqlConnection(CONNECTION_STRING), msg => ConsoleMessenger.Log(ConsoleMessenger.Prefix.System, msg)) { Locations = new[] { $"{DIR}{Path.DirectorySeparatorChar}db{Path.DirectorySeparatorChar}migrations{Path.DirectorySeparatorChar}" }, IsEraseDisabled = true, }; evolve.Migrate(); }