private static void BoltSocket() { _serverSocket = new ServerSocket(); _serverSocket.AddMessageHandler<RequestMessage>(RequestHandler); _serverSocket.Bind("tcp://127.0.0.1:9900"); Console.ReadLine(); _serverSocket.Close(); }
/// <summary> /// Server message handler. /// </summary> /// <param name="server">The server.</param> /// <param name="serverMessage">The server message we received from client.</param> /// <param name="sender">The client we received server message from.</param> public static void HandleServerMessage(ServerSocket server, ServerMessage serverMessage, Client sender) { switch (serverMessage.Type) { //Handle new user connected. case ServerMessageType.NewUserConnected: sender.User = (User)serverMessage.Content; var clients = server.Clients.Where(c => c.User != sender.User); foreach (var client in clients) { //Add chat with new user to every connected user. serverMessage.Content = sender.User; serverMessage.Receiver = client.User; client.SendMessage(serverMessage); } //Add chats with every connected user to new user's chatlist. foreach (var client in clients) { //Franckly, I don't know why it doesn't work without sleep. //I daresay this is due to the fact that the sender doesn't have enought time to process packets. Thread.Sleep(50); serverMessage.Content = client.User; serverMessage.Receiver = sender.User; sender.SendMessage(serverMessage); } break; //Handle user disconnect. case ServerMessageType.UserDisconected: sender.Socket.Close(); server.Clients.Remove(sender); foreach (var client in server.Clients) { serverMessage.Receiver = client.User; client.SendMessage(serverMessage); } break; } }
/// <summary> /// Message handler. /// </summary> /// <param name="server">The server.</param> /// <param name="message">The message we received from client.</param> public static void HandleMessage(ServerSocket server, Message message) { //Sender null check. if (message.Sender is null) { return; } //Receiver null check. if (message.Receiver is null) { //Serches specified user in clients list. var sender = GetClient(server, message.Sender); //Sends notification to sender about unspecified reseiver. var answer = new ServerMessage(ServerMessageType.UnspecifiedReceiver, "Receiver was null.", message.Sender); sender.SendMessage(answer); return; } //After all checks simply send message. var client = GetClient(server, message.Receiver); if (client is { })
public static void Main(string[] args) { if (args.Length > 0) { BotCount = int.Parse(args[0]); } ReceiveQueue.Start(PacketHandler.Handle); ServerSocket.Start(13338); Console.WriteLine("Server running!"); World.Systems.Add(new GCMonitor(true, false)); World.Systems.Add(new MoveSystem(true, false)); for (int i = 0; i < BotCount; i++) { ref var npc = ref World.CreateEntity(100_000 + i); npc.Add(new NetworkComponent(100_000 + i)); npc.Add <PositionComponent>(); npc.Add <DestinationComponent>(); npc.Add(new SpeedComponent(16)); npc.Add <VelocityComponent>(); Collections.Npcs.TryAdd(100_000 + i, new Npc(100_000 + i)); }
public void Stop() { ConnectedUsers.Clear(); ServerSocket.Stop(); DisplayColoredText("Сервер закрыт", ConsoleColor.Red); }
static void Main(string[] args) { ServerSocket server = new ServerSocket(1234); server.start(); }
void Start() { serverSocket = new NetSocketServerSocket(); serverSocket.listen(8888); Console.WriteLine("Listening"); while (true) { DataSocket s = serverSocket.handle(); if (s != null) { Console.WriteLine("Connection aquired"); Connection c = new Connection(ConnectorIdGenerator.Next(), s); Connections.Add(c.ConnectionId, c); } foreach (Connection connection in Connections.Values) { connection.DataSocket.handle(); var msg = connection.DataSocket.getMessage(); if (msg != null) { BinaryReader reader = new BinaryReader(new MemoryStream(msg)); int cmd = reader.ReadInt16(); Console.WriteLine($"Got msg command: {cmd}"); if (cmd == 0) { if (ConnectionState.EnsureConnectionNotAuthenticated(this, connection.ConnectionId)) { string name = reader.ReadString(); string password = reader.ReadString(); connection.State = ConnectionState.Authenticated; connection.Username = name; connection.LobbyId = uint.MaxValue; connection.GameId = uint.MaxValue; Console.WriteLine($"Player authenticated id:{connection.ConnectionId} name:{name}"); { var mem = new MemoryStream(); BinaryWriter writer = new BinaryWriter(mem); writer.Write((System.Int16) 0); writer.Write(true); writer.Write((System.UInt32)connection.ConnectionId); writer.Write("Player 1"); writer.Flush(); connection.DataSocket.send(mem.ToArray()); } //send lobbies { var mem = new MemoryStream(); BinaryWriter writer = new BinaryWriter(mem); writer.Write((System.Int16) 1); writer.Write((System.Int16) 1); writer.Write((System.UInt32)Lobbies.Values.Count); foreach (var l in Lobbies.Values) { writer.Write((System.UInt32)l.LobbyId); writer.Write(l.Name); writer.Write((System.UInt32)l.GameType); } writer.Flush(); connection.DataSocket.send(mem.ToArray()); } } } else if (cmd == 1) { string name = reader.ReadString(); uint game = reader.ReadUInt32(); LobbyController.CreateLobby(this, connection.ConnectionId, name, game); } else if (cmd == 2) { LobbyController.StartLobby(this, connection.ConnectionId); } else if (cmd == 3) { uint lobbyId = reader.ReadUInt32(); LobbyController.JoinLobby(this, connection.ConnectionId, lobbyId); } else if (cmd == 4) { LobbyController.LeaveLobby(this, connection.ConnectionId); } else if (cmd == 5) { if (connection.GameId != uint.MaxValue) { Games[connection.GameId].RPCHandler.Read(connection.ConnectionId, reader); } //Send to all sockets { Console.WriteLine("Broadcasting rpc"); var mem = new MemoryStream(); BinaryWriter writer = new BinaryWriter(mem); writer.Write((System.Int16) 6); writer.Write((System.UInt32)connection.ConnectionId); writer.Write(msg, 2, msg.Length - 2); writer.Flush(); foreach (var connId in Games[connection.GameId].Players) { Connections[connId].DataSocket.send(mem.ToArray()); } } } else { connection.Error = true; connection.ErrorMessage = "Network error"; } } } foreach (var game in Games.Values) { /*game.onTick(); * if (game.isDone()) * { * * }*/ //GameController.Handle(); game.GameInstance.OnTick(); { var mem = new MemoryStream(); BinaryWriter writer = new BinaryWriter(mem); writer.Write((System.Int16) 7); writer.Flush(); foreach (var connId in game.Players) { Connections[connId].DataSocket.send(mem.ToArray()); } } if (game.GameInstance.IsDone()) { //Stop game } } Thread.Sleep(200); } }
// Threaded task, this ensures we can still use this application ASYNC meaning it wont block any input public Task<String> Listen(String ip, uint port) { Task<String> threaded_listener = new Task<String> (() => { ServerSocket s = new ServerSocket(ip, port, _directory); return s.Recieve(); } ); threaded_listener.Start(); return threaded_listener; }