예제 #1
0
파일: SocketClient.cs 프로젝트: kcitwm/dova
 protected void OnSent(SocketEventArgs args)
 {
     EventHandler handler = events[sentKey] as EventHandler;
     if (null != handler)
     {
         handler(this, args);
     }
 }
예제 #2
0
파일: SocketClient.cs 프로젝트: kcitwm/dova
 protected void OnReceive(SocketEventArgs args)
 {
     EventHandler handler = events[receiveKey] as EventHandler;
     if (null != handler)
     {
         handler(this, args);
     }
 }
예제 #3
0
파일: SocketClient.cs 프로젝트: kcitwm/dova
 public string Receive()
 {
     long t = DateTime.Now.Ticks;
     try
     {
         Log.Info("开始同步接收数据"); 
         int read = socket.Receive(so.Buffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None);
         if (read > 0)
         {
             string data = encode.GetString(so.Buffer, 0, read);
             Log.Write(LogAction.Info, Config.AppName, className, "Receive", DateTime.Now.Ticks - t, "同步接收到数据:" + data);
             so.SB.Append(data);
             if (!string.IsNullOrEmpty(data) && !data.EndsWith(so.ETX))
                 socket.Receive(so.Buffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None);
             if (so.SB.Length > 1)
             {
                 so.SB.Length = 0;
                 SocketEventArgs se = new SocketEventArgs();
                 se.Received = read;
                 se.Data = data;
                 OnReceive(se);
                 return data;
             }
         }
         Log.Info("同步数据接收字节长度:" + read);
     }
     catch (Exception e)
     {
         Log.Write(LogAction.Error, Config.AppName, className, "Receive", DateTime.Now.Ticks - t, "同步接收数据异步:" + e.ToString());
         throw;
     }
     return "";
 }
예제 #4
0
파일: SocketClient.cs 프로젝트: kcitwm/dova
        public void ReceiveCallBack(IAsyncResult ar)
        {
            long t = DateTime.Now.Ticks;
            try
            {
                StateObject so = (StateObject)ar.AsyncState;
                Socket s = so.WorkSocket;
                int read = s.EndReceive(ar);
                if (read > 0)
                {
                    string data = encode.GetString(so.Buffer, 0, read);
                    Log.Write(LogAction.Info, Config.AppName, className, "ReceiveCallBack", DateTime.Now.Ticks - t, "异步接收到数据:" + data); 
                    so.SB.Append(data);
                    if (!string.IsNullOrEmpty(data) && !data.EndsWith(so.ETX))
                        s.BeginReceive(so.Buffer, 0, StateObject.BUFFER_SIZE, 0, new AsyncCallback(ReceiveCallBack), so);
                    else
                    {
                        if (so.SB.Length > 1)
                        {
                            so.SB.Length = 0;
                            SocketEventArgs se = new SocketEventArgs();
                            se.Received = read;
                            se.Data = data;
                            OnReceive(se);
                        }
                    }
                    return;
                }
                Log.Info("异步数据接收字节长度:" + read);
            }
            catch (Exception e)
            {
                Log.Write(LogAction.Error, Config.AppName, className, "ReceiveCallBack", DateTime.Now.Ticks - t, "异步接收数据异常:" + e.ToString()); 
            }

        }
예제 #5
0
파일: SocketClient.cs 프로젝트: kcitwm/dova
        public void SendCallBack(IAsyncResult ar)
        {
            long t = DateTime.Now.Ticks;
            try
            {
                Log.Info("开始异步发送数据");
                StateObject so = (StateObject)ar.AsyncState;
                Socket s = so.WorkSocket;
                int n = s.EndSend(ar);
                Log.Write(LogAction.Info, Config.AppName, className, "SendCallBack", DateTime.Now.Ticks - t, "异步发送数据成功,数据长度:" + n);
                SocketEventArgs se = new SocketEventArgs();
                se.Sent = n;
                OnSent(se);
            }
            catch (Exception e)
            {
                Log.Write(LogAction.Info, Config.AppName, className, "SendCallBack", DateTime.Now.Ticks - t, "异步发送数据失败:" + e.ToString());
            }

        }