Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="content"></param>
        /// <param name="destUserId"></param>
        public void Send(string content, int destUserId)
        {
            #region
            TcpClientEx tcpclient = ChatClient.ConnectServer();

            ChatContent contentobj = new ChatContent()
            {
                _FromUID = int.Parse(Logon._User.uid),
                _Text    = content,
                _ToUId   = destUserId
            };

            SendChatContent sendchatcontent = new SendChatContent()
            {
                _Content = contentobj
            };

            byte[] command = sendchatcontent.GetProtocolCommand();

            tcpclient.SendToEndDevice(command);

            //可接收是否发送成功

            /*
             * tcpclient.Receive();
             *
             * RecvUserCheckResult usercheckresult = new RecvUserCheckResult();
             *
             * tcpclient.Dispatcher(usercheckresult);
             */
            tcpclient.Close();

            #endregion
        }
Ejemplo n.º 2
0
        public bool UserLogonRequest()
        {
            #region

            TcpClientEx tcpclient = new TcpClientEx(
                ServerInfor._Ip.ToString(), Convert.ToInt16(ServerInfor._Port));
            SendUserValidCheck senduservalidcheck = new
                                                    SendUserValidCheck()
            {
                _UserInfor = _User
            };
            byte[] command = senduservalidcheck.GetProtocolCommand();
            tcpclient.Connect();
            tcpclient.SendToEndDevice(command);
            tcpclient.Receive();
            RecvUserCheckResult usercheckresult = new RecvUserCheckResult();
            tcpclient.Dispatcher(usercheckresult);
            tcpclient.Close();

            if (usercheckresult._Result._Success)
            {
                string message    = usercheckresult._Result._Message;
                int    splitindex = message.IndexOf("-");
                _User.uid          = message.Substring(0, splitindex);
                _User.userfullName = message.Substring(splitindex + 1, message.Length - splitindex - 1);
            }
            return(usercheckresult._Result._Success);

            #endregion
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        private void requestUserList()
        {
            #region
            TcpClientEx tcpclient = new TcpClientEx(
                ServerInfor._Ip.ToString(), Convert.ToInt16(ServerInfor._Port));

            SendRequstFriendShip sendrequestfriendship =
                new SendRequstFriendShip()
            {
                _UserInfor = Logon._User
            };

            byte[] command = sendrequestfriendship.GetProtocolCommand();

            ExtConsole.WriteByteArray(command);

            tcpclient.Connect();

            tcpclient.SendToEndDevice(command);

            tcpclient.ReceiveFile();

            //RecvUserCheckResult usercheckresult = new RecvUserCheckResult();

            //tcpclient.Dispatcher(usercheckresult);

            //Console.WriteLine(usercheckresult._Result._Message);

            tcpclient.Close();

            #endregion
        }
Ejemplo n.º 4
0
        private void btnStartSend_Click(object sender, EventArgs e)
        {
            MethodInvoker gd = new MethodInvoker(() =>
            {
                TcpClientEx tcpclient = new TcpClientEx("192.168.159.104", 1005);

                SendFileSyn transfilesyn = new SendFileSyn();


                tcpclient.Connect();

                tcpclient.SendToEndDevice(transfilesyn.GetProtocolCommand());

                tcpclient.Receive();

                RecvFileAck transfileack         = new RecvFileAck();
                transfileack.OnStartingDownload += new EventHandler(StartingDownload);
                tcpclient.Dispatcher(transfileack);

                tcpclient.Close();
            });

            gd.BeginInvoke(null, null);
        }
 public override void Disconnect()
 {
     _tcpClient.Close();
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Runs the client connection asynchronously.
        /// </summary>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public async Task RunAsync()
        {
            ///尝试重新连接
            bool isReconnected = true;
            int  reconnectTry  = -1;

            do
            {
                reconnectTry++;
                ByteBuffer = new ByteBuffer();
                if (ServerTcpClient != null)
                {
                    // Take accepted connection from listener
                    tcpClient = ServerTcpClient;
                }
                else
                {
                    // Try to connect to remote host
                    var connectTimeout = TimeSpan.FromTicks(ConnectTimeout.Ticks + (MaxConnectTimeout.Ticks - ConnectTimeout.Ticks) / 20 * Math.Min(reconnectTry, 20));
                    tcpClient = new TcpClientEx(AddressFamily.InterNetwork);
                    tcpClient.ReceiveTimeout    = TcpPackConfig.ReceiveTimeout;
                    tcpClient.SendTimeout       = TcpPackConfig.SendTimeout;
                    tcpClient.SendBufferSize    = TcpPackConfig.SendBufferSize;
                    tcpClient.ReceiveBufferSize = TcpPackConfig.ReceiveBufferSize;


                    Message?.Invoke(this, new AsyncTcpEventArgs("准备连接到服务器" + IPAddress.ToString()));
                    Task connectTask;
                    if (!string.IsNullOrWhiteSpace(HostName))
                    {
                        connectTask = tcpClient.ConnectAsync(HostName, Port);
                    }
                    else
                    {
                        connectTask = tcpClient.ConnectAsync(IPAddress, Port);
                    }
                    var timeoutTask = Task.Delay(connectTimeout);
                    if (await Task.WhenAny(connectTask, timeoutTask) == timeoutTask)
                    {
                        //此处增加一个连接超时的任务
                        Message?.Invoke(this, new AsyncTcpEventArgs("连接超时"));
                        OnConnectedTimeout(isReconnected);//连接超时的返回

                        continue;
                    }
                    try
                    {
                        await connectTask;
                    }
                    catch (Exception ex)
                    {
                        Message?.Invoke(this, new AsyncTcpEventArgs("连接到远程主机时出错", ex));
                        await timeoutTask;
                        continue;
                    }
                }
                reconnectTry = -1;
                stream       = tcpClient.GetStream();


                // 读取直到连接关闭。
                //只有在读取时才能检测到闭合连接,因此我们需要读取
                //永久地,不仅仅是当我们可能使用接收到的数据时。
                var networkReadTask = Task.Run(async() =>
                {
                    // 10 KiB should be enough for every Ethernet packet
                    byte[] buffer = new byte[TcpPackConfig.ByteBufferCapacity];
                    while (true)
                    {
                        int readLength;


                        try
                        {
                            //异步读取有问题
                            readLength = await stream.ReadAsync(buffer, 0, buffer.Length);
                        }
                        catch (IOException ex) when((ex.InnerException as SocketException)?.ErrorCode == (int)SocketError.OperationAborted)
                        {
                            // Warning: This error code number (995) may change
                            Message?.Invoke(this, new AsyncTcpEventArgs("本地关闭连接", ex));
                            readLength = -1;
                        }
                        catch (IOException ex) when((ex.InnerException as SocketException)?.ErrorCode == (int)SocketError.ConnectionAborted)
                        {
                            Message?.Invoke(this, new AsyncTcpEventArgs("连接失败", ex));
                            readLength = -1;
                        }
                        catch (IOException ex) when((ex.InnerException as SocketException)?.ErrorCode == (int)SocketError.ConnectionReset)
                        {
                            Message?.Invoke(this, new AsyncTcpEventArgs("远程重置连接", ex));
                            readLength = -2;
                        }
                        if (readLength <= 0)
                        {
                            if (readLength == 0)
                            {
                                Message?.Invoke(this, new AsyncTcpEventArgs("远程关闭连接"));
                            }
                            closedTcs.TrySetResult(true);
                            OnClosed(readLength != -1);
                            return;
                        }
                        //此处做处理,数据包要达到
                        var segment = new ArraySegment <byte>(buffer, 0, readLength);
                        ByteBuffer.Enqueue(segment);
                        await OnReceivedAsync(readLength);
                    }
                });

                closedTcs = new TaskCompletionSource <bool>();
                await OnConnectedAsync(isReconnected);

                // Wait for closed connection
                await networkReadTask;
                tcpClient.Close();

                isReconnected = true;
            }while (AutoReconnect && ServerTcpClient == null);
        }