Esempio n. 1
0
 private void ReceiveBodyData(IAsyncResult ar)
 {
     try
     {
         SocketPackage package = (SocketPackage)ar.AsyncState;
         int           num     = socketTcp.EndReceive(ar);
         if (num > 0)
         {
             package.bodyIndex += num;
             if (package.bodyIndex < package.bodyLength)
             {
                 socketTcp.BeginReceive(package.bodyBuffer, package.bodyIndex, package.bodyLength - package.bodyIndex, SocketFlags.None, ReceiveBodyData, package);
             }
             else
             {
                 string msg = Encoding.UTF8.GetString(package.bodyBuffer);
                 OnReciveMsg(msg);
                 package.ResetData();
                 socketTcp.BeginReceive(package.headBuffer, 0, package.headLength, SocketFlags.None, ReceiveHeadData, package);
             }
         }
         else
         {
             OnDisConnected();
             Clear();
         }
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("RcvBodyError:" + ex.Message, LogLevel.Error);
     }
 }
 public void SendMsg(byte[] data)
 {
     try
     {
         socketUdp.SendTo(data, data.Length, SocketFlags.None, ep);
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("SndMsgError:" + ex.Message, LogLevel.Error);
     }
 }
Esempio n. 3
0
 public void Close()
 {
     if (thread != null)
     {
         thread.Interrupt();
         thread.Abort();
     }
     if (socketUdp != null)
     {
         socketUdp.Close();
     }
     SocketTools.LogMsg("UDP已关闭...", LogLevel.Info);
 }
 public void SendMsg(string msg = "")
 {
     try
     {
         byte[] array = Encoding.UTF8.GetBytes(msg);
         array = Encoding.UTF8.GetBytes(msg);
         socketUdp.SendTo(array, array.Length, SocketFlags.None, ep);
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("SndMsgError:" + ex.Message, LogLevel.Error);
     }
 }
 public void SocketQuit()
 {
     if (thread != null)
     {
         thread.Interrupt();
         thread.Abort();
     }
     if (socketUdp != null)
     {
         socketUdp.Close();
     }
     OnDisConnected();
     SocketTools.LogMsg("UDP已关闭...", LogLevel.Info);
 }
Esempio n. 6
0
    private void SendCallBack(IAsyncResult ar)
    {
        NetworkStream networkStream = (NetworkStream)ar.AsyncState;

        try
        {
            networkStream.EndWrite(ar);
            networkStream.Flush();
            networkStream.Close();
        }
        catch (Exception ex)
        {
            SocketTools.LogMsg("SndMsgError:" + ex.Message, LogLevel.Error);
        }
    }
Esempio n. 7
0
 public void StartRcvData(Socket socket, Action closeCallBack)
 {
     try
     {
         this.socketTcp     = socket;
         this.closeCallBack = closeCallBack;
         OnConnected();
         SocketPackage pEPkg = new SocketPackage();
         socket.BeginReceive(pEPkg.headBuffer, 0, pEPkg.headLength, SocketFlags.None, ReceiveHeadData, pEPkg);
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("StartRcvData:" + ex.Message, LogLevel.Error);
     }
 }
Esempio n. 8
0
 public void StartAsServer(string ip, int port, Action <bool> cb = null)
 {
     try
     {
         serverCallBack = cb;
         socketTcp.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
         socketTcp.Listen(backlog);
         socketTcp.BeginAccept(ClientConnectCallBack, socketTcp);
         SocketTools.LogMsg("Tcp服务端开启成功!正在等待连接......", LogLevel.Info);
         serverCallBack?.Invoke(true);
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("Tcp服务端开启失败:" + ex.Message, LogLevel.Error);
         serverCallBack?.Invoke(false);
     }
 }
Esempio n. 9
0
 public void StartAsClient(int port, Action <bool> cb = null)
 {
     try
     {
         iep = new IPEndPoint(IPAddress.Any, port);
         socketUdp.Bind(iep);
         ep     = iep;
         thread = new Thread(StartClientReceive);
         thread.Start();
         SocketTools.LogMsg("Udp客户端开启成功!", LogLevel.Info);
         cb?.Invoke(true);
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("Udp客户端开启失败!" + ex.Message, LogLevel.Error);
         cb?.Invoke(false);
     }
 }
Esempio n. 10
0
    public void SendMsg(string msg)
    {
        byte[]        data          = SocketTools.PackageLengthInfo(msg);
        NetworkStream networkStream = null;

        try
        {
            networkStream = new NetworkStream(socketTcp);
            if (networkStream.CanWrite)
            {
                networkStream.BeginWrite(data, 0, data.Length, SendCallBack, networkStream);
            }
        }
        catch (Exception ex)
        {
            SocketTools.LogMsg("SndMsgError:" + ex.Message, LogLevel.Error);
        }
    }
Esempio n. 11
0
 public void StartAsServer(int port, Action <bool> cb = null)
 {
     try
     {
         socketUdp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
         iep    = new IPEndPoint(IPAddress.Broadcast, port);
         ep     = iep;
         thread = new Thread(StartServerReceive);
         thread.Start();
         SocketTools.LogMsg("Udp服务端开启成功!", LogLevel.Info);
         cb?.Invoke(true);
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("Udp服务端开启失败!" + ex.Message, LogLevel.Error);
         cb?.Invoke(false);
     }
 }
