コード例 #1
0
ファイル: TcpClient.cs プロジェクト: tangfengray/soul
        private static void ReceiveCallback(IAsyncResult ar)
        {
            ClientStateObject state = (ClientStateObject)ar.AsyncState;
            TcpClient         t     = state.c;
            Socket            s     = t.GetSocket();

            if (s == null)
            {
                return;
            }
            try
            {
                int read = s.EndReceive(ar);
                if (read > 0)
                {
                    t.onReceive(state.buffer, read);
                    s.BeginReceive(state.buffer, 0, state.buffer.Length, 0, new AsyncCallback(ReceiveCallback), state);
                }
                else
                {
                    t.onClose(s);
                }
            }
            catch (System.Exception ex)
            {
                t.onClose(s);
                Log.Instance().WriteLog(ex.Message);
                Log.Instance().WriteLog(ex.StackTrace);
            }
        }
コード例 #2
0
ファイル: TcpClient.cs プロジェクト: tangfengray/soul
        public void SendData(byte[] data)
        {
            if (!mSocket.Connected)
            {
                return;
            }
            ClientStateObject state = new ClientStateObject();

            state.c = this;
            state.s = this.mSocket;
            mSocket.BeginSend(data, 0, data.Length, 0, new AsyncCallback(SendCallback), state);
        }
コード例 #3
0
ファイル: TcpClient.cs プロジェクト: tangfengray/soul
        public static void SendCallback(IAsyncResult ar)
        {
            ClientStateObject state = (ClientStateObject)ar.AsyncState;

            try
            {
                int retlen = state.s.EndSend(ar);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                state.c.onClose(state.s);
            }
        }
コード例 #4
0
ファイル: TcpClient.cs プロジェクト: tangfengray/soul
        private static void ConnectCallback(IAsyncResult ar)
        {
            TcpClient t = (TcpClient)ar.AsyncState;

            try
            {
                Socket s = t.GetSocket();
                s.EndConnect(ar);
                t.onConnect(true);
                ClientStateObject state = new ClientStateObject();
                state.s = s;
                state.c = t;
                s.BeginReceive(state.buffer, 0, state.buffer.Length, 0, new AsyncCallback(ReceiveCallback), state);
            }
            catch (System.Exception ex)
            {
                t.onConnect(false);

                //Log.Instance().WriteLog(ex.Message);
                //Log.Instance().WriteLog(ex.StackTrace);
            }
        }
コード例 #5
0
ファイル: TcpClient.cs プロジェクト: dream-young-soul/soul
 public void SendData(byte[] data)
 {
     if (!mSocket.Connected) return;
     ClientStateObject state = new ClientStateObject();
     state.c = this;
     state.s = this.mSocket;
     mSocket.BeginSend(data,0,data.Length,0,new AsyncCallback(SendCallback), state);
 }
コード例 #6
0
ファイル: TcpClient.cs プロジェクト: dream-young-soul/soul
        private static void ConnectCallback(IAsyncResult ar)
        {
            TcpClient t = (TcpClient)ar.AsyncState;
            try
            {
                Socket s = t.GetSocket();
                s.EndConnect(ar);
                t.onConnect(true);
                ClientStateObject state = new ClientStateObject();
                state.s = s;
                state.c = t;
                s.BeginReceive(state.buffer, 0, state.buffer.Length, 0, new AsyncCallback(ReceiveCallback), state);

            }
            catch (System.Exception ex)
            {
                t.onConnect(false);

                //Log.Instance().WriteLog(ex.Message);
                //Log.Instance().WriteLog(ex.StackTrace);
            }
        }