Exemple #1
0
    public void DoIncoming()
    {
        //lock (Socket_)
        //{
        if (!Socket_.Connected)
        {
            return;
        }
        if (Socket_.Poll(0, System.Net.Sockets.SelectMode.SelectRead))
        {
            try
            {
                int recved = Socket_.Receive(IncomingBuffer_.Memory, IncomingBuffer_.WrPtr(), IncomingBuffer_.Space, System.Net.Sockets.SocketFlags.None);
                IncomingBuffer_.WrPtr(recved);
                TotalIncoming_ += recved;
                // ClientLog.Instance.LogError("Incoming once");
            }
            catch (System.Net.Sockets.SocketException se)
            {
                ClientLog.Instance.Log("Exception at DoIncoming" + se.ErrorCode);
                LastError_ = se.ErrorCode;
            }
        }
        //}

        //lock (IncomingBuffer_)
        //{
        //if (RecvBuffer_.Length != 0)
        //{
        //    //解压
        //    RecvBuffer_.Decompression(IncomingBuffer_);

        //}
        //}
    }
Exemple #2
0
    //private void ImcomingThreadFunc()
    //{
    //    do
    //    {
    //        DoIncoming();
    //        System.Threading.Thread.Sleep(1);
    //    } while (IsWorking_);
    //}

    public void DoOutgoing()
    {
        if (!CanFlush_)
        {
            return;
        }
        //lock (Socket_)
        //{
        if (!Socket_.Connected)
        {
            return;
        }
        if (OutgoingBuffer_.Length != 0)
        {
            //发送
            if (Socket_.Poll(0, System.Net.Sockets.SelectMode.SelectWrite))
            {
                try
                {
                    int sended = Socket_.Send(OutgoingBuffer_.Memory, OutgoingBuffer_.RdPtr(), OutgoingBuffer_.Length, System.Net.Sockets.SocketFlags.None);
                    OutgoingBuffer_.RdPtr(sended);
                    OutgoingBuffer_.Crunch();
                    TotalOutgoing_ += sended;

                    CanFlush_ = false;
                }
                catch (System.Net.Sockets.SocketException se)
                {
                    ClientLog.Instance.Log("Exception at DoOutgoing" + se.ErrorCode);
                    LastError_ = se.ErrorCode;
                }
            }
        }
        //}
    }
Exemple #3
0
 public void EnableNoneBlock(bool v)
 {
     //lock (Socket_)
     //{
     Socket_.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Tcp, System.Net.Sockets.SocketOptionName.NoDelay, v);
     //}
 }
Exemple #4
0
 public void EnableDebug(bool v)
 {
     //lock (Socket_)
     //{
     Socket_.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Tcp, System.Net.Sockets.SocketOptionName.Debug, v);
     //}
 }
Exemple #5
0
 private void ConnectServerCallback(System.IAsyncResult ar)
 {
     try
     {
         EndTimeout();
         Socket_.EndConnect(ar);
         GlobalInstanceFunction.Instance.Invoke(() =>
         {
             if (NetConnection.Instance.Socket_Callback != null)
             {
                 NetConnection.Instance.Socket_Callback(ar);
                 NetConnection.Instance.Socket_Callback = null;
             }
         }, 1);
     }
     catch (System.Net.Sockets.SocketException se)
     {
         ClientLog.Instance.Log(se.Message);
         GlobalInstanceFunction.Instance.Invoke(() =>
         {
             if (NetConnection.Instance.Socket_Callback != null)
             {
                 NetConnection.Instance.Socket_Callback(null);
                 NetConnection.Instance.Socket_Callback = null;
                 LastError_ = se.ErrorCode;
             }
         }, 1);
     }
 }
Exemple #6
0
 public void Deconnect()
 {
     //lock (Socket_)
     //{
     try
     {
         Socket_.Shutdown(System.Net.Sockets.SocketShutdown.Both);
         Socket_.Close();
     }
     catch (System.Net.Sockets.SocketException se)
     {
         CreateSocket();
         return;
     }
     CreateSocket();
     //}
 }
Exemple #7
0
 public bool Connect(string ipaddr, int port)
 {
     //lock (Socket_)
     //{
     try
     {
         //System.Net.IPAddress[] addr = System.Net.Dns.GetHostAddresses(ipaddr);
         //Socket_.Connect(new System.Net.IPEndPoint(addr[0], port));
         BeginTimeout();
         Socket_.BeginConnect(ipaddr_, int.Parse(port_), ConnectServerCallback, Socket_);
     }
     catch (System.Net.Sockets.SocketException se)
     {
         LastError_ = se.ErrorCode;
         return(false);
     }
     return(true);
     //}
 }
Exemple #8
0
    public bool Reset()
    {
        if (!IsWorking_)
        {
            return(true);
        }

        bool hasData = false;

        while (IsConneted && LastError == 0)
        {
            if (Socket_.Poll(0, System.Net.Sockets.SelectMode.SelectRead))
            {
                try
                {
                    if (0 >= Socket_.Receive(IncomingBuffer_.Memory, IncomingBuffer_.WrPtr(), IncomingBuffer_.Space, System.Net.Sockets.SocketFlags.None))
                    {
                        break;
                    }
                    else
                    {
                        hasData = true;
                    }
                }
                catch (System.Net.Sockets.SocketException se)
                {
                    ClientLog.Instance.Log("Exception at Reset" + se.ErrorCode);
                    LastError_ = se.ErrorCode;
                    break;
                }
            }
            else
            {
                break;
            }
        }
        IncomingBuffer_.Reset();
        OutgoingBuffer_.Reset();
        return(hasData);
    }