Inheritance: MonoBehaviour
示例#1
0
 public static void AddNetworkEvent(NetworkAction action)
 {
     lock (queueLock)
     {
         actionQueue.Add(action);
     }
 }
示例#2
0
        /// <summary>
        /// Handler the server registers to react to network messages
        /// </summary>
        void ServerOnDataHandler(object source, NetworkEventArgs n)
        {
            NetIncomingMessage msg     = n.GetData();
            NetworkMessageType msgType = n.GetInfo();

            if (msgType == NetworkMessageType.LoadFinish)
            {
                //just ignore the message content for now
                _loadedClients++;
                if (_loadedClients == _targetClientCount)
                {
                    OnAllLoaded();
                }
            }
            else if (msgType == NetworkMessageType.TankControl)
            {
                //read from old message
                int           networkId     = msg.ReadInt32(32);
                NetworkAction networkAction = (NetworkAction)msg.ReadByte();

                //write to new message
                NetOutgoingMessage relayMessage = Server.CreateMessage(n.GetData().LengthBits);
                relayMessage.Write((byte)NetworkMessageType.TankControl);
                relayMessage.Write(networkId, 32);
                relayMessage.Write((byte)networkAction);
                Server.SendToAll(relayMessage, NetDeliveryMethod.ReliableUnordered);
            }
        }
 public AdminChatMessage(NetworkAction networkAction, ChatDestination chatDestination, uint destination, string message)
 {
     this.NetworkAction   = networkAction;
     this.ChatDestination = chatDestination;
     this.Message         = message;
     this.Destination     = destination;
 }
示例#4
0
 public AdminChatMessageEvent(Player player, ChatDestination chatDestination, NetworkAction action, string msg)
 {
     this.Player          = player;
     this.Message         = msg;
     this.ChatDestination = chatDestination;
     this.NetworkAction   = action;
 }
示例#5
0
    public void ChangeRobot(NetworkAction action)
    {
        JsonData data           = JsonMapper.ToObject(action.data);
        JsonData robotStructure = data["robot"];

        Debug.Log("RoboStruct : " + robotStructure.ToString());
        if (robotStructure != null)
        {
            int newAiMode = Int32.Parse(robotStructure["TOP"].ToString());
            switch (newAiMode)
            {
            case 0:
                aiMode = AiMode.Aggressive;
                break;

            case 1:
                aiMode = AiMode.Flanking;
                break;

            case 2:
                aiMode = AiMode.Objective;
                break;
            }
        }
    }
            public static Socket ConnectToServer(NetworkAction _call, string hostName)
            {
                System.Diagnostics.Debug.WriteLine("connecting  to " + hostName);

                // Create a TCP/IP socket.
                Socket    socket;
                IPAddress ipAddress;

                Networking.MakeSocket(hostName, out socket, out ipAddress);

                SocketState ss = new SocketState(socket, -1);

                ss.callMe = _call;

                socket.BeginConnect(ipAddress, Networking.DEFAULT_PORT, ConnectedCallback, ss);

                return(socket);

                /**
                 * hostname - the name of the server to connect to
                 *
                 * callMe - a delegate function to be called when a connection is made. Your SpaceWars client will provide this function when it invokes ConnectToServer.
                 *
                 * ConnectToServer should attempt to connect to the server via a provided hostname. It should save the callMe function in a socket state object for use when data arrives.
                 *
                 * It will need to create a socket and then use the BeginConnect method. Note this method takes the "state" object and "regurgitates" it back to you when a connection is made, thus allowing communication between this function and the ConnectedCallback function (below).
                 **/
            }
示例#7
0
 public SocketState(Socket s, int id, NetworkAction d)
 {
     socket  = s;
     this.id = id;
     callMe  = d;
     buffer  = new byte[1024];
     builder = new StringBuilder();
 }
 public PacketServerChatMessage(NetworkAction action, uint clientId, bool selfSend, string message, long data)
 {
     this.NetworkAction = action;
     this.ClientId      = clientId;
     this.Message       = message;
     this.SelfSend      = selfSend;
     this.Data          = data;
 }
