コード例 #1
0
        private void RecoverTcpSocket(MySAE mysae)
        {
            try
            {
                if (mysae.SocketId == Guid.Empty)
                {
                    return;
                }
                if (OnDisConnect != null)
                {
                    try
                    {
                        OnDisConnect(mysae);
                    }
                    catch (Exception ex)
                    {
                        Data.Logger.Error(ex);
                    }
                }

                CloseTCPSocket(mysae);
                mysae.SetBuffer(mysae.BufferOffset, mysae.BufferLength);
                this._tcpReceiveSaePool.CheckPush(mysae);
                this._tcpReceiveSemaphoreSendedClients.Release();
            }
            catch (Exception ex)
            {
                Data.Logger.Error(ex);
            }
        }
コード例 #2
0
 public Boolean SetBuffer(MySAE args)
 {
     args.BufferLength = this._bufferSize * 2; //2倍于发送缓冲区大小
     if ((this._numSize - args.BufferLength) < this._currentIndex)
     {
         return(false);
     }
     args.BufferOffset = this._currentIndex;
     args.SetBuffer(this._buffer, args.BufferOffset, args.BufferLength);
     this._currentIndex += args.BufferLength;
     return(true);
 }
コード例 #3
0
 private void CloseSendSocket(MySAE mySae)
 {
     try
     {
         mySae.RemoteEndPoint = null;
         mySae.IsUdp          = false;
         mySae.SetBuffer(mySae.BufferOffset, mySae.BufferLength);
         _sendSaePool.Push(mySae);
         _sendSemaphoreSendedClients.Release();
     }
     catch (Exception ex)
     {
         Data.Logger.Error(ex);
     }
 }
コード例 #4
0
        private void CloseUdpSocket(MySAE mysae)
        {
            if (mysae.SocketId == Guid.Empty)
            {
                return;
            }
            IPEndPoint ipEndPoint = mysae.RemoteEndPoint as IPEndPoint;

            if (ipEndPoint != null)
            {
                int hashCode = ipEndPoint.Port % this._totalUdpListConnect;
                lock (this._udpListConnect[hashCode])
                {
                    if (this._udpListConnect[hashCode].ContainsKey(ipEndPoint))
                    {
                        this._udpReceiveSemaphoreSendedClients.Release();
                        lock (mysae)
                        {
                            if (mysae.SocketId == Guid.Empty)
                            {
                                return;
                            }
                            if (OnDisConnect != null)
                            {
                                try
                                {
                                    OnDisConnect(mysae);
                                }
                                catch (Exception ex)
                                {
                                    Data.Logger.Error(ex);
                                }
                            }

                            this._udpListConnect[hashCode].Remove(ipEndPoint);
                            mysae.SetBuffer(mysae.BufferOffset, mysae.BufferLength);
                            this._udpReceiveSaePool.Push(mysae);
                            if (_log && Data.Logger.IsDebugEnabled)
                            {
                                Data.Logger.Debug(String.Format("{0}:{1}	UDP断开", ipEndPoint.Address.ToString(),
                                                                ipEndPoint.Port));
                            }
                        }
                    }
                }
            }
        }
コード例 #5
0
        public bool SendBinaryByUdp(MySAE mySae, Guid msgId, byte[] bytes, int index, int length)
        {
            try
            {
                if (mySae == null)
                {
                    return(false);
                }
                else
                {
                    if (mySae.SocketId == Guid.Empty)
                    {
                        return(false);
                    }
                    this._sendSemaphoreSendedClients.WaitOne();
                    MySAE sendSae = this._sendSaePool.Pop(msgId);
                    if (sendSae == null)
                    {
                        this._sendSemaphoreSendedClients.Release();
                        return(false);
                    }

                    sendSae.UserToken      = mySae.UserToken;
                    sendSae.RemoteEndPoint = mySae.RemoteEndPoint;
                    sendSae.IsUdp          = true;
                    sendSae.Ip             = mySae.Ip;
                    sendSae.Port           = mySae.Port;
                    while (length > sendSae.BufferLength)
                    {
                        SendBinaryByUdp(mySae, Guid.NewGuid(), bytes, index, sendSae.BufferLength);
                        index  = index + sendSae.BufferLength;
                        length = length - sendSae.BufferLength;
                    }

                    mySae.SetBuffer(bytes, index, length);
                    return(SendByUdp(sendSae, length, 1));
                }
            }
            catch (Exception ex)
            {
                Data.Logger.Error(ex);
                return(false);
            }
        }
コード例 #6
0
        public bool SendBinaryByTcp(MySAE mysae, Guid msgId, byte[] bytes, int index, int length)
        {
            try
            {
                if (mysae == null)
                {
                    return(false);
                }


                if (mysae.SocketId == Guid.Empty)
                {
                    return(false);
                }
                this._sendSemaphoreSendedClients.WaitOne();
                MySAE sendSae = this._sendSaePool.Pop(msgId);
                if (sendSae == null)
                {
                    this._sendSemaphoreSendedClients.Release();
                    return(false);
                }

                lock (sendSae)
                {
                    sendSae.UserToken = mysae.UserToken;
                    sendSae.SocketId  = mysae.SocketId;
                    sendSae.Ip        = mysae.Ip;
                    sendSae.Port      = mysae.Port;

                    if (bytes != null && bytes.Length >= index + length)
                    {
                        sendSae.SetBuffer(bytes, index, length);
                        return(this.SendByTcp(sendSae, length, 1));
                    }
                }

                return(false);
            }
            catch (Exception ex)
            {
                Data.Logger.Error(ex);
                return(false);
            }
        }
