Esempio n. 1
0
 public void AsyncDisconnect(CallBackNormal tmpDisconnectBack)
 {
     try
     {
         errorSocket             = ErrorSockect.eSuccess;
         this.callBackDisconnect = tmpDisconnectBack;
         if (clientSocket == null)
         {
             this.callBackDisconnect(false, ErrorSockect.eDisConnectUnkown, "client is null");
         }
         else if (!clientSocket.Connected)
         {
             this.callBackDisconnect(false, ErrorSockect.eDisConnectUnkown, "client is unConnect");
         }
         else
         {
             IAsyncResult asynDisconnet = clientSocket.BeginDisconnect(false, DisconnectCallBack, clientSocket);
             if (WriteDot(asynDisconnet))
             {
                 this.callBackDisconnect(false, ErrorSockect.eDisConnectUnkown, "disconnect failed");
             }
         }
     }
     catch (Exception e)
     {
         this.callBackDisconnect(false, ErrorSockect.eDisConnectUnkown, "disconnect failed");
     }
 }
Esempio n. 2
0
    public void AsyncDisConnect(CallBackNormal tmpConnectBack)
    {
        try
        {
            errorSocket             = ErrorSocket.Sucess;
            this.callBackDisConnect = tmpConnectBack;

            if (clientSocket == null)
            {
                callBackDisConnect(false, ErrorSocket.DisConnectUnKown, "client is null");
            }
            else if (clientSocket.Connected == false)
            {
                callBackDisConnect(false, ErrorSocket.DisConnectUnKown, "client is unconnect");
            }
            else
            {
                IAsyncResult asynDisConnect = clientSocket.BeginDisconnect(false, DisConnectCallBack, clientSocket);
                if (!WriteDot(asynDisConnect))
                {
                    callBackDisConnect(false, ErrorSocket.SendUnSucessUnKown, "Disconnect time out");
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError("e =" + e.StackTrace);
        }
    }
Esempio n. 3
0
 public void AsyncDisconnect(CallBackNormal disconnectBack)
 {
     try
     {
         errorSocket             = ErrorSocket.Success;
         this.callBackDisconnect = disconnectBack;
         if (clientSocket == null)
         {
             callBackDisconnect(false, ErrorSocket.DisConnectUnknown, "socket为空");
         }
         else if (!clientSocket.Connected)
         {
             callBackDisconnect(false, ErrorSocket.DisConnectUnknown, "断开连接");
         }
         else
         {
             IAsyncResult async = clientSocket.BeginDisconnect(false, DisconnectCallBack, clientSocket);
             if (WriteDot(async) == false)
             {
                 callBackDisconnect(false, ErrorSocket.DisConnectUnknown, "超时连接");
             }
         }
     }
     catch
     {
         callBackDisconnect(false, ErrorSocket.DisConnectUnknown, "超时连接");
     }
 }
Esempio n. 4
0
    public void AsynDisconnect(CallBackNormal tmpDisconnect)
    {
        try
        {
            errorSocket             = ErrorSocket.Sucess;
            this.callBackDisConnect = tmpDisconnect;
            if (clientSocket == null)
            {
                this.callBackDisConnect(false, ErrorSocket.DisConnectUnknow, "client is null");
            }
            else if (!clientSocket.Connected)
            {
                this.callBackDisConnect(false, ErrorSocket.DisConnectUnknow, "client is Unconnect");
            }
            else
            {
                IAsyncResult asyDisconnect = clientSocket.BeginDisconnect(false, DisconnectCallBack, clientSocket);

                if (!WriteDot(asyDisconnect))
                {
                    callBackSend(false, ErrorSocket.DisConnectUnknow, "disconnect failed");
                }
            }
        }
        catch (Exception ex)
        {
            callBackSend(false, ErrorSocket.DisConnectUnknow, "disconnect failed");
        }
    }
Esempio n. 5
0
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="callBackConnect">发起连接的回调</param>
        /// <param name="callBackRecv">接收消息回调</param>
        public void AsyncConnect(string ip, ushort port, CallBackNormal callBackConnect, CallBackRecv callBackRecv)
        {
            errorSocket          = ErrorSocket.Success;
            this.callBackConnect = callBackConnect;
            this.callBackRecv    = callBackRecv;


            if (clientSocket != null && clientSocket.Connected)
            {
                this.callBackConnect(false, ErrorSocket.ConnectError, "connect repeat");
            }
            else if (clientSocket == null || !clientSocket.Connected)
            {
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                IPAddress address = IPAddress.Parse(ip);

                IPEndPoint ipep = new IPEndPoint(address, port);

                IAsyncResult ar = clientSocket.BeginConnect(ipep, ConnectCallBack, clientSocket);

                if (!WriteDot(ar))
                {
                    this.callBackConnect(false, errorSocket, "连接超时");
                }
            }
        }
Esempio n. 6
0
        public void AsynSend(List <byte[]> sendBufferList, CallBackNormal tmpSendBack)
        {
            errorSocket       = ErrorSocket.Success;
            this.callBackSend = tmpSendBack;

            if (clientSocket == null)
            {
                this.callBackSend(false, ErrorSocket.SocketNull, "");
            }
            else if (!clientSocket.Connected)
            {
                this.callBackSend(false, ErrorSocket.SendUnSuccessUnKnow, "");
            }
            else
            {
                for (int i = 0; i < sendBufferList.Count; i++)
                {
                    byte[]       sendBuffer = sendBufferList[i];
                    IAsyncResult asySend    = clientSocket.BeginSend(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, SendCallBack, clientSocket);
                    if (!WriteDot(asySend))
                    {
                        this.callBackSend(false, ErrorSocket.SendUnSuccessUnKnow, "send failed");
                    }
                }
            }
        }
Esempio n. 7
0
        public void AsyncDisconnect(CallBackNormal tmpDisconnectBack)
        {
            try
            {
                errorSocket = ErrorSocket.Success;

                this.callBackDisConnect = tmpDisconnectBack;

                if (clientSocket == null)
                {
                    errorSocket = ErrorSocket.DiscConnectUnKnow;
                    this.callBackDisConnect(false, errorSocket, "client is null");
                }
                else if (clientSocket.Connected == false)
                {
                    errorSocket = ErrorSocket.DiscConnectUnKnow;
                    this.callBackDisConnect(false, errorSocket, "client is null");
                }
                else
                {
                    IAsyncResult asySend = clientSocket.BeginDisconnect(false, DisconnectCallBack, clientSocket);

                    if (!WriteDot(asySend))
                    {
                        this.callBackDisConnect(false, ErrorSocket.DiscConnectUnKnow, "超时");
                    }
                }
            }
            catch (Exception e)
            {
                this.callBackDisConnect(false, ErrorSocket.DiscConnectUnKnow, e.ToString());
            }
        }
Esempio n. 8
0
    /// <summary>
    /// 连接服务器
    /// </summary>
    /// <param name="ip"></param>
    /// <param name="port"></param>
    /// <param name="connectBack"></param>
    /// <param name="callBackRecv"></param>
    public void AsyncConnect(string ip, ushort port, CallBackNormal connectBack, CallBackRecv callBackRecv)
    {
        errorSocket = ErrorSocket.Sucess;

        this.callBackConnect = connectBack;
        this.callBackRecv    = callBackRecv;


        if (clientSocket != null && clientSocket.Connected)               // 已经连接
        {
            this.callBackConnect(false, ErrorSocket.ConnetcErrot, "connect repeat!");
        }
        else if (clientSocket == null || !clientSocket.Connected)         // 没有连接则开始连接
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);

            IAsyncResult connent = clientSocket.BeginConnect(endPoint, OnConnectCallback, clientSocket);
            if (!WriteDot(connent))
            {
                this.callBackConnect(false, errorSocket, "连接超时!");
                clientSocket.EndConnect(connent);
            }
        }
    }