示例#9
0
    public static void TriggerEvent(string eventName, NetworkAction arg)
    {
        GameEvent thisEvent = null;

        if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            thisEvent.Invoke(arg);
        }
    }
示例#10
0
 internal static void InitNetwork(NetworkHandlerSystem nhs)
 {
     actionSetO2LevelForCharacter     = nhs.CreateAction <long, float>(HandleSetO2LevelForCharacter);
     actionSetH2LevelForCharacter     = nhs.CreateAction <long, float>(HandleSetH2LevelForCharacter);
     actionSetEnergyLevelForCharacter = nhs.CreateAction <long, float>(HandleSetEnergyLevelForCharacter);
     actionSetO2LevelForPlayer        = nhs.CreateAction <long, float>(HandleSetO2LevelForPlayer);
     actionSetH2LevelForPlayer        = nhs.CreateAction <long, float>(HandleSetH2LevelForPlayer);
     actionSetEnergyLevelForPlayer    = nhs.CreateAction <long, float>(HandleSetEnergyLevelForPlayer);
 }
示例#11
0
 private void SendNotification(NetworkAction whatNetworkAction)
 {
     //we need a second peer for this to send a message to the server itself
     NetOutgoingMessage message = _client.CreateMessage();
     message.Write((byte) NetworkMessageType.TankControl);
     message.Write(Tank.NetworkId, 32);
     message.Write((byte)whatNetworkAction);
     _client.SendMessage(message, _client.ServerConnection, NetDeliveryMethod.ReliableUnordered, 0);
 }
示例#12
0
    void OnJoin(NetworkAction action)
    {
        Debug.Log("OnJoin data " + action.data);
        string ip      = action.senderIp;
        string session = action.senderSession;

        RegisterPlayer(ip, session);
        playerListChanged.Invoke(GetEnemies());
    }
