Exemplo n.º 1
0
 private void TryDequeueAndPostSend(TcpConnect tcpConn)
 {
     byte[] data;
     if (tcpConn.TryDequeue(out data))
     {
         AsyncUserToken dataToken = (AsyncUserToken)tcpConn.SendEventArgs.UserToken;
         dataToken.messageBytes  = data;
         dataToken.messageLength = data.Length;
         if (TestLag)
         {
             dataToken.SendTime = System.DateTime.Now.Ticks; // IDllImportAPI.HighPrecision_GetTickCount();
         }
         try
         {
             PostSend(tcpConn.SendEventArgs);
         }
         catch (System.Exception ex)
         {
             Log.Log.Net.Info(ex.ToString());
             Log.Log.Net.Info(ex.StackTrace.ToString());
             //发送发生了异常,那么这个连接都关闭算了。
             HandleCloseSocket(tcpConn.ReceiveEventArgs);
         }
     }
     else
     {
         tcpConn.ResetSendFlag();
     }
 }
Exemplo n.º 2
0
        private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs)
        {
            PostAccept();

            if (acceptEventArgs.SocketError != SocketError.Success)
            {
                HandleBadAccept(acceptEventArgs);
                return;
            }

            var newConn = new TcpConnect(this, acceptEventArgs);

            acceptEventArgs.AcceptSocket = null;
            this.acceptEventArgsPool.Push(acceptEventArgs);

            lock (this)
            {
                if (clientSockets.ContainsKey(newConn) == false)
                {
                    clientSockets.Add(newConn, newConn);
                }
                else
                {
                    Log.Log.Net.Info("怎么可能?clientSockets.ContainsKey(newConn) == false");
                }
                newConnectQueue.Enqueue(newConn);
            }

            PostReceive(newConn.ReceiveEventArgs);
        }
Exemplo n.º 3
0
 public RecvEventArg(TcpConnect con, byte[] m)
 {
     connect = con;
     msg     = new byte[m.Length];
     System.Buffer.BlockCopy(m, 0, msg, 0, m.Length);
     ReceiveTime = 0;
 }
Exemplo n.º 4
0
 public void Raise_CloseConnect(TcpConnect pConnect)
 {
     pConnect.State = NetState.Disconnect;
     if (CloseConnect != null)
     {
         CloseConnect(pConnect, this, null, 0);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Posts the send.
        /// </summary>
        public void PostSend(TcpConnect tcpConn, byte[] data, int offset, int count)
        {
            int P = NetPacketParser.PREFIX_SIZE;

            byte[] buffer = new byte[count + P];
            Buffer.BlockCopy(BitConverter.GetBytes((UInt16)count), 0, buffer, 0, P);
            Buffer.BlockCopy(data, offset, buffer, P, count);
            tcpConn.Enqueue(buffer);
            if (tcpConn.TrySetSendFlag())
            {
                try
                {
                    AsyncUserToken dataToken = (AsyncUserToken)tcpConn.SendEventArgs.UserToken;
                    dataToken.Reset(true);

                    TryDequeueAndPostSend(tcpConn);
                }
                catch
                {
                    tcpConn.ResetSendFlag();
                    throw;
                }
            }
        }
Exemplo n.º 6
0
        private void HandleCloseSocket(SocketAsyncEventArgs ioEventArgs)
        {
            lock (this)
            {
                if (ioEventArgs == null)
                {
                    Log.Log.Net.Info("HandleCloseSocket ioEventArgs==null");
                    return;
                }
                TcpConnect connect = null;
                var        token   = ioEventArgs.UserToken as AsyncUserToken;
                if (token == null)
                {
                    Log.Log.Net.Info("HandleCloseSocket ioEventArgs.UserToken==null");
                }
                else
                {
                    if (token.tcpConn == null)
                    {
                        return;
                        //Log.FileLog.WriteLine("HandleCloseSocket token.tcpConn==null");
                    }
                    else
                    {
                        connect = token.tcpConn;
                    }
                }
                if (connect == null)
                {
                    Log.Log.Net.Info("HandleCloseSocket connect==null");
                    try
                    {
                        if (ioEventArgs.AcceptSocket != null)
                        {
                            ioEventArgs.AcceptSocket.Shutdown(SocketShutdown.Both);
                            ioEventArgs.AcceptSocket.Close();
                        }
                        else
                        {
                            //TcpConnect已经Close了
                            Log.Log.Net.Info("HandleCloseSocket ioEventArgs.AcceptSocket==null");
                        }
                    }
                    catch (System.Exception ex)
                    {
                        Log.Log.Net.Info(ex.ToString());
                        Log.Log.Net.Info(ex.StackTrace.ToString());
                    }
                    finally
                    {
                        ioEventArgs.AcceptSocket = null;
                    }
                }
                else
                {
                    try
                    {
                        if (closeConnectQueue.ContainsKey(connect))
                        {
                            clientSockets.Remove(connect);
                            return;
                            //Log.FileLog.WriteLine("HandleCloseSocket closeConnectQueue.ContainsKey(connect)");
                        }

                        TcpConnect conn;
                        if (clientSockets.TryGetValue(connect, out conn))
                        {
                            clientSockets.Remove(connect);
                        }
                        else
                        {
                            Log.Log.Net.Info("Error! HandleCloseSocket clientSockets找不到connect");
                        }

                        closeConnectQueue[connect] = connect;
                    }
                    catch (System.Exception ex)
                    {
                        Log.Log.Net.Info(ex.ToString());
                        Log.Log.Net.Info(ex.StackTrace.ToString());
                    }
                }
            }
        }
Exemplo n.º 7
0
 public void CloseSocket(TcpConnect tcpConn)
 {
     HandleCloseSocket(tcpConn.ReceiveEventArgs);
     //tcpConn.Close();
 }