Exemplo n.º 1
0
 private void InvokeStateChangedEvent(NetworkClientState newState)
 {
     if (StateChangedEvent != null)
     {
         StateChangedEvent(newState);
     }
 }
Exemplo n.º 2
0
        public void ResetSocketConnect()
        {
            try
            {
                if (_connectState == NetworkClientState.Closed)
                {
                    return;
                }
                _connectState = NetworkClientState.Closed;
                if (_byteArray != null)
                {
                    _byteArray.Destroy();
                    _byteArray = null;
                }

                if (_socket != null)
                {
                    _socket.Close();
                    _socket = null;
                }

                _msgCounter = 0;
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 3
0
 public bool ResetSocket()
 {
     if (_connectState != NetworkClientState.Closed)
     {
         _connectState = NetworkClientState.Closed;
         try
         {
             if (_socket.Connected)
             {
                 _socket.Shutdown(SocketShutdown.Both);
                 _socket.Dispose();
             }
         }
         finally
         {
             _socket.Close();
             _socket = null;
         }
         _byteArray.Destroy();
         _byteArray = null;
         return(true);
     }
     if (_unityInvoke != null)
     {
         _unityInvoke.Dispose();
         _unityInvoke = null;
     }
     _isCalled = true;
     return(false);
 }
Exemplo n.º 4
0
 /// <summary>
 /// 异步连接服务器
 /// </summary>
 public void Connect(IPEndPoint ipEndPoint)
 {
     if (_connectState != NetworkClientState.Closed)
     {
         if (onConnected != null)
         {
             onConnected.Invoke();
         }
         Debug.Log("客户端非关闭状态,无法进行该操作!");
         return;
     }
     _currentIpEndPoint = ipEndPoint;
     _connectState      = NetworkClientState.Connecting;
     _unityInvoke       = new UnityInvoke("SocketClient");
     Debug.Log("connect Start");
     try
     {
         _msgCounter = 0;
         _socket     = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         _socket.BeginConnect(ipEndPoint, DoConnect, _socket);
     }
     catch (Exception ex)
     {
         Debug.Log(ex.ToString());
     }
 }
Exemplo n.º 5
0
 public void Initialize()
 {
     lock (_lock)
     {
         _state = NetworkClientState.Connecting;
         Connect();
     }
 }
Exemplo n.º 6
0
 private void Connect()
 {
     if (TryConnectClient())
     {
         _state = NetworkClientState.Connected;
         _channel.Listen(_tcpClient);
     }
 }
    NetworkManagerClient()
    {
        mState = NetworkClientState.Uninitialized;
        mDeliveryNotificationManager = new core.DeliveryNotificationManager(true, false);
        mLastRoundTripTime           = 0.0f;

        core.NetGameObject.CreateRpcPacketClient = NetworkManagerClient.CreateRpcPacket;
        core.NetGameObject.SendClient            = NetworkManagerClient.Send;
    }
Exemplo n.º 8
0
 public void Shutdown()
 {
     lock (_lock)
     {
         _state = NetworkClientState.Shutdown;
         _reconnectTimer.Dispose();
         _channel.OnDisconnect -= OnDisconnect;
         CleanupTcpClient();
     }
 }
    public void Init(System.Net.IPEndPoint inServerAddress, string inName, byte inWorldId)
    {
        base.Init(core.World.DefaultWorldCount);

        // client
        NetPeerConfiguration config = new NetPeerConfiguration("game", inServerAddress.AddressFamily);

#if DEBUG
        // 디버깅 환경에서 타임 아웃 처리 조정
        config.ConnectionTimeout = 300f;

        //if (Configuration.Instance.EnableLatencySimulation)
        //{
        //    config.SimulatedLoss = Configuration.Instance.SimulatedLoss;
        //    config.SimulatedRandomLatency = Configuration.Instance.SimulatedRandomLatency;
        //    config.SimulatedMinimumLatency = Configuration.Instance.SimulatedMinimumLatency;
        //    config.SimulatedDuplicatesChance = Configuration.Instance.SimulatedDuplicatesChance;
        //}
#endif

        //config.AutoFlushSendQueue = false;
        mNetPeer = new NetClient(config);
        mNetPeer.Start();
        mDeliveryNotificationManager = new core.DeliveryNotificationManager(true, false);
        mReplicationManagerClient    = new ReplicationManagerClient();

        algo = new NetXorEncryption(GetClient(), "AceTopSecret");

        mLastRoundTripTime     = 0.0f;
        mTimeOfLastInputPacket = 0f;


        mServerAddress       = inServerAddress;
        mState               = NetworkClientState.SayingHello;
        mTimeOfLastHello     = 0.0f;
        mTimeOfLastStartPlay = 0.0f;
        mName           = inName;
        mWorldId        = inWorldId;
        tryConnectCount = 0;

        mAvgRoundTripTime = new core.WeightedTimedMovingAverage(1.0f);

        NetOutgoingMessage hail = GetClient().CreateMessage("hail");
        GetClient().Connect(mServerAddress, hail);
        IsTcp     = false;
        IsUdpOk   = false;
        IsTrySend = true;

        respawn = false;

        // tcp
        SetConnector(inServerAddress);

        LinkedObject.Clear();
    }
Exemplo n.º 10
0
 void HandleWelcomePacket(NetIncomingMessage inInputStream)
 {
     if (mState == NetworkClientState.NCS_SayingHello)
     {
         //if we got a player id, we've been welcomed!
         int playerId = (int)inInputStream.ReadUInt32();
         mPlayerId = playerId;
         mState    = NetworkClientState.NCS_Welcomed;
         core.Engine.sInstance.ServerClientId = mPlayerId;
         //LOG("'%s' was welcomed on client as player %d", mName.c_str(), mPlayerId);
     }
 }
Exemplo n.º 11
0
        public void EstablishConnection(string host, int port)
        {
            State = NetworkClientState.Initializing;
            SocketAsyncEventArgs connectArgs = new SocketAsyncEventArgs();

            connectArgs.RemoteEndPoint = new IPEndPoint(Dns.GetHostEntry(host).AddressList.First(), port);
            connectArgs.Completed     += OnConnectionEstablished;
            if (!Socket.ConnectAsync(connectArgs))
            {
                OnConnectionEstablished(this, connectArgs);
            }
        }
Exemplo n.º 12
0
        void ChangeStateTo(NetworkClientState newState)
        {
            var oldState = State;

            State = newState;
            if (oldState != newState)
            {
                if (StateChanged != null)
                {
                    StateChanged(this, newState);
                }
            }
        }
Exemplo n.º 13
0
        void ClientStateToView()
        {
            NetworkClientState state = raftClient.Client.State;

            State = state.ToString();
            if (raftClient.Client.IsStarted)
            {
                ButtonText = "Stop";
            }
            else
            {
                ButtonText = "Start";
            }
        }
Exemplo n.º 14
0
    void Init(System.Net.IPEndPoint inServerAddress, string inName)
    {
        base.Init(0);

        mServerAddress   = inServerAddress;
        mState           = NetworkClientState.NCS_SayingHello;
        mTimeOfLastHello = 0.0f;
        mName            = inName;

        mAvgRoundTripTime = new core.WeightedTimedMovingAverage(1.0f);

        NetOutgoingMessage hail = GetClient().CreateMessage("hail");

        GetClient().Connect(mServerAddress, hail);
    }
    void HandleWelcomePacket(NetIncomingMessage inInputStream)
    {
        if (mState == NetworkClientState.SayingHello)
        {
            var code = (core.ErrorCode)inInputStream.ReadByte();
            switch (code)
            {
            case core.ErrorCode.Auth:
            {
                // 인증 실패는 즉시 실패 처리한다.
                Debug.Log("Auth error");
                //LobbyController.Instance.CancelWaitStartPlay();
                IsTrySend = false;
            }
            break;

            case core.ErrorCode.Wait:
            {
                Debug.Log("Wait");
                IsUdpOk = true;
            }
            break;

            case core.ErrorCode.Success:
            {
                //if we got a player id, we've been welcomed!
                int playerId = (int)inInputStream.ReadUInt32();
#if USE_TICK_COUNTER
                var tickNumber = inInputStream.ReadUInt32();
                var rtt        = core.Timing.sInstance.GetTimef() - inInputStream.ReadFloat();
                core.World.Instance().mTickCounter.Correct(tickNumber, rtt);
#endif
                isRush    = inInputStream.ReadBoolean();
                mPlayerId = playerId;
                mState    = NetworkClientState.Welcomed;
                core.Engine.sInstance.ServerClientId = mPlayerId;
                Debug.Log($"'{mName}' was welcomed on client as player playerId:{mPlayerId}, isRush:{isRush}");

                //LobbyController.Instance.CancelWaitStartPlay();

                //_isBattleSceneLoadDone = false;

                //ACScrambleCommand.Instance.Execute( new ACCMD_NETWORK_EVENT( ACCMD_NETWORK_EVENT.EACEvent.PACKET_WELCOME_SUCCESS ) );
            }
            break;
            }
        }
    }
Exemplo n.º 16
0
 public void Destroy()
 {
     if (_socket != null)
     {
         _socket.Close();
     }
     if (_byteArray != null)
     {
         _byteArray.Destroy();
         _byteArray = null;
     }
     if (_unityInvoke != null)
     {
         _unityInvoke.Dispose();
     }
     _isCalled     = true;
     _connectState = NetworkClientState.Closed;
 }
Exemplo n.º 17
0
        void OnConnectionEstablished(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                State = NetworkClientState.Disconnected;
                return;
            }
            NetworkPacketSerializer.WritePacket(new JoinRequestPacket(), Socket);
            var packet  = NetworkPacketSerializer.ReadPacket(Socket);
            var welcome = packet as WelcomePacket;

            if (packet == null)
            {
                throw new Exception("Expected a welcome packet but received something else!");
            }
            Playground = new Playground(welcome.Map);
            RaiseMapChanged(welcome.Map);
            State = NetworkClientState.Ready;
        }
Exemplo n.º 18
0
        private void OnDisconnect(object sender)
        {
            lock (_lock)
            {
                if (_state != NetworkClientState.Connected)
                {
                    return;
                }
                if (sender != _tcpClient)
                {
                    return;
                }

                _state = NetworkClientState.Connecting;
                CleanupTcpClient();
                _tcpClient = new TcpClient();
                Connect();
            }
        }
Exemplo n.º 19
0
        private void DoConnect(IAsyncResult ar)
        {
            System.Net.Sockets.Socket client = null;
            try
            {
                client = (System.Net.Sockets.Socket)ar.AsyncState;
                client.EndConnect(ar);
                _connectState = NetworkClientState.Connected;
                _msgCounter   = 0;
                Debug.Log("connect success!");
                if (_byteArray == null)
                {
                    _byteArray = new ByteArray();
                }
                _unityInvoke.Call((int p) =>
                {
                    if (onConnected != null)
                    {
                        onConnected.Invoke();
                    }
                }, 0);
            }
            catch (SocketException ex)
            {
                DoConnectError(ex);
                return;
            }

            try
            {
                StateObject obj = new StateObject(client);
                client.BeginReceive(obj.Buffer, 0, obj.Size, 0, DoReceive, obj);
            }
            catch (SocketException ex)
            {
                DoNetworkError(ex);
            }
        }
    void HandleStartPlayPacket(NetIncomingMessage inInputStream)
    {
        if (mState == NetworkClientState.Welcomed)
        {
            var code = (core.ErrorCode)inInputStream.ReadByte();
            switch (code)
            {
            case core.ErrorCode.Success:
            {
                //if we got a player id, we've been welcomed!
                int playerId = (int)inInputStream.ReadUInt32();
                mPlayerId = playerId;
                mState    = NetworkClientState.Play;
                core.Engine.sInstance.ServerClientId = mPlayerId;
                Debug.Log($"'{mName}' was welcomed on client as player {mPlayerId}");
            }
            break;

            default:
                Debug.LogError($"HandleStartPlayPacket error {code}");
                break;
            }
        }
    }
Exemplo n.º 21
0
 public void Start()
 {
     CurrentConnection = new Connection(this);
     State             = new NetworkClientState();
 }
Exemplo n.º 22
0
 void Client_StateChanged(object sender, NetworkClientState e)
 {
     ClientStateToView();
 }
Exemplo n.º 23
0
 NetworkManagerClient()
 {
     mState = NetworkClientState.NCS_Uninitialized;
     mDeliveryNotificationManager = new core.DeliveryNotificationManager(true, false);
     mLastRoundTripTime           = 0.0f;
 }
Exemplo n.º 24
0
 public SocketClient()
 {
     _msgCounter = 0;
     // ipEndPoint = new IPEndPoint(IPAddress.Parse(SocketManager.SeverURL), SocketManager.Port);
     _connectState = NetworkClientState.Closed;
 }
Exemplo n.º 25
0
 private void NetworkClientStateChangedEvent(NetworkClientState newState)
 {
     state = newState;
 }
Exemplo n.º 26
0
 public NetworkClient()
 {
     State  = NetworkClientState.Disconnected;
     Socket = new Socket(AddressFamily.InterNetwork | AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.IP);
 }
Exemplo n.º 27
0
 private void SetState(NetworkClientState state)
 {
     this.state = state;
     InvokeStateChangedEvent(state);
 }