示例#13
0
        private void SendNotification(NetworkAction whatNetworkAction)
        {
            //we need a second peer for this to send a message to the server itself
            NetOutgoingMessage message = _client.CreateMessage();

            message.Write((byte)NetworkMessageType.TankControl);
            message.Write(Tank.NetworkId, 32);
            message.Write((byte)whatNetworkAction);
            _client.SendMessage(message, _client.ServerConnection, NetDeliveryMethod.ReliableUnordered, 0);
        }
        public void CreateNetwork()
        {
            NetworkAction networkAction = new NetworkAction(DatabaseManager.ConnectionFor(dbName), newNetwork, true);

            networkAction.Completed += networkAction_Completed;
            networkAction.RunAsync();
            AutoResetEvent.WaitOne();
            DatabaseManager.ConnectionFor(dbName).LoadCache(DatabaseManager.ConnectionFor(dbName).Session);
            Assert.AreEqual(networks + 1, DatabaseManager.ConnectionFor(dbName).Cache.Networks.Length);
        }
 /// <summary>
 /// Takes a NetworkAction delegate, a, and sets it to this sockets action.
 /// if a is null, sets action to empty lambda.
 /// </summary>
 public void SetNetworkAction(NetworkAction a)
 {
     if (a != null)
     {
         action = a;
     }
     else
     {
         action = x => { };
     }
 }
        /// <summary>
        /// Creates a new ConnectionState object with the user given TcpListener
        /// and NetworkAction delegate.
        ///
        /// If listen is null, throws ArgumentNullException.
        /// If action is null, sets networkAction to an empty lambda.
        /// </summary>
        public ConnectionState(TcpListener listen, NetworkAction networkAction)
        {
            if (listen == null)
            {
                string exceptionMessage = "TcpListener object passed to ConnectionState cannot be null";
                throw new ArgumentNullException(exceptionMessage);
            }

            listener = listen;
            SetNetworkAction(networkAction);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="callMe"></param>
        public static void ServerAwaitingClientLoop(NetworkAction callMe)
        {
            TcpListener listener = new TcpListener(IPAddress.Any, DEFAULT_PORT);

            listener.Start();
            ConnectionState cs = new ConnectionState();

            cs.listener = listener;
            cs.callMe   = callMe;
            listener.BeginAcceptSocket(AcceptNewClient, cs);
        }
示例#18
0
        private void CreateNonBonded()
        {
            XenAPI.Network network = PopulateNewNetworkObj();
            PIF            nic     = pageNetworkDetails.SelectedHostNic;
            long           vlan    = pageNetworkDetails.VLAN;

            NetworkAction action = pageNetworkType.SelectedNetworkType == NetworkTypes.External
                                       ? new NetworkAction(xenConnection, network, nic, vlan)
                                       : new NetworkAction(xenConnection, network, true);

            action.RunAsync();
        }
示例#19
0
    void OnAddRobot(NetworkAction action)
    {
        Debug.Log("OnAddRobot " + action);
        string   name           = players[action.senderIp].name;
        JsonData data           = JsonMapper.ToObject(action.data);
        JsonData robotStructure = data["robot"];

        Debug.Log("RoboStruct : " + robotStructure.ToString());
        string hexCode = data["color"].ToString();

        EnemyManager.instance.CreatePlayer(action.senderIp, name, robotStructure, hexCode);
    }
        public void CreateExternalNetwork()
        {
            Assert.True(DatabaseManager.ConnectionFor(dbName).Cache.PIFs.Length > 0);
            PIF           pif           = DatabaseManager.ConnectionFor(dbName).Cache.PIFs[0];
            NetworkAction networkAction = new NetworkAction(DatabaseManager.ConnectionFor(dbName), newNetwork, pif, 2);

            networkAction.Completed += networkAction_Completed;
            networkAction.RunAsync();
            AutoResetEvent.WaitOne();
            DatabaseManager.RefreshCacheFor(dbName);
            Assert.AreEqual(networks + 1, DatabaseManager.ConnectionFor(dbName).Cache.Networks.Length);
        }
示例#21
0
 void Update()
 {
     lock (queueLock)
     {
         while (actionQueue.Count > 0)
         {
             NetworkAction action = actionQueue[0];
             TriggerEvent("game", action);
             actionQueue.RemoveAt(0);
         }
     }
 }
示例#22
0
        public void receiveServerChat(Packet p)
        {
            NetworkAction action   = (NetworkAction)p.readUint8();
            DestType      dest     = (DestType)p.readUint8();
            long          clientId = p.readUint32();
            String        message  = p.readString();
            long          data     = p.readUint64();

            if (OnChat != null)
            {
                OnChat(action, dest, clientId, message, data);
            }
        }
示例#23
0
        public virtual void AsyncSend(NetworkData a_oData)
        {
            try
            {
                NetworkAction?.StateChanged(State.Sending, new StateObject(this, a_oData));

                NetworkSocket?.BeginSend(a_oData.Buffer, 0, a_oData.DataLength(true), SocketFlags.None, new AsyncCallback(SendCallback), this);
            }
            catch (Exception)
            {
                NetworkAction?.StateChanged(State.Error, new StateObject(this));
            }
        }
示例#24
0
        protected virtual void AcceptConnection()
        {
            try
            {
                NetworkAction?.StateChanged(State.Listening, new StateObject(this));

                m_oNetObject?.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), this);
            }
            catch (Exception e)
            {
                NetworkAction?.StateChanged(State.Error, new StateObject(this, e));
            }
        }
示例#25
0
        /// <summary>
        /// Starts a TcpListener to continuously listen for data from clients on a given socket
        /// </summary>
        /// <param name="callMe"></param>
        public static void ServerAwaitingClientLoop(NetworkAction callMe)
        {
            TcpListener lstn = new TcpListener(IPAddress.Any, 11000);

            lstn.Start();
            ConnectionState cs = new ConnectionState();

            cs.listener = lstn;
            cs.callMe   = callMe;

            // Send AcceptNewClient as a delegate and the new ConnectionState to accept a new client
            lstn.BeginAcceptSocket(AcceptNewClient, cs);
        }
