示例#1
0
 public void Working(ReceiveEventHandler receive = null)
 {
     FleckLog.Level = LogLevel.Debug;
     allSockets     = new List <IWebSocketConnection>();
     server         = new WebSocketServer("ws://0.0.0.0:7066");
     server.RestartAfterListenError = true;
     server.Start(socket =>
     {
         socket.OnOpen = () =>
         {
             allSockets.Add(socket);
             OnOpen?.BeginInvoke(socket, null, null);
         };
         socket.OnClose = () =>
         {
             allSockets.Remove(socket);
             OnClose?.BeginInvoke(socket, null, null);//通知断开
         };
         socket.OnMessage = message =>
         {
             receive?.BeginInvoke(message, socket, null, null);
         };
         socket.OnError = ex =>
         {
             OnError?.BeginInvoke(ex, socket, null, null);
         };
     });
 }
示例#2
0
        private void Listen()
        {
            int i = 0;

            try
            {
                for (i = 0; i <= MaxConnection; ++i)
                {
                    clientSocket.Add(Accept());
                    OnConnection.BeginInvoke(clientSocket[i].RemoteEndPoint.ToString(), null, null);
                    clientSocket[i].Blocking          = true;
                    clientSocket[i].SendBufferSize    = _SendBufferSize;
                    clientSocket[i].ReceiveBufferSize = _ReceiveBufferSize;

                    Thread t = new Thread(Received);
                    t.IsBackground = true;
                    t.Name         = i.ToString();
                    t.Priority     = ThreadPriority.Lowest;
                    t.Start();
                }
            }
            catch
            {
                Error_Message = "服务器已停止";
                OnError.BeginInvoke(Error_Message, null, null);
                CloseSocket(i);
            }
        }
示例#3
0
文件: Manage.cs 项目: tinshen/pidDev
 static void _configManage_OnError(string Error_Message)
 {
     Error_Messages = Error_Message;
     if (OnError != null)
     {
         OnError.BeginInvoke(Error_Message, null, null);
     }
 }
示例#4
0
 /// <summary>
 /// 根据协议类型获取协议
 /// </summary>
 /// <param name="ProtocolType"></param>
 /// <returns></returns>
 public Base_ProtocolType Get_Base_ProtocolTypeByType(string ProtocolType)
 {
     try
     {
         return(remoteobj.Get_Base_ProtocolTypeByType(ProtocolType));
     }
     catch (Exception ex)
     {
         OnError.BeginInvoke(ex.Message, null, null);
         return(null);
     }
 }
示例#5
0
 /// <summary>
 /// 根据ID获取协议
 /// </summary>
 /// <param name="Protocol_Id"></param>
 /// <returns></returns>
 public Base_ProtocolType Get_Base_ProtocolTypeById(int Protocol_Id)
 {
     try
     {
         return(remoteobj.Get_Base_ProtocolTypeById(Protocol_Id));
     }
     catch (Exception ex)
     {
         OnError.BeginInvoke(ex.Message, null, null);
         return(null);
     }
 }
示例#6
0
 /// <summary>
 /// 获取全部协议
 /// </summary>
 /// <returns></returns>
 public List <Base_ProtocolType> Get_Base_ProtocolType()
 {
     try
     {
         _Base_ProtocolType = remoteobj.Get_Base_ProtocolType();
         return(_Base_ProtocolType);
     }
     catch (Exception ex)
     {
         OnError.BeginInvoke(ex.Message, null, null);
         return(null);
     }
 }
示例#7
0
        private SocketMessage Process(string id, SocketMessage msg)
        {
            try
            {
                TopicMessage b_msg;

                switch (msg.Type)
                {
                case (byte)Net.Model.MessageType.Leave:

                    UserListHelper.Remove(id);
                    break;

                case (byte)Net.Model.MessageType.EnqueueRequest:

                    b_msg = SerializeHelper.ProtolBufDeserialize <TopicMessage>(msg.Content);
                    UserListHelper.GetOrAdd(id, b_msg.Topic);
                    TopicQueueHelper.Enqueue(b_msg);
                    return(new SocketMessage((byte)Net.Model.MessageType.EnqueueResponse, null));

                case (byte)Net.Model.MessageType.DequeueRequest:

                    b_msg = SerializeHelper.ProtolBufDeserialize <TopicMessage>(msg.Content);
                    UserListHelper.GetOrAdd(id, b_msg.Topic);
                    var data = TopicQueueHelper.Dequque(b_msg.Topic);
                    if (data != null)
                    {
                        return(new SocketMessage((byte)Net.Model.MessageType.DequeueResponse, SerializeHelper.ProtolBufSerialize(data)));
                    }
                    else
                    {
                        var rmsg = new TopicMessage(b_msg.Topic, null);
                        return(new SocketMessage((byte)Net.Model.MessageType.DequeueResponse, SerializeHelper.ProtolBufSerialize(rmsg)));
                    }

                case (byte)Net.Model.MessageType.Ping:
                    return(new SocketMessage((byte)Net.Model.MessageType.Pong, null));

                default:
                    _server.CloseClientSocket(id);
                    break;
                }
            }
            catch (Exception ex)
            {
                OnError?.BeginInvoke(id, ex, null, null);
            }

            return(null);
        }