Esempio n. 9
0
    public void AsyncConnect(string _ip, ushort _port, CallBackNormal _connectBack, CallBackRecv _callBackRecv)
    {
        errorSocket     = ErrorSocket.Sucess;
        callBackConnect = _connectBack;
        callBackRecv    = _callBackRecv;

        if (clientSocket != null && clientSocket.Connected)
        {
            this.callBackConnect(false, ErrorSocket.ConnectError, "connect repeat");
        }
        else if (clientSocket == null || !clientSocket.Connected)
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress ipAdress = IPAddress.Parse(_ip);

            IPEndPoint endPoint = new IPEndPoint(ipAdress, _port);

            IAsyncResult connect = clientSocket.BeginConnect(endPoint, ConnectCallBack, clientSocket);
            if (!WriteDot(connect))
            {
                callBackConnect(false, errorSocket, "链接超时");
            }
        }
    }
Esempio n. 10
0
    /// <summary>
    /// 断开连接
    /// </summary>
    /// <param name="disconnectBack"></param>
    public void AsyncDisconnect(CallBackNormal disconnectBack)
    {
        try {
            errorSocket = ErrorSocket.Sucess;

            this.callBackDisconnect = disconnectBack;

            if (clientSocket == null)
            {
                this.callBackDisconnect(false, ErrorSocket.DisConnectUnkown, "client is null");
            }
            else if (!clientSocket.Connected)
            {
                this.callBackDisconnect(false, ErrorSocket.DisConnectUnkown, "client is unconnect");
            }
            else
            {
                IAsyncResult asyncDisConnect = clientSocket.BeginDisconnect(false, OnDisconnectCallback, clientSocket);

                if (!WriteDot(asyncDisConnect))
                {
                    this.callBackDisconnect(false, ErrorSocket.DisConnectUnkown, "断开超时");
                }
            }
        } catch (Exception e) {
            this.callBackDisconnect(false, ErrorSocket.DisConnectUnkown, e.ToString());
        }
    }
 public void AsyncDisConnect(CallBackNormal CallBackDisConnect)
 {
     try
     {
         this.DisConnect = CallBackDisConnect;
         if (ClientSocket == null)
         {
             DisConnect(false, ConnectEvent.UnknowError, "Connect is Null");
         }
         else if (!ClientSocket.Connected)
         {
             DisConnect(false, ConnectEvent.UnknowError, "Client  is UnConnect");
         }
         else
         {
             IAsyncResult async = ClientSocket.BeginDisconnect(false, disConnectcallBack, ClientSocket);
             if (!OverTime(async))
             {
                 DisConnect(false, ConnectEvent.UnknowError, "DisConnect OverTime");
             }
         }
     }
     catch (Exception e)
     {
         DisConnect(false, ConnectEvent.UnknowError, e.ToString());
     }
 }
