Esempio n. 1
0
		public void ReceiveAsync (NetCallback callback)
		{
			OnReceiveCallback = callback;
			// 从远程target接收Number据.
			this.mSocket.BeginReceive (mTmpBuffer, 0, mTmpBufferSize, 0,
				(AsyncCallback)ReceiveCallback, this);
		}
Esempio n. 2
0
        /// <summary>
        /// 连接到某个网络地址
        /// </summary>
        /// <param name="host"></param>
        /// <param name="port"></param>
        public void Connect(String host, int port, NetCallback connectFunc, NetCallback readFunc)
        {
            if (_isFree)
            {
                throw new Exception("netSession is free!");
            }
            if (String.IsNullOrEmpty(host) || port < 1)
            {
                throw new Exception("error host or port!");
            }

            _connectFunc = connectFunc;
            _readFunc    = readFunc;

            if (_tcp != null)
            {
                _tcp.Close();
                _tcp = null;
            }
            try {
                _tcp = new TcpClient();
                _tcp.BeginConnect(host, port, new AsyncCallback(TcpConnectCallback), this);
            } catch (Exception ex) {
                OnException(ex);
            }
        }
Esempio n. 3
0
        private bool HandlePush_Ex(PhobosPacket packet)
        {
            //1.二进制传输的消息 直接广播出去
            var bytecmd = packet.Command.GetType() == typeof(ByteNetCommand);

            if (bytecmd)
            {
                NetworkManager.AddEvent(Protocal.Message, (ByteNetCommand)packet.Command);
                return(true);
            }

            var pid = Registry.MustFind(packet.Command.GetType());

            //2. 服务器下推消息
            NetCallback callback = null;

            if (mHandlers.TryGetValue(pid, out callback))
            {
                // 调用回调函数
                callback.Invoke(packet.Head, packet.Command);
                return(true);
            }

            Util.Log($"Push callback not found for: {pid}");
            return(false);
        }
Esempio n. 4
0
        public void connectAsync(NetCallback onConnectStateChgCallback)
        {
            try
            {
                isActive = false;
                this.onConnectStateChgCallback = onConnectStateChgCallback;
                if (ipe == null)
                {
                    onConnectStateChg(false, Errors.IPEndPointIsNull);
                    return;
                }

                mSocket.BeginConnect(ipe, (AsyncCallback)connectCallback, this);
                if (connectTimeout != null)
                {
                    connectTimeout.Dispose();
                    connectTimeout = null;
                }
                connectTimeout = TimerEx.schedule((TimerCallback)connectTimeOut, null, timeoutMSec);
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
Esempio n. 5
0
		public void connectAsync (NetCallback callback, NetCallback offLineCallback)
		{
			if (ipe == null) {
				callback (this, false);
				return;
			}
			this.offLineCallback = offLineCallback;
			IsConnectionSuccessful = false;
			connectCallbackFunc = callback;

			mSocket.BeginConnect (ipe, (AsyncCallback)connectCallback, this);
            if(connectTimeout != null) {
                connectTimeout.Dispose();
                connectTimeout = null;
            }
            connectTimeout = TimerEx.schedule((TimerCallback)connectTimeOut, null, timeoutMSec);
        }