示例#8
0
 /// <summary>
 /// 登录验证,成功返回实体,失败返回NULL
 /// </summary>
 /// <param name="LoginName"></param>
 /// <param name="LoginPass"></param>
 /// <returns></returns>
 public Base_Customers Login(string LoginName, string LoginPass, ref bool IsCon)
 {
     try
     {
         Base_Customers bc = remoteobj.Get_Base_CustomersByLogin(LoginName, LoginPass, ref IsCon);
         IsCon = true;
         return(bc);
     }
     catch (Exception ex)
     {
         IsCon = false;
         OnError.BeginInvoke(ex.Message, null, null);
         return(null);
     }
 }
示例#9
0
        private void Received()
        {
            int          Size = 0;
            MemoryStream ms   = new MemoryStream();

            int i = int.Parse(Thread.CurrentThread.Name);

            try
            {
                while (true)
                {
                    byte[] _byteData = new byte[this.clientSocket[i].ReceiveBufferSize];
                    int    byteSize  = this.clientSocket[i].Receive(_byteData, SocketFlags.None);
                    byte[] _newbyte  = new byte[byteSize];
                    Buffer.BlockCopy(_byteData, 0, _newbyte, 0, byteSize);

                    byte[] _Data = new byte[byteSize];
                    if (Size == 0)
                    {
                        byte[] _PacketDataSize = new byte[100];
                        _Data = new byte[_newbyte.Length - 100];
                        Buffer.BlockCopy(_newbyte, 0, _PacketDataSize, 0, 100);
                        Size = int.Parse(System.Text.ASCIIEncoding.ASCII.GetString(_PacketDataSize).ToString());
                        Buffer.BlockCopy(_newbyte, 100, _Data, 0, _Data.Length);
                    }
                    else
                    {
                        Buffer.BlockCopy(_newbyte, 0, _Data, 0, _Data.Length);
                    }

                    ms.Write(_Data, 0, _Data.Length);
                    if (Size == ms.Length)
                    {
                        this.OnReceivedBigData.BeginInvoke(Deserialize(ms.ToArray()), byteSize, clientSocket[i], i, null, null);
                        ms.Close();
                        ms.Dispose();
                        ms   = new MemoryStream();
                        Size = 0;
                    }
                }
            }
            catch (Exception e)
            {
                Error_Message = e.Message;
                OnError.BeginInvoke(e.Message, null, null);
                CloseSocket(i);
            }
        }
示例#10
0
        /// <summary>
        /// 接收数据
        /// </summary>
        private void Receive()
        {
            int          Size = 0;
            MemoryStream ms   = new MemoryStream();

            byte[] _byte = new byte[ReceiveBufferSize];

            try
            {
                while (true)
                {
                    int    i        = this.Receive(_byte, SocketFlags.None);
                    byte[] _newbyte = new byte[i];
                    Buffer.BlockCopy(_byte, 0, _newbyte, 0, i);

                    byte[] _Data = new byte[i];
                    if (Size == 0)//得到大小之后就不用再去得到
                    {
                        byte[] _PacketDataSize = new byte[100];
                        _Data = new byte[_newbyte.Length - 100];
                        Buffer.BlockCopy(_newbyte, 0, _PacketDataSize, 0, 100);
                        Size = int.Parse(System.Text.ASCIIEncoding.ASCII.GetString(_PacketDataSize).ToString()); //数据大小
                        Buffer.BlockCopy(_newbyte, 100, _Data, 0, _Data.Length);                                 //数据
                    }
                    else
                    {
                        Buffer.BlockCopy(_newbyte, 0, _Data, 0, _Data.Length);      //已经接收到大小,这里就不用再接收。
                    }
                    ms.Write(_Data, 0, _Data.Length);

                    if (Size == ms.Length)//判断是否传送完毕
                    {
                        this.OnReceivedBigData.BeginInvoke(Deserialize(ms.ToArray()), i, this, null, null);
                        ms.Close();
                        ms.Dispose();
                        ms   = new MemoryStream();
                        Size = 0;
                    }
                }
            }
            catch (Exception e)
            {
                Error_Message = e.Message;
                OnError.BeginInvoke(e.Message, null, null);
                OnCloseConnection.BeginInvoke(null, null);
                CloseSocket();
            }
        }
示例#11
0
        /// <summary>
        /// 发送大量数据
        /// </summary>
        /// <param name="Data"></param>
        /// <param name="_ThreadID"></param>
        public void SendTo(Object Data, int _ThreadID)
        {
            try
            {
                byte[] _Data = new byte[this.clientSocket[_ThreadID].SendBufferSize];
                _Data = this.Serialize(Data);
                int    _DataSize       = _Data.Length;
                byte[] _PacketDataSize = System.Text.ASCIIEncoding.ASCII.GetBytes(_DataSize.ToString());
                byte[] _Packet         = new byte[100 + _Data.Length];

                Buffer.BlockCopy(_PacketDataSize, 0, _Packet, 0, _PacketDataSize.Length);
                Buffer.BlockCopy(_Data, 0, _Packet, 100, _Data.Length);
                this.OnSendData.BeginInvoke(this.clientSocket[_ThreadID].Send(_Packet), this.clientSocket[_ThreadID].RemoteEndPoint.ToString(), null, null);
            }
            catch (Exception e)
            {
                Error_Message = e.Message;
                OnError.BeginInvoke(e.Message, null, null);
                CloseSocket(_ThreadID);
            }
        }
示例#12
0
 public static void OnErrorMethod(string error_message)
 {
     OnError.BeginInvoke(error_message, null, null);
 }
示例#13
0
 protected override void HandleError(Exception err)
 {
     OnError?.BeginInvoke(this, err, null, null);
 }