Exemple #1
0
    protected void _doConnect(string strHostName, int nPort, Action <bool, Exception> callback)
    {
        if (IsConnected())
        {
            Debugger.Log("Connect failed! Can do connecting only when a connection is disconnected!!!");
            return;
        }

        __onConnect = callback;

        try
        {
            _connectionState = ConnectionState.CONNECTING;

            _hostName = strHostName;
            _port     = nPort;

            _tcpClient = new TcpClient();
            _tcpClient.BeginConnect(strHostName, nPort, _onConnectedCallback, _tcpClient);
        }
        catch (Exception e)
        {
            if (__onConnect != null)
            {
                __onConnect(false, e);
            }
        }
    }
Exemple #2
0
        public void SendNetMessage(NetMessage message)
        {
            if (!IsConnected())
            {
                Debugger.Log("Connection already down, send message failed!"); return;
            }

            try
            {
                if (SendingFilter != null)
                {
                    if (SendingFilter(message))
                    {
                        ByteBuffer buffer = message.Serialize();
                        _socket.Send(buffer.GetInternalBuffer());
                    }
                    else
                    {
                        if (OnSendingFiltered != null)
                        {
                            OnSendingFiltered(message);
                        }
                    }
                }
                else
                {
                    ByteBuffer buffer = message.Serialize();
                    _socket.Send(buffer.GetInternalBuffer());
                }
            }
            catch (Exception e) { Debugger.LogError(e); }
        }
Exemple #3
0
    public void SendNotify(Message pMessage)
    {
        if (!IsConnected())
        {
            Debugger.Log("Connection already down, send message failed!"); return;
        }

        lock (__lock)
        {
            __sendMsgQueue.Enqueue(pMessage);
        }
    }
Exemple #4
0
        ///------------------
        protected void _doConnect(string strHostName, int nPort, Action <bool, Exception> callback)
        {
            if (IsConnected())
            {
                Debugger.Log("Connect failed! Can do connecting only when a connection is disconnected!!!");
                return;
            }

            __onConnect = callback;

            _socket.Connect(strHostName, nPort, (ret, err) =>
            {
                _connectionState = ret ? ConnectionState.CONNECTED : ConnectionState.DISCONNECTED;
                callback(ret, err);
            });
        }
Exemple #5
0
    // Update is called once per frame
    public void Update(float curTimeInSeconds)
    {
        __fCurrentSeconds = curTimeInSeconds;

        lock (__lock)
        {
            /// Processing receiving queue
            foreach (Byte[] rawMsgBytes in __rawMsgQueue)
            {
                bool bNeedDispatch;

                if (OnReceivingFilter != null)
                {
                    bNeedDispatch = OnReceivingFilter(rawMsgBytes, rawMsgBytes.Length);
                }
                else
                {
                    bNeedDispatch = true;
                }

                if (bNeedDispatch)
                {
                    Message message = new Message();
                    message.Deserialize(rawMsgBytes, rawMsgBytes.Length);

                    if (message.IsRPCReturn())
                    {
                        if (_rpcCallMap.ContainsKey(message.RPC_ID))
                        {
                            var rpcCallMsg = _rpcCallMap[message.RPC_ID];
                            _rpcCallMap.Remove(message.RPC_ID);
                            if (rpcCallMsg._Callback != null)
                            {
                                rpcCallMsg._Callback(message);
                            }
                        }
                        else
                        {
                            Debugger.Log("Invalid RPC_ID: " + message.RPC_ID);
                        }
                    }
                    else if (message.IsRPCCall())
                    {
                        Debugger.Assert(false, "Not supported now!");
                    }
                    else
                    {
                        DispatchEvent(message);
                    }

                    if (OnReceived != null)
                    {
                        OnReceived(message);
                    }
                }
            }

            __rawMsgQueue.Clear();

            /// Process error msg queue
            ///... _InternalMsgType.Disconnected has bug: Disconnect() method will clear the __sysMsgQueue, which will
            /// break current iteration.
            foreach (_InternalMsg errMsg in __sysMsgQueue)
            {
                switch (errMsg.type)
                {
                case _InternalMsgType.Connected:
                    if (__onConnect != null)
                    {
                        __onConnect(true, null);
                    }
                    __onConnect = null;
                    break;

                case _InternalMsgType.ConnectFailed:
                    if (__onConnect != null)
                    {
                        __onConnect(false, errMsg.exception);
                    }
                    __onConnect = null;
                    break;

                case _InternalMsgType.Disconnected:
                    Disconnect();
                    if (OnDisconnected != null)
                    {
                        OnDisconnected(errMsg.exception);
                    }
                    break;
                }
            }

            __sysMsgQueue.Clear();

            /// Process sending queue
            if (__sendMsgQueue.Count != 0 &&
                (_connectionState == ConnectionState.CONNECTED || _connectionState == ConnectionState.VALIDATED))
            {
                Message pMessage = __sendMsgQueue.Dequeue();
                _sendMessage(pMessage);
            }

            /// Checking RPC timeout
            foreach (var msg in _rpcCallMap.Values)
            {
                if (__fCurrentSeconds - msg._RPCCallStartTime > 6.0f)
                {
                    if (OnTimeout != null)
                    {
                        OnTimeout(TimeoutType.RPCCall, msg.msgID);
                    }
                    msg._RPCCallStartTime = __fCurrentSeconds;
                }
            }
        }
    }