Esempio n. 12
0
    public void AsyncConnnect(string ip, ushort port, CallBackNormal connectBack, CallBackNormal sendBack, CallBackRecv recvBack, CallBackNormal disBack)
    {
        errorSocket = ErrorSocket.Sucess;

        this.callBackConnect = connectBack;

        this.callBackRecv = recvBack;

        this.callBackSend = sendBack;

        this.callBackDisconnect = disBack;




        if (clientSocket != null && clientSocket.Connected)
        {

            this.callBackConnect(false,ErrorSocket.ConnectError,"connect repeat");
        }
        else if (clientSocket == null || !clientSocket.Connected)
        {

            clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            IPAddress ipAdress = IPAddress.Parse(ip);

            IPEndPoint endPoint = new IPEndPoint(ipAdress,port);

           IAsyncResult   connect =     clientSocket.BeginConnect(endPoint, ConnectCallBack,clientSocket);

            // 超时处理
           if (!WriteDot(connect))
           {
               this.callBackConnect(false, errorSocket, "连接超时");
           }



        }


    }
 public void AsyncSend(byte[] SendDate, CallBackNormal AsyncSendcallBack)
 {
     this.CallBackSend = AsyncSendcallBack;
     if (ClientSocket == null)
     {
         CallBackSend(false, ConnectEvent.SendError, "Send failed");
     }
     else if (!ClientSocket.Connected)
     {
         CallBackSend(false, ConnectEvent.SendError, "Send Failed");
     }
     else
     {
         IAsyncResult async = ClientSocket.BeginSend(SendDate, 0, SendDate.Length, SocketFlags.None, SendcallBack, ClientSocket);
         if (!OverTime(async))
         {
             CallBackSend(false, ConnectEvent.SendError, "Send OverTime");
         }
     }
 }
