示例#1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="socket"></param>
 /// <param name="conferenceName"></param>
 /// <param name="selfUri"></param>
 public TcpDateEvent(Socket socket, ConferenceClientAccept clientIncordingEntity)
 {
     try
     {
         this.Socket = socket;
         this.ClientIncordingEntity = clientIncordingEntity;
     }
     catch (Exception ex)
     {
         LogManage.WriteLog(this.GetType(), ex);
     }
 }
示例#2
0
        /// <summary>
        /// 获取客户端发送过来的数据
        /// </summary>
        /// <param name="obj"></param>
        private void GetRecieveData(object obj)
        {
            try
            {
                //客户端套接字
                if (obj is Socket)
                {
                    Socket clientSocket = obj as Socket;
                    //连接状态
                    while (clientSocket != null &&
                           clientSocket.Connected)
                    {
                        //IPEndPoint remoteEndPoint = (IPEndPoint)client.RemoteEndPoint;//服务器对客户端操作的地址加端口
                        //PC客户端所传数据
                        ConferenceClientAccept clientIncordingEntity = null;

                        List <byte> lists = new List <byte>();
callBack:
                        byte[] buffer = new byte[BUFFER_SIZE];

                        int count = clientSocket.Receive(buffer);//挂起操作


                        if (count == 0)
                        {
                            //客户端与服务器端断开连接
                            break;
                        }
                        if (count == BUFFER_SIZE)
                        {
                            lists.AddRange(buffer);
                        }

                        else if (count < BUFFER_SIZE)
                        {
                            byte[] dataless = new byte[count];
                            Array.Copy(buffer, dataless, count);
                            lists.AddRange(dataless);
                        }
                        if (clientSocket.Available != 0)
                        {
                            goto callBack;
                        }
                        byte[] data = lists.ToArray();
                        lists.Clear();
                        lists = null;

                        if (TCPDataAccroiding != null)
                        {
                            string MobilePhoneIM = Encoding.UTF8.GetString(buffer, 0, count);

                            this.TCPDataAccroiding(MobilePhoneIM, new Action <bool, string, string>((successed, conferenceName, selfUri) =>
                            {
                                if (successed)
                                {
                                    clientIncordingEntity = new ConferenceClientAccept()
                                    {
                                        ConferenceClientAcceptType = ConferenceClientAcceptType.ConferenceAudio, ConferenceName = conferenceName, SelfUri = "_" + selfUri
                                    };

                                    if (this.TCPDataArrival != null)
                                    {
                                        this.TCPDataArrival(new TcpDateEvent(clientSocket, clientIncordingEntity), SocketClientType.Android);
                                    }
                                }
                                else
                                {
                                    using (MemoryStream stream = new MemoryStream(data))
                                    {
                                        stream.Position           = 0;
                                        BinaryFormatter formatter = new BinaryFormatter();
                                        formatter.Binder          = new UBinder();
                                        var result = formatter.Deserialize(stream);
                                        Type type  = result.GetType();
                                        if (type.Equals(typeof(ConferenceClientAccept)))
                                        {
                                            clientIncordingEntity = result as ConferenceClientAccept;
                                            if (this.TCPDataArrival != null)
                                            {
                                                this.TCPDataArrival(new TcpDateEvent(clientSocket, clientIncordingEntity), SocketClientType.PC);
                                            }
                                        }
                                        stream.Flush();
                                    }
                                }
                            }));
                        }
                        else
                        {
                            using (MemoryStream stream = new MemoryStream(data))
                            {
                                stream.Position = 0;
                                BinaryFormatter formatter = new BinaryFormatter();
                                formatter.Binder = new UBinder();
                                var  result = formatter.Deserialize(stream);
                                Type type   = result.GetType();
                                if (type.Equals(typeof(ConferenceClientAccept)))
                                {
                                    clientIncordingEntity = result as ConferenceClientAccept;
                                    if (this.TCPDataArrival != null)
                                    {
                                        this.TCPDataArrival(new TcpDateEvent(clientSocket, clientIncordingEntity), SocketClientType.PC);
                                    }
                                }
                                stream.Flush();
                            }
                        }
                        Array.Clear(data, 0, data.Length);
                        data = null;
                    }
                    try
                    {
                        if (clientSocket != null)
                        {
                            //if (this.TCPDataArrival != null)
                            //{
                            //    this.TCPDataArrival(new TcpDateEvent(clientSocket, new PackageBase() { head = EPackageHead.Discon }));
                            //}
                            //服务器端释放已经断开的客户端连接Socket对象资源
                            Thread.Sleep(300);
                            if (clientSocket.Poll(10, SelectMode.SelectRead))
                            {
                                clientSocket.Shutdown(SocketShutdown.Both);
                            }
                            clientSocket.Close();
                            clientSocket = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManage.WriteLog(this.GetType(), ex);
                    }
                }
            }
            catch (Exception ex)
            {
                //LogManage.WriteLog(this.GetType(), ex);
            }
        }