Ejemplo n.º 1
0
        private void OnConnect(object sender, SocketAsyncEventArgs e)
        {
            if (e != null && e.SocketError == SocketError.Success)
            {
                //启动发送数据线程
                SendDataService = new SendDataService(this);

                //启动异步接收数据操作
                var state = new AsynSocketArg();
                state.InitializeState(Socket, base.heardLength, false);
                WaitForData(state);
            }
            //处理其他操作
            OnConnectEvent(e);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 参数为true表示释放所有资源,只能由使用者调用
 /// 参数为false表示释放非托管资源,只能由垃圾回收器自动调用
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposed)
     {
         disposed = true;
         if (disposing)
         {
             // TODO: 释放托管资源(托管的对象)。
         }
         // TODO: 释放未托管资源(未托管的对象)
         if (SendDataService != null)
         {
             SendDataService.Dispose();
             SendDataService = null;
         }
         if (IConnected && Socket != null)
         {
             Socket.Close();
             Socket = null;
         }
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 缓冲发送内部数据的接口
 /// </summary>
 /// <param name="message"></param>
 /// <param name="ithrow">失败是否抛出异常</param>
 public void Send(object message, bool ithrow)
 {
     if (ithrow && !IConnected)
     {
         throw new PawayException("Not Connected.");
     }
     if (SendDataService != null && message != null)
     {
         var byteData = message is byte[]? message as byte[] : StructHelper.SerializeObject(message);
         SendDataService.SendData(byteData);
     }
     else if (ithrow)
     {
         if (SendDataService == null)
         {
             throw new PawayException("Send Service is null.");
         }
         if (message == null)
         {
             throw new PawayException("Message Argument can not be empty.");
         }
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 为每个客户端连接创建实例
 /// </summary>
 /// <param name="socket">连接的Socket实例</param>
 /// <param name="heard">头部字节长度</param>
 public SocketPackage(Socket socket, int heard)
 {
     base.heardLength = heard;
     Socket           = socket;
     SendDataService  = new SendDataService(this);
 }