コード例 #7
0
        private void StartUdpAccept(MySAE mysae)
        {
            if (mysae == null)
            {
                byte[] udpBuffer = new byte[this._receiveBufferSize * 2];
                mysae            = new MySAE(true, true);
                mysae.Completed += new EventHandler <SocketAsyncEventArgs>(OnUdpAcceptCompleted);
                mysae.SetBuffer(udpBuffer, 0, udpBuffer.Length);
                mysae.BufferOffset   = 0;
                mysae.BufferLength   = udpBuffer.Length;
                mysae.RemoteEndPoint = _localEndAny;
                mysae.SocketId       = Guid.NewGuid();
                mysae.MsgId          = mysae.SocketId;
            }

            if (isRun)
            {
                if (!_listenSocketUdp.ReceiveFromAsync(mysae))
                {
                    OnUdpAcceptCompleted(this, mysae);
                }
            }
        }
コード例 #8
0
        private void OnUdpAcceptCompleted(object sender, SocketAsyncEventArgs e)
        {
            MySAE mysae = (MySAE)e;

            try
            {
                if (mysae.LastOperation == SocketAsyncOperation.ReceiveFrom)
                {
                    if (mysae.BytesTransferred > 0)
                    {
                        if (mysae.SocketError == SocketError.Success)
                        {
                            Int32 byteTransferred = mysae.BytesTransferred;
                            try
                            {
                                IPEndPoint ipEndPoint = mysae.RemoteEndPoint as IPEndPoint;
                                if (_log && Data.Logger.IsDebugEnabled)
                                {
                                    Data.Logger.Debug(String.Format("{0}:{1}	UDP接收:[{2}]\n{3}",
                                                                    ipEndPoint.Address.ToString(),
                                                                    ipEndPoint.Port,
                                                                    Utility.Convert.HexByteToStr(mysae.Buffer, mysae.Offset, mysae.BytesTransferred),
                                                                    System.Text.Encoding.UTF8.GetString(mysae.Buffer, mysae.Offset,
                                                                                                        mysae.BytesTransferred)));
                                }
                                int   hashCode = ipEndPoint.Port % this._totalUdpListConnect;
                                MySAE themysae = null;
                                lock (_udpListConnect[hashCode])
                                {
                                    if (!_udpListConnect[hashCode].ContainsKey(ipEndPoint))
                                    {
                                        this._udpReceiveSemaphoreSendedClients.WaitOne();
                                        themysae = this._udpReceiveSaePool.Pop(Guid.NewGuid());
                                        if (themysae == null)
                                        {
                                            this._udpReceiveSemaphoreSendedClients.Release();
                                            return;
                                        }

                                        themysae.RemoteEndPoint = mysae.RemoteEndPoint;
                                        themysae.Ip             = ipEndPoint.Address.ToString();
                                        themysae.Port           = ipEndPoint.Port;
                                        _udpListConnect[hashCode].Add(ipEndPoint, themysae);
                                        if (OnAcceptConnect != null)
                                        {
                                            try
                                            {
                                                this.OnAcceptConnect(themysae);
                                            }
                                            catch (Exception ex)
                                            {
                                                Data.Logger.Error(ex);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        themysae = this._udpListConnect[hashCode][ipEndPoint];
                                    }
                                }

                                lock (themysae)
                                {
                                    if (themysae.SocketId == Guid.Empty)
                                    {
                                        return;
                                    }
                                    themysae.ClearTime();
                                    if ((themysae.BufferLength - (themysae.Offset - themysae.BufferOffset)) >=
                                        mysae.BytesTransferred)
                                    {
                                        Array.Copy(mysae.Buffer, mysae.Offset, themysae.Buffer, themysae.Offset,
                                                   mysae.BytesTransferred);
                                        try
                                        {
                                            int bufferOffset            = themysae.BufferOffset;
                                            int bufferLastOffset        = themysae.BufferOffset + themysae.BufferLength;
                                            int receiveBufferLastOffset = themysae.Offset + mysae.BytesTransferred;
                                            while (bufferOffset < receiveBufferLastOffset)
                                            {
                                                int msgLength = 1;
                                                if (this.OnMsgReceived(themysae, themysae.Buffer, bufferOffset,
                                                                       receiveBufferLastOffset - bufferOffset, ref msgLength))
                                                {
                                                    bufferOffset       = bufferOffset + msgLength;
                                                    themysae.ErrorTime = 0;
                                                }
                                                else
                                                {
                                                    if (bufferOffset < themysae.Offset)
                                                    {
                                                        bufferOffset = themysae.Offset;
                                                    }
                                                    else
                                                    {
                                                        themysae.ErrorTime++;
                                                        if (bufferOffset == themysae.Offset)
                                                        {
                                                            bufferOffset = themysae.BufferOffset;
                                                        }
                                                        if (bufferLastOffset - (receiveBufferLastOffset - bufferOffset) <
                                                            this._receiveBufferSize || themysae.ErrorTime > 10)
                                                        {
                                                            bufferOffset       = receiveBufferLastOffset;
                                                            themysae.ErrorTime = 0;
                                                        }

                                                        break;
                                                    }
                                                }
                                            }

                                            if (bufferOffset != themysae.BufferOffset)
                                            {
                                                if (bufferOffset < receiveBufferLastOffset) //移动缓存区
                                                {
                                                    Array.Copy(themysae.Buffer, bufferOffset, themysae.Buffer,
                                                               themysae.BufferOffset, receiveBufferLastOffset - bufferOffset);
                                                    receiveBufferLastOffset = themysae.BufferOffset +
                                                                              receiveBufferLastOffset - bufferOffset;
                                                }
                                                else
                                                {
                                                    receiveBufferLastOffset = themysae.BufferOffset;
                                                }
                                            }

                                            themysae.SetBuffer(receiveBufferLastOffset,
                                                               bufferLastOffset - receiveBufferLastOffset);
                                        }
                                        catch (Exception ex)
                                        {
                                            Data.Logger.Error(ex);
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Data.Logger.Error(ex);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Data.Logger.Error(ex);
            }
            finally
            {
                StartUdpAccept(mysae);
            }
        }
コード例 #9
0
        private void OnTcpReceiveCompleted(object sender, SocketAsyncEventArgs e)
        {
            MySAE mysae = (MySAE)e;

            lock (mysae)
            {
                if (mysae.LastOperation != SocketAsyncOperation.Receive)
                {
                    return;
                }
                if (mysae.SocketError == SocketError.Success && mysae.BytesTransferred > 0)
                {
                    mysae.ClearTime();
                    try
                    {
                        if (_log && Data.Logger.IsDebugEnabled)
                        {
                            Data.Logger.Debug(String.Format("{0}:{1}	TCP接收:[{2}]\n{3}", mysae.Ip, mysae.Port,
                                                            Utility.Convert.HexByteToStr(mysae.Buffer, mysae.Offset,
                                                                                         mysae.BytesTransferred),
                                                            System.Text.Encoding.UTF8.GetString(mysae.Buffer,
                                                                                                mysae.Offset, mysae.BytesTransferred)));
                        }

                        int bufferOffset            = mysae.BufferOffset;
                        int bufferLastOffset        = mysae.BufferOffset + mysae.BufferLength;
                        int receiveBufferLastOffset = mysae.Offset + mysae.BytesTransferred;
                        while (bufferOffset < receiveBufferLastOffset)
                        {
                            int msgLength = 1;
                            if (this.OnMsgReceived(mysae, mysae.Buffer, bufferOffset, receiveBufferLastOffset - bufferOffset, ref msgLength))
                            {
                                bufferOffset    = bufferOffset + msgLength;
                                mysae.ErrorTime = 0;
                            }
                            else
                            {
                                if (bufferOffset < mysae.Offset)
                                {
                                    bufferOffset = mysae.Offset;
                                }
                                else
                                {
                                    mysae.ErrorTime = mysae.ErrorTime + 1;
                                    if (bufferOffset == mysae.Offset)
                                    {
                                        bufferOffset = mysae.BufferOffset;
                                    }
                                    if (bufferLastOffset - (receiveBufferLastOffset - bufferOffset) <
                                        this._receiveBufferSize || mysae.ErrorTime > 10)
                                    {
                                        bufferOffset    = receiveBufferLastOffset;
                                        mysae.ErrorTime = 0;
                                    }

                                    break;
                                }
                            }
                        }

                        if (bufferOffset != mysae.BufferOffset)
                        {
                            if (bufferOffset < receiveBufferLastOffset) //移动缓存区
                            {
                                Array.Copy(mysae.Buffer, bufferOffset, mysae.Buffer, mysae.BufferOffset,
                                           receiveBufferLastOffset - bufferOffset);
                                receiveBufferLastOffset = mysae.BufferOffset + receiveBufferLastOffset - bufferOffset;
                            }
                            else
                            {
                                receiveBufferLastOffset = mysae.BufferOffset;
                            }
                        }

                        mysae.SetBuffer(receiveBufferLastOffset, bufferLastOffset - receiveBufferLastOffset);
                    }
                    catch (Exception ex)
                    {
                        Data.Logger.Error(ex);
                    }

                    try
                    {
                        Boolean willRaiseEvent = (mysae.UserToken as System.Net.Sockets.Socket).ReceiveAsync(mysae);
                        if (!willRaiseEvent)
                        {
                            OnTcpReceiveCompleted(this, mysae);
                        }
                    }
                    catch (ObjectDisposedException)
                    {
                        this.RecoverTcpSocket(mysae);
                    }
                }
                else
                {
                    this.RecoverTcpSocket(mysae);
                }
            }
        }