Esempio n. 12
0
 private void ServerConnectCallBack(IAsyncResult ar)
 {
     try
     {
         socketTcp.EndConnect(ar);
         StartReceiveMsg(socketTcp, delegate
         {
             SocketTools.LogMsg("Tcp服务器断开连接......", LogLevel.Info);
             clientCallBack?.Invoke(false);
         });
         SocketTools.LogMsg("Tcp连接服务器成功!正在接收数据......", LogLevel.Info);
         clientCallBack?.Invoke(true);
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("Tcp客户端关闭:" + ex.Message, LogLevel.Error);
         clientCallBack?.Invoke(false);
     }
 }
Esempio n. 13
0
 public void StartAsClient_Name(string name, int port, Action <bool> cb = null)
 {
     try
     {
         clientCallBack = cb;
         IAsyncResult asyncResult = socketTcp.BeginConnect(new IPEndPoint(Dns.GetHostEntry(name).AddressList[0], port), ServerConnectCallBack, socketTcp);
         bool         flag        = asyncResult.AsyncWaitHandle.WaitOne(overtime, exitContext: true);
         if (!flag)
         {
             Close();
             SocketTools.LogMsg("Tcp客户端连接超时", LogLevel.Error);
             clientCallBack?.Invoke(flag);
         }
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("Tcp客户端启动失败:" + ex.Message, LogLevel.Error);
         clientCallBack?.Invoke(false);
     }
 }
Esempio n. 14
0
 public void StartAsClient(string ip, int port, Action <bool> cb = null)
 {
     TIME_ID   = TimerManager.Instance.StartTimer(Update);
     socketTcp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     try
     {
         clientCallBack = cb;
         IAsyncResult asyncResult = socketTcp.BeginConnect(new IPEndPoint(IPAddress.Parse(ip), port), ServerConnectCallBack, socketTcp);
         bool         flag        = asyncResult.AsyncWaitHandle.WaitOne(overtime, exitContext: true);
         if (!flag)
         {
             Close();
             SocketTools.LogMsg("Tcp客户端连接超时", LogLevel.Error);
             clientCallBack?.Invoke(flag);
         }
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("Tcp客户端启动失败:" + ex.Message, LogLevel.Error);
         clientCallBack?.Invoke(false);
     }
 }
Esempio n. 15
0
 public bool Close()
 {
     try
     {
         serverCallBack = null;
         clientCallBack = null;
         if (socketTcp != null)
         {
             if (socketTcp.Connected)
             {
                 socketTcp.Shutdown(SocketShutdown.Both);
             }
             socketTcp.Close();
         }
         return(true);
     }
     catch (Exception arg)
     {
         SocketTools.LogMsg("Tcp关闭Socket错误:" + arg, LogLevel.Error);
         return(false);
     }
 }
Esempio n. 16
0
 private void ReceiveHeadData(IAsyncResult ar)
 {
     try
     {
         SocketPackage package = (SocketPackage)ar.AsyncState;
         if (socketTcp.Available == 0)
         {
             OnDisConnected();
             Clear();
         }
         else
         {
             int num = socketTcp.EndReceive(ar);
             if (num > 0)
             {
                 package.headIndex += num;
                 if (package.headIndex < package.headLength)
                 {
                     socketTcp.BeginReceive(package.headBuffer, package.headIndex, package.headLength - package.headIndex, SocketFlags.None, ReceiveHeadData, package);
                 }
                 else
                 {
                     package.InitBodyBuffer();
                     socketTcp.BeginReceive(package.bodyBuffer, 0, package.bodyLength, SocketFlags.None, ReceiveBodyData, package);
                 }
             }
             else
             {
                 OnDisConnected();
                 Clear();
             }
         }
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("ReceiveHeadError:" + ex.Message, LogLevel.Error);
     }
 }
Esempio n. 17
0
 private void ClientConnectCallBack(IAsyncResult ar)
 {
     try
     {
         Socket socket  = socketTcp.EndAccept(ar);
         T      session = new T();
         session.StartRcvData(socket, delegate
         {
             if (sessionList.Contains(session))
             {
                 sessionList.Remove(session);
                 SocketTools.LogMsg("客户端断开连接......", LogLevel.Info);
             }
         });
         sessionList.Add(session);
         SocketTools.LogMsg("Tcp连接客户端成功!正在接收数据......", LogLevel.Info);
     }
     catch (Exception ex)
     {
         SocketTools.LogMsg("Tcp服务器关闭:" + ex.Message, LogLevel.Error);
         serverCallBack?.Invoke(false);
     }
     socketTcp.BeginAccept(ClientConnectCallBack, socketTcp);
 }
Esempio n. 18
0
 protected virtual void OnDisConnected()
 {
     SocketTools.LogMsg("Session Disconnected.", LogLevel.Info);
 }
Esempio n. 19
0
 protected virtual void OnReciveMsg(string msg)
 {
     SocketTools.LogMsg("Receive Network Message.", LogLevel.Info);
 }
Esempio n. 20
0
 protected virtual void OnConnected()
 {
     SocketTools.LogMsg("New Seesion Connected.", LogLevel.Info);
 }