Exemple #1
0
        //data arrive
        private void DataCallback(IAsyncResult ar)
        {
            IOCPSocket socket = null;

            try
            {
                if (ar.AsyncState == null)
                {
                    if (_appExcptionHandler != null)
                    {
                        _appExcptionHandler(socket, AppExceptionEnum.Error, "Socket已释放");
                    }
                    return;
                }
            }
            catch (Exception ex)
            {
            }

            try
            {
                socket = ar.AsyncState as IOCPSocket;

                socket.SocketObject.DataLength = socket.SocketObject.ClientSocket.EndReceive(ar);

                if (socket.SocketObject.DataLength > 0)
                {
                    if (_dataHandler != null)
                    {
                        _dataHandler(socket);
                    }
                    socket.LastRecvDataTime = DateTime.Now;
                }
                else
                {
                    socket.Close();
                    return;
                }

                IOCPSocket _socket = socket;
                _socket.SocketObject.DataLength = 0;

                socket.SocketObject.ClientSocket.BeginReceive(_socket.SocketObject.Buffer, _socket.SocketObject.DataLength, IOCPObject.BufferLengthMax, SocketFlags.None, new AsyncCallback(DataCallback), _socket);
            }
            catch (Exception ex)
            {
                if (socket != null)
                {
                    _id = socket.ID;
                }
                Close();
            }
        }
Exemple #2
0
        //允许接入
        private void AcceptClientHandlerCallback(IAsyncResult ar)
        {
            IOCPSocket socket = null;

            try
            {
                _client.EndConnect(ar);

                socket = acceptEventHandler(_client.Client);

                socket.LastRecvDataTime = DateTime.Now;
                socket.CreateDateTime   = DateTime.Now;
                _socketObject           = socket.SocketObject;

                if (_appExcptionHandler != null)
                {
                    _appExcptionHandler(socket, AppExceptionEnum.StartOK, "启动成功");
                }
                if (_acceptHandler != null)
                {
                    _acceptHandler(socket);
                }

                _client.Client.BeginReceive(socket.SocketObject.Buffer, socket.SocketObject.DataLength, IOCPObject.BufferLengthMax, SocketFlags.None, new AsyncCallback(DataCallback), socket);
            }
            catch (SocketException ex)
            {
                if (_exceptionHandler != null)
                {
                    _exceptionHandler(socket, AppExceptionEnum.Error, ex.Message);
                }
                Close();
            }
            catch (ObjectDisposedException ex)
            {
                if (_exceptionHandler != null)
                {
                    _exceptionHandler(socket, AppExceptionEnum.Error, ex.Message);
                }
            }
            catch (Exception ex)
            {
                if (_exceptionHandler != null)
                {
                    _exceptionHandler(socket, AppExceptionEnum.Error, ex.Message);
                }
                Close();
            }
        }
Exemple #3
0
        /// <summary>
        /// 本地更新变量
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        protected IOCPSocket acceptEventHandler(Socket client)
        {
            IOCPSocket socket = new IOCPSocket();

            socket.ID = Guid.NewGuid();

            socket.IsLogined = IsLogined;
            socket.DeviceId  = DeviceId;
            socket.Serial    = _deviceSerial;

            socket.SocketObject            = new IOCPObject();
            socket.SocketObject.DataLength = 0;
            socket.SocketObject.Buffer     = new byte[IOCPObject.BufferLengthMax];

            socket.SocketObject.ClientSocket = client;

            socket.OnDataHandler         += _dataHandler;
            socket.OnAcceptHandler       += _acceptHandler;
            socket.OnCloseHandler        += _closeHandler;
            socket.OnExceptionHandler    += _exceptionHandler;
            socket.OnAppExceptionHandler += _appExcptionHandler;

            return(socket);
        }
Exemple #4
0
 public SocketBuffer()
 {
     LastDataReceived = DateTime.Now;
     Buffer           = new CmdBuffer();
     Socket           = new IOCPSocket();
 }