Exemplo n.º 1
0
        //IServerConnectoin<ClientDefault, ClientPingDefault>
        public void DispatchOrdersToClients(IServerConnectoin <ClientDefault, ClientPingDefault> conn, int frame, byte[] data)
        {
            var from = conn != null ? conn.PlayerIndex : 0;

            foreach (var c in Conns.Except(conn).ToList())
            {
                DispatchOrdersToClient(c, from, frame, data);
            }
        }
Exemplo n.º 2
0
 public void DispatchOrders(IServerConnectoin <ClientDefault, ClientPingDefault> conn, int frame, byte[] data)
 {
     if (frame == 0 && conn != null)
     {
         //string log = "DispatchOrders InterpretServerOrders PlayerIndex->{0} frame->{1}".F(conn.PlayerIndex, frame);
         //Log.Write("wybserver", log);
         //Console.WriteLine(log);
         InterpretServerOrders(conn, data);
     }
     else
     {
         //string log = "DispatchOrders DispatchOrdersToClients PlayerIndex->{0} frame->{1}".F(conn != null ? conn.PlayerIndex : -1, frame);
         //Log.Write("wybserver", log);
         //Console.WriteLine(log);
         DispatchOrdersToClients(conn, frame, data);
     }
 }
Exemplo n.º 3
0
 void DispatchOrdersToClient(IServerConnectoin <ClientDefault, ClientPingDefault> c, int client, int frame, byte[] data)
 {
     Console.WriteLine("DispatchOrdersToClient frame->{0}".F(frame));
     try
     {
         SendData(c.Socket, BitConverter.GetBytes(data.Length + 4));
         SendData(c.Socket, BitConverter.GetBytes(client));
         SendData(c.Socket, BitConverter.GetBytes(frame));
         SendData(c.Socket, data);
     }
     catch (Exception e)
     {
         DropClient(c);
         //Log.Write("server", "Dropping client {0} because dispatching orders failed: {1}",
         //    client.ToString(CultureInfo.InvariantCulture), e);
     }
 }
Exemplo n.º 4
0
        void InterpretServerOrders(IServerConnectoin <ClientDefault, ClientPingDefault> conn, byte[] data)
        {
            var ms = new MemoryStream(data);
            var br = new BinaryReader(ms);

            try
            {
                while (ms.Position < ms.Length)
                {
                    var so = ServerOrderDefault.Deserialize(br);
                    if (so == null)
                    {
                        return;
                    }
                    InterpretServerOrder(conn, so);
                }
            }
            catch (EndOfStreamException) { }
            catch (NotImplementedException) { }
        }
Exemplo n.º 5
0
 public void SendOrderTo(IServerConnectoin <ClientDefault, ClientPingDefault> conn, string order, string data)
 {
     DispatchOrdersToClient(conn, 0, 0, new ServerOrderDefault(order, data).Serialize());
 }
Exemplo n.º 6
0
 public ClientDefault GetClient(IServerConnectoin <ClientDefault, ClientPingDefault> conn)
 {
     return(LobbyInfo.ClientWithIndex(conn.PlayerIndex));
 }
Exemplo n.º 7
0
 /// <summary>
 /// 解析特殊消息
 /// </summary>
 /// <param name="conn"></param>
 /// <param name="so"></param>
 public void InterpretServerOrder(IServerConnectoin <ClientDefault, ClientPingDefault> conn, IServerOrder so)
 {
     UnityEngine.Debug.LogError("Receive so name->" + so.Name);
 }
Exemplo n.º 8
0
        public void DropClient(IServerConnectoin <ClientDefault, ClientPingDefault> toDrop)
        {
            if (!PreConns.Remove(toDrop))
            {
                Conns.Remove(toDrop);

                var dropClient = LobbyInfo.Clients.FirstOrDefault(c1 => c1.Index == toDrop.PlayerIndex);
                if (dropClient == null)
                {
                    return;
                }

                var suffix = "";
                if (State == ServerState.GameStarted)
                {
                    suffix = dropClient.IsObserver ? " (Spectator)" :  "";
                }
                SendMessage("{0}{1} has disconnected.".F(dropClient.Name, suffix));

                // Send disconnected order, even if still in the lobby
                DispatchOrdersToClients(toDrop, 0, new ServerOrderDefault("Disconnected", "").Serialize());

                LobbyInfo.Clients.RemoveAll(c => c.Index == toDrop.PlayerIndex);
                LobbyInfo.ClientPings.RemoveAll(p => p.Index == toDrop.PlayerIndex);

                // Client was the server admin
                // TODO: Reassign admin for game in progress via an order
                if (Dedicated && dropClient.IsAdmin && State == ServerState.WaitingPlayers)
                {
                    // Remove any bots controlled by the admin
                    LobbyInfo.Clients.RemoveAll(c => c.Bot != null && c.BotControllerClientIndex == toDrop.PlayerIndex);

                    var nextAdmin = LobbyInfo.Clients.Where(c1 => c1.Bot == null)
                                    .MinByOrDefault(c => c.Index);

                    if (nextAdmin != null)
                    {
                        nextAdmin.IsAdmin = true;
                        SendMessage("{0} is now the admin.".F(nextAdmin.Name));
                    }
                }

                DispatchOrders(toDrop, toDrop.MostRecentFrame, new byte[] { 0xbf });

                // All clients have left: clean up
                if (!Conns.Any())
                {
                    foreach (var t in serverTraits.WithInterface <INotifyServerEmpty <ClientDefault, ClientPingDefault> >())
                    {
                        t.ServerEmpty(this);
                    }
                }

                if (Conns.Any() || Dedicated)
                {
                    SyncLobbyClients();
                }

                if (!Dedicated && dropClient.IsAdmin)
                {
                    Shutdown();
                }
            }

            try
            {
                toDrop.Socket.Disconnect(false);
            }
            catch { }
        }