Exemplo n.º 1
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public bool send(INetMsg message)
        {
            if (!isConnected)
            {
                return(false);
            }
            NetMsg msg = (NetMsg)message;

            if (msg == null)
            {
                return(false);
            }

            //序列化
            msg.serialization();

            NetworkStream stream = _tcp.GetStream();

            if (stream.CanWrite)
            {
                try
                {
                    msg.buffer.position = 0;
                    stream.BeginWrite(msg.buffer.array, msg.buffer.position, msg.buffer.limit - msg.buffer.position, new AsyncCallback(AsyncWrite), null);
                }
                catch (Exception ex)
                {
                    OnException(ex);
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
    public void ReceiveMsg(INetMsg msg, int connectionId)
    {
        var stationSelect = msg as StationSelectMsg;

        if (stationSelect != null)
        {
            this.ReceivedStationSelect(connectionId, stationSelect);
        }

        var startBattleMsg = msg as StartBattleMsg;

        if (startBattleMsg != null)
        {
            CoreNetwork.Instance.StopHostBroadcast();

            switch (this.stationSelected)
            {
            case Stations.Pilot:
            {
                SceneManager.LoadScene("PilotStation");
                break;
            }

            case Stations.SensorsAndWeapons:
            {
                SceneManager.LoadScene("SensorsAndWeaponsStation");
                break;
            }

            default:
                break;
            }
        }
    }
Exemplo n.º 3
0
 public void Send(INetMsg nm)
 {
     if (m_NC != null && m_NC.Connected)
     {
         m_NC.send(nm);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 发送数据
 /// </summary>
 /// <param name="buffer"></param>
 /// <returns></returns>
 public bool send(INetMsg message)
 {
     if (_nowSession == null)
     {
         return(false);
     }
     return(_nowSession.send(message));
 }
Exemplo n.º 5
0
 public static byte[] Serialize(INetMsg msg)
 {
     var networkWriter = new NetworkWriter();
     uint index = GetMsgIndexFromType( msg.GetType() );
     networkWriter.WritePackedUInt32( index );
     ((MessageBase)msg).Serialize(networkWriter);
     return networkWriter.AsArray();
 }
 public void ReceiveMsg(INetMsg msg, int _)
 {
     var energyCons = msg as EnergyConsumtionMsg;
     if (energyCons != null && energyCons.Station == this.Station)
     {
         this.Text.text = string.Format("{0}", energyCons.EnergyConsumed.ToString("F1"));
     }
 }
    public void ReceiveMsg(INetMsg msg, int _)
    {
        var energyCons = msg as EnergyConsumtionMsg;

        if (energyCons != null && energyCons.Station == this.Station)
        {
            this.Text.text = string.Format("{0}", energyCons.EnergyConsumed.ToString("F1"));
        }
    }
Exemplo n.º 8
0
        public static byte[] Serialize(INetMsg msg)
        {
            var  networkWriter = new NetworkWriter();
            uint index         = GetMsgIndexFromType(msg.GetType());

            networkWriter.WritePackedUInt32(index);
            ((MessageBase)msg).Serialize(networkWriter);
            return(networkWriter.AsArray());
        }
    public void ReceiveMsg(INetMsg msg, int _)
    {
        var scanTargetMsg = msg as ScanTargetMsg;

        if (scanTargetMsg == null)
        {
            return;
        }

        Debug.Log("[SensorsAndWeapons] Received :" + scanTargetMsg);
        switch (scanTargetMsg.Action)
        {
        case ScanTargetMsg.Type.Add:
        {
            var newSensorTargetObj = (GameObject)Instantiate(this.SensorsTargetPrefab, Vector3.zero, Quaternion.identity);
            newSensorTargetObj.GetComponent <SensorsTarget>().Target = scanTargetMsg.ScanTarget;
            newSensorTargetObj.transform.SetParent(this.TargetsTrans);
            newSensorTargetObj.transform.localScale = Vector3.one;
            break;
        }

        case ScanTargetMsg.Type.Remove:
        {
            foreach (Transform sesnorTargetObj in this.TargetsTrans)
            {
                var target = sesnorTargetObj.GetComponent <SensorsTarget>().Target;
                if (target.ID == scanTargetMsg.ScanTarget.ID)
                {
                    Destroy(sesnorTargetObj.gameObject);
                    break;
                }
            }
            break;
        }

        case ScanTargetMsg.Type.Update:
        {
            foreach (Transform sensorTargetObj in this.TargetsTrans)
            {
                var sensorTarget = sensorTargetObj.GetComponent <SensorsTarget>();
                if (sensorTarget.Target.ID == scanTargetMsg.ScanTarget.ID)
                {
                    sensorTarget.Target = scanTargetMsg.ScanTarget;
                    break;
                }
            }
            break;
        }

        default:
            break;
        }

        this.OrderTargets();
    }
Exemplo n.º 10
0
 void SetAllegiance(INetMsg msg, int connectionId)
 {
     if (msg is StationSelectMsg)
     {
         this.allegiances[connectionId] = msg.Allegiance;
     }
     else
     {
         msg.Allegiance = this.allegiances[connectionId];
     }
 }
Exemplo n.º 11
0
 public void ReceiveMsg(INetMsg msg, int connectionId)
 {
     var thrusterMsg = msg as ThrusterMsg;
     if (thrusterMsg != null)
     {
         if (thrusterMsg.Allegiance != this.Allegiance)
         {
             return;
         }
         var thruster = this.Thrusters[(int)thrusterMsg.type];
         thruster.IsActive = thrusterMsg.activate;
     }
 }
Exemplo n.º 12
0
    public void ReceiveMsg(INetMsg msg, int connectionId)
    {
        var thrusterMsg = msg as ThrusterMsg;

        if (thrusterMsg != null)
        {
            if (thrusterMsg.Allegiance != this.Allegiance)
            {
                return;
            }
            var thruster = this.Thrusters[(int)thrusterMsg.type];
            thruster.IsActive = thrusterMsg.activate;
        }
    }
    public void ReceiveMsg(INetMsg msg, int _)
    {
        var scanTargetMsg = msg as ScanTargetMsg;
        if (scanTargetMsg == null)
            return;

        Debug.Log("[SensorsAndWeapons] Received :" + scanTargetMsg);
        switch (scanTargetMsg.Action)
        {
            case ScanTargetMsg.Type.Add:
            {
                var newSensorTargetObj = (GameObject)Instantiate(this.SensorsTargetPrefab, Vector3.zero, Quaternion.identity);
                newSensorTargetObj.GetComponent<SensorsTarget>().Target = scanTargetMsg.ScanTarget;
                newSensorTargetObj.transform.SetParent( this.TargetsTrans );
                newSensorTargetObj.transform.localScale = Vector3.one;
                break;
            }
            case ScanTargetMsg.Type.Remove:
            {
                foreach(Transform sesnorTargetObj in this.TargetsTrans)
                {
                    var target = sesnorTargetObj.GetComponent<SensorsTarget>().Target;
                    if (target.ID == scanTargetMsg.ScanTarget.ID)
                    {
                        Destroy( sesnorTargetObj.gameObject );
                        break;
                    }
                }
                break;
            }
            case ScanTargetMsg.Type.Update:
            {
                foreach(Transform sensorTargetObj in this.TargetsTrans)
                {
                    var sensorTarget = sensorTargetObj.GetComponent<SensorsTarget>();
                    if (sensorTarget.Target.ID == scanTargetMsg.ScanTarget.ID)
                    {
                        sensorTarget.Target =  scanTargetMsg.ScanTarget;
                        break;
                    }
                }
                break;
            }
            default:
                break;
        }

        this.OrderTargets();
    }
Exemplo n.º 14
0
        public void Send(INetMsg msg)
        {
            if (this.isHost)
            {
                Debug.LogFormat("[CoreNetwork] [HOST] Sending to clients : {0}", msg);
            }
            else
            {
                Debug.LogFormat("[CoreNetwork] [Client] Sending to host : {0}", msg);
            }

            byte[] data = MessageHandler.Serialize(msg);

            this.SendToAll(data);
        }
Exemplo n.º 15
0
        void PushMessage(int connectionId, INetMsg msg)
        {
            Debug.LogFormat("[CoreNetwork] Received message from {0} , {1}, {2}", connectionId, msg, msg.Allegiance);

            if (!this.isHost && msg.Allegiance != Global.Allegiance)
            {
                Debug.LogFormat("[CoreNetwork] Message ignored, different allegiance");
                return;
            }

            foreach (var receiver in this.receivers)
            {
                receiver.ReceiveMsg(msg, connectionId);
            }
        }
Exemplo n.º 16
0
 public void ReceiveMsg(INetMsg msg, int connectionId)
 {
 }
Exemplo n.º 17
0
 void SetAllegiance(INetMsg msg, int connectionId)
 {
     if (msg is StationSelectMsg)
     {
         this.allegiances[connectionId] = msg.Allegiance;
     }
     else
     {
         msg.Allegiance = this.allegiances[connectionId];
     }
 }
Exemplo n.º 18
0
 public void Send(INetMsg msg)
 {
 }
Exemplo n.º 19
0
 public void ReceiveMsg(INetMsg msg, int connectionId)
 {
 }
Exemplo n.º 20
0
 public void Send(INetMsg msg){}
Exemplo n.º 21
0
        void PushMessage(int connectionId, INetMsg msg)
        {
            Debug.LogFormat("[CoreNetwork] Received message from {0} , {1}, {2}", connectionId, msg, msg.Allegiance);

            if (!this.isHost && msg.Allegiance != Global.Allegiance)
            {
                Debug.LogFormat("[CoreNetwork] Message ignored, different allegiance");
                return;
            }

            foreach(var receiver in this.receivers)
            {
                receiver.ReceiveMsg(msg, connectionId);
            }
        }
Exemplo n.º 22
0
    public void ReceiveMsg(INetMsg msg, int connectionId)
    {
        var stationSelect = msg as StationSelectMsg;
        if (stationSelect != null)
        {
            this.ReceivedStationSelect(connectionId, stationSelect);
        }

        var startBattleMsg = msg as StartBattleMsg;
        if (startBattleMsg != null)
        {
            CoreNetwork.Instance.StopHostBroadcast();

            switch (this.stationSelected)
            {
                case Stations.Pilot:
                {
                    SceneManager.LoadScene("PilotStation");
                    break;
                }
                case Stations.SensorsAndWeapons:
                {
                    SceneManager.LoadScene("SensorsAndWeaponsStation");
                    break;
                }
                default:
                    break;
            }
        }

    }
Exemplo n.º 23
0
        public void Send(INetMsg msg)
        {
            if (this.isHost)
            {
                Debug.LogFormat("[CoreNetwork] [HOST] Sending to clients : {0}", msg);
            }
            else
            {
                Debug.LogFormat("[CoreNetwork] [Client] Sending to host : {0}", msg);
            }

            byte[] data = MessageHandler.Serialize(msg);

            this.SendToAll( data );
        }