Esempio n. 14
0
 public void AsynSend(byte[] sendBuffer, CallBackNormal tmpSendBack)
 {
     errorSocket       = ErrorSockect.eSuccess;
     this.callBackSend = tmpSendBack;
     if (clientSocket == null)
     {
         this.callBackSend(false, ErrorSockect.eSockectNull, "");
     }
     else if (!clientSocket.Connected)
     {
         callBackSend(false, ErrorSockect.eSockectUnConnect, "");
     }
     else
     {
         IAsyncResult asySend = clientSocket.BeginSend(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, SendCallBack, clientSocket);
         if (!WriteDot(asySend))
         {
             callBackSend(false, ErrorSockect.eSendUnSuccessUnKown, "send failed");
         }
     }
 }
Esempio n. 15
0
 public void AsyncSend(byte[] sendBuffer, CallBackNormal sendBack)
 {
     errorSocket       = ErrorSocket.Success;
     this.callBackSend = sendBack;
     if (clientSocket == null)
     {
         this.callBackSend(false, ErrorSocket.SocketNull, "socket为空");
     }
     else if (!clientSocket.Connected)
     {
         callBackSend(false, ErrorSocket.SocketUnConnect, "没有连接");
     }
     else
     {
         IAsyncResult async = clientSocket.BeginSend(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, SendCallBack, clientSocket);
         if (WriteDot(async) == false)
         {
             callBackSend(false, ErrorSocket.SendUnSuccessUnknown, "发送失败");
         }
     }
 }
    public void AsyncConnect(string IP, ushort port, CallBackNormal callBackConnect, CallBackReceive callBackReceive)
    {
        connectEvent         = ConnectEvent.Success;
        this.CallbackConnect = callBackConnect;
        this.CallbackRev     = callBackReceive;
        if (ClientSocket != null && ClientSocket.Connected)
        {
            this.CallbackConnect(false, ConnectEvent.ConnectError, "Connect Repect");
        }
        else if (ClientSocket == null || !ClientSocket.Connected)
        {
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress iPAddress = IPAddress.Parse(IP);

            IPEndPoint   iPEndPoint   = new IPEndPoint(iPAddress, port);
            IAsyncResult asyncConnect = ClientSocket.BeginConnect(iPEndPoint, ConnectCallBack, ClientSocket);
            if (!OverTime(asyncConnect))
            {
                this.CallbackConnect(false, ConnectEvent.ConnectOverTime, "Connect OverTime");
            }
        }
    }
Esempio n. 17
0
    /// <summary>
    /// 发送消息
    /// </summary>
    /// <param name="sendBuffer"></param>
    /// <param name="sendBack"></param>
    public void AsyncSend(byte[] sendBuffer, CallBackNormal sendBack)
    {
        errorSocket = ErrorSocket.Sucess;

        this.callBackSend = sendBack;

        if (clientSocket == null)
        {
            this.callBackSend(false, ErrorSocket.SocketNull, "");
        }
        else if (!clientSocket.Connected)
        {
            this.callBackSend(false, ErrorSocket.SocketUnConnect, "");
        }
        else
        {
            IAsyncResult asyncSend = clientSocket.BeginSend(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, OnSendCallback, clientSocket);
            if (!WriteDot(asyncSend))
            {
                this.callBackSend(false, ErrorSocket.SendUnSucessUnKnown, "发送超时");
            }
        }
    }