void Connect_Completed(object sender, SocketAsyncEventArgs e)
 {
     ReceiveEventArgs.Completed -= Connect_Completed;
     if (e.SocketError == SocketError.Success)
     {
         ReceiveEventArgs.SetBuffer(RecevieBuff, 0, RecevieBuff.Length);
         SendEventArgs = new SocketAsyncEventArgs();
         SendBuff      = new byte[RecevieBuff.Length];
         SendEventArgs.SetBuffer(SendBuff, 0, SendBuff.Length);
         SendEventArgs.RemoteEndPoint = e.RemoteEndPoint;
         SendEventArgs.Completed     += SendEventArgs_Completed;
         ReceiveEventArgs.Completed  += Receive_Completed;
         socket.ReceiveAsync(ReceiveEventArgs);
         if (onConnect != null)
         {
             asyncOp.Post(result =>
             {
                 onConnect(true);
             }, null);
         }
     }
     else
     {
         if (onConnect != null)
         {
             asyncOp.Post(result =>
             {
                 onConnect(false);
             }, null);
         }
         handleExpression();
     }
 }
 protected void StartReceive()
 {
     ReceiveEventArgs.SetBuffer(_RecvBuffer, 0, _RecvBuffer.Length);
     if (!_ChannelSocket.ReceiveAsync(ReceiveEventArgs))
     {
         Log.Debug("ChannelSocket.ReceiveAsync 事件已同步完成");
         ProcessReceive(ReceiveEventArgs);
     }
 }
Exemple #3
0
        /// <summary>
        /// 重新异步开始接受数据
        /// </summary>
        internal void SyncRecvData()
        {
            if (Socket == null || !Socket.Connected)
            {
                return;
            }

            var buff = DogBuffer.GetFromPool4K();

            RecvBuffer = buff;
            ReceiveEventArgs.SetBuffer(buff.Bytes, 0, buff.Bytes.Length);
            Socket.ReceiveAsync(ReceiveEventArgs);
        }
        /// <summary>
        /// 重新异步开始接受数据
        /// </summary>
        internal void SyncRecvData()
        {
            if (Socket == null || !Socket.Connected)
            {
                return;
            }

            ReceiveEventArgs.SetBuffer(0, RecvBuffer.Bytes.Length);

            //  好吧,会有一定概率,在通过第一行的验证后,socket被关闭,然后Socket被设置为空
            if (Socket != null)
            {
                Socket.ReceiveAsync(ReceiveEventArgs);
            }
        }
Exemple #5
0
 private void ConnectComplate(object sender, SocketAsyncEventArgs asyncEventArgs)
 {
     ReceiveEventArgs.Completed -= ConnectComplate;
     if (asyncEventArgs.SocketError == SocketError.Success)
     {
         ReceiveEventArgs.Completed += ReceiveComplate;
         ReceiveEventArgs.SetBuffer(receiveBuffer, 0, receiveBuffer.Length);
         ConnectSocket = asyncEventArgs.ConnectSocket;
         StartReceiveAsync();
         OnConnected(this);//响应连接成功事件
     }
     else
     {
         loger.Error(string.Format("连接{0}失败", remoteEndPoint));
     }
 }
 protected void OnConnectComplete(object sender, SocketAsyncEventArgs e)
 {
     if (e.SocketError == SocketError.Success)
     {
         IsConnecting = true;
         ReceiveEventArgs.SetBuffer(State.Buffer, 0, State.Buffer.Length);
         Client.ReceiveAsync(ReceiveEventArgs);
         if (Connected != null)
         {
             Connected(this, new AsyncConnectionEventArgs(null, this));
         }
     }
     else
     {
         IsConnecting = false;
     }
 }
        /// <summary>
        /// 重新异步开始接受数据
        /// </summary>
        internal void SyncRecvData()
        {
            if (isClose || Socket == null || !Socket.Connected)
            {
                return;
            }

#if !UNITY_IPHONE
            lock (Socket)
#endif
            {
                var buff = DogBuffer.GetFromPool32K();
                RecvBuffer = buff;
                ReceiveEventArgs.SetBuffer(RecvBuffer.Bytes, 0, RecvBuffer.Bytes.Length);
                Socket.ReceiveAsync(ReceiveEventArgs);
            }
        }
		protected void OnReceiveComplete(ReceiveEventArgs arg)
		{
			if (arg.SocketError != System.Net.Sockets.SocketError.Success)
			{
				//Release back to the pool
				arg.Socket = null;
				_receivePool.PushBack(arg);

				Close();
			}
			else if (arg.BytesTransferred == 0)
			{
				//Release back to the pool
				arg.Socket = null;
				_receivePool.PushBack(arg);

				Close();
			}
			else
			{
				//receivedBytes += arg.BytesTransferred;

				//lock (storeBuffer)
				{
					//var free = storeBuffer.Length - storeWriteOffset;
					//if (free < arg.BytesTransferred)
					//{
					//    throw new InvalidOperationException("Receive buffer has no room for this segment");
					//}

					//Migrate to the storeBuffer
					//Array.Copy(receiveBuffer, arg.Offset, storeBuffer, storeWriteOffset, arg.BytesTransferred);
					//storeWriteOffset += arg.BytesTransferred;

					lock (_rxSyncRoot)
					//lock (storeBuffer)
					{
						storeWriteOffset += arg.BytesTransferred;
						var free = arg.Buffer.Length - storeWriteOffset;

						if (free == 0)
						{
							//Move back to the start. This can overwrite data, so make sure terraria reading isn't slow!
							storeWriteOffset = 0;
							free = receiveBuffer.Length;
							reset = true;
							Console.WriteLine("Reader reset");
						}

						arg.SetBuffer(storeWriteOffset, free);

						available += arg.BytesTransferred;
					}
				}

				if (!_socket.ReceiveAsync(arg))
				{
					//The receive was processed synchronously which means the callback wont be executed.
					OnReceiveComplete(arg);
				}
			}
		}