示例#26
0
 public void Awake()
 {
     if (instance == null)
     {
         instance = this;
     }
     else if (instance != this)
     {
         Debug.Log("An instance already exists, destroying this instance");
         Destroy(this);
     }
     networkAction = new NetworkAction();
     InitialisePacketsIDReader();
 }
        public void DeleteNetwork()
        {
            CreateNetwork();
            Network network = FindCreatedNetwork();

            Assert.NotNull(network);
            NetworkAction networkAction = new NetworkAction(DatabaseManager.ConnectionFor(dbName), network, false);

            networkAction.Completed += networkAction_Completed;
            networkAction.RunAsync();
            AutoResetEvent.WaitOne();
            DatabaseManager.RefreshCacheFor(dbName);
            Assert.AreEqual(networks, DatabaseManager.ConnectionFor(dbName).Cache.Networks.Length);
        }
示例#28
0
        /// <summary>
        /// Allows a hostname to resolve to a Socket, getting the IP address from a DHCP server if needed.
        /// </summary>
        /// <param name="callMe"></param>
        /// <param name="hostname"></param>
        /// <returns></returns>
        public static Socket ConnectToServer(NetworkAction callMe, string hostname)
        {
            Socket    socket;
            IPAddress ip;

            MakeSocket("localhost", out socket, out ip);

            SocketState state = new SocketState(socket);

            state.Call = callMe;

            state.sock.BeginConnect(ip, 11000, ConnectedCallback, state);
            return(state.sock);
        }
示例#29
0
        /// <summary>
        /// Moded method to also take an HTTP port
        /// </summary>
        /// <param name="callMe"></param>
        /// <param name="port"></param>
        public static void ServerAwaitingClientLoop(NetworkAction callMe, int port)
        {
            lstn = new TcpListener(IPAddress.Any, Networking.HTTP_PORT);

            lstn.Start();

            NewConnectionState cs = new NewConnectionState(lstn, callMe);

            cs.listener = lstn;

            cs.callMe = callMe;

            lstn.BeginAcceptSocket(AcceptNewClient, cs);
        }
示例#30
0
        /// <summary>
        /// Starts the connection process from a socket to a server. Sets a delegate callback
        /// function which is specified by the client to perform actions desired by the client
        /// once a connection has been made.
        /// </summary>
        /// <param name="callbackFunction"></param>
        /// <param name="hostname"></param>
        /// <returns></returns>
        public static Socket ConnectToServer(NetworkAction callbackFunction, string hostname)
        {
            IPAddress addr;
            Socket    mySocket;

            MakeSocket(hostname, out mySocket, out addr);

            SocketState state = new SocketState(mySocket);

            state.callMe = callbackFunction;

            mySocket.BeginConnect(addr, DEFAULT_PORT, ConnectedToServer, state);
            return(mySocket);
        }
示例#31
0
        /// <summary>
        /// This is the heart of the server code. It starts with a TcpListener for new connections
        /// and passes the listener, along with the callMe delegate, to BeginAcceptSocket as the state parameter.
        /// </summary>
        public static void ServerAwaitingClientLoop(NetworkAction action)
        {
            TcpListener listener = new TcpListener(IPAddress.Any, DEFAULT_PORT);

            listener.Start();

            // setting up the connection state
            ConnectionState state = new ConnectionState();

            state.listener = listener;
            state.callMe   = action;

            listener.BeginAcceptSocket(AcceptNewClient, state);
        }
示例#32
0
 private static string stringify(object source, NetworkAction action)
 {
     return action.ToString() + " " + source.ToString();
 }
示例#33
0
 public NetworkEvent(object source, NetworkAction action)
     : base(source, NetworkEvent.stringify(source, action))
 {
     this._action = action;
 }