Exemplo n.º 1
0
 private void Run()
 {
     while (true)
     {
         if (_queuePacket.Count == 0)
         {
             Thread.Sleep(1);
             continue;
         }
         MsgPacket packet;
         lock (_queuePacket)
         {
             packet = _queuePacket.Dequeue();
         }
         try
         {
             Socket socket    = _socketClient.Socket;
             byte[] sendBytes = packet.Serialize(_stream);
             int    count     = socket.Send(sendBytes, sendBytes.Length, SocketFlags.None);
             if (count == 0)
             {
                 SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + "socket send packet count is 0");
                 LostConnect();
                 break;
             }
         }
         catch (Exception e)
         {
             SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + e.ToString());
             LostConnect();
             break;
         }
     }
 }
Exemplo n.º 2
0
        private IEnumerator ConnectSocket(EndPoint endPoint)
        {
            _socket      = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            Status       = SocketStatus.Connecting;
            _asyncResult = _socket.BeginConnect(endPoint, AsyncCallback, _socket);
            float curTime   = 0f;
            float totalTime = 10f;

            while (curTime < totalTime && !_asyncResult.IsCompleted)
            {
                curTime += Time.deltaTime;
                yield return(null);
            }
            bool finish = true;

            try
            {
                _socket.EndConnect(_asyncResult);
                _asyncResult = null;
            }
            catch (Exception e)
            {
                SocketTools.Log("SocketClientType:" + SocketClientType + "连接超时" + "\n" + e.ToString());
                finish = false;
            }
            finally
            {
                _asyncResult = null;
            }
            try
            {
                if (finish)
                {
                    Status    = SocketStatus.Connected;
                    _receiver = new SocketReceiver(this);
                    _sender   = new SocketSender(this);
                }
                else
                {
                    Status = SocketStatus.DisConnected;
                }
            }
            catch (Exception e)
            {
                SocketTools.LogError("SocketClientType:" + SocketClientType + "\n" + e.ToString());
                finish = false;
            }
            finally
            {
                if (_callback != null)
                {
                    Action <bool> temp = _callback;
                    _callback = null;
                    temp.Invoke(finish);
                }
            }
            yield return(null);
        }
Exemplo n.º 3
0
 public void Close()
 {
     lock (lockObj)
     {
         _callback = null;
         if (_connectCoroutine != null)
         {
             this.SocketClientMgr.StopCoroutine(_connectCoroutine);
             _connectCoroutine = null;
         }
         if (_socket != null)
         {
             try
             {
                 if (_asyncResult != null)
                 {
                     _socket.EndConnect(_asyncResult);
                     _asyncResult = null;
                 }
                 if (Status == SocketStatus.Connected)
                 {
                     _socket.Shutdown(SocketShutdown.Both);
                 }
             }
             catch (Exception e)
             {
                 SocketTools.LogError(e.ToString());
             }
             finally
             {
                 try
                 {
                     _socket.Close();
                 }
                 catch (Exception ex)
                 {
                     SocketTools.LogError(ex.ToString());
                 }
                 finally
                 {
                     _socket = null;
                     Status  = SocketStatus.DisConnected;
                 }
             }
         }
         if (_receiver != null)
         {
             _receiver.Close();
             _receiver = null;
         }
         if (_sender != null)
         {
             _sender.Close();
             _sender = null;
         }
     }
 }
Exemplo n.º 4
0
 private void Run()
 {
     while (true)
     {
         Socket socket = _socketClient.Socket;
         try
         {
             //获取到数据包头
             int headCount = socket.Receive(packetHeadBuff, packetHeadBuff.Length, SocketFlags.None);
             if (headCount == 0)
             {
                 SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + "receive headData is 0");
                 LostConnect();
                 break;
             }
             else
             {
                 MsgPacket packet     = new MsgPacket(packetHeadBuff, packetLengthBuff, packetIDBuff, packetStatusBuff);
                 int       bodyLength = packet.Length - MsgPacket.PacketHeadSize;
                 if (bodyLength < 0)
                 {
                     SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + "packet body data is <0");
                     LostConnect();
                     break;
                 }
                 byte[] buff = zeroBuff;
                 if (bodyLength != 0)
                 {
                     buff = new byte[bodyLength];
                     int bodyCount = socket.Receive(buff, buff.Length, SocketFlags.None);
                     if (bodyCount == 0)
                     {
                         SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + "receive bodyData is 0");
                         LostConnect();
                         break;
                     }
                 }
                 Type type = _socketClient.SocketClientMgr.GetProtoType(packet.ID);
                 packet.Deserialize(type, buff, _stream);
                 lock (_queuePacket)
                 {
                     _queuePacket.Enqueue(packet);
                 }
             }
         }
         catch (Exception e)
         {
             SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + e.ToString());
             LostConnect();
             break;
         }
     }
 }
        public void SendMsg(SocketClientType socketClientType, ushort ID, byte[] buff)
        {
            SocketClient socketClient;

            _map.TryGetValue(socketClientType, out socketClient);
            if (socketClient != null && socketClient.Status == SocketStatus.Connected)
            {
                MsgPacket packet = new MsgPacket(ID, buff);
                socketClient.SendPacket(packet);
            }
            else
            {
                SocketTools.LogError("socketClientType:" + socketClientType + " is not connected!");
            }
        }
Exemplo n.º 6
0
 public void Deserialize(Type type, byte[] buff, Stream stream)
 {
     this.Buff = buff;
     if (type != null && ProtoBuf.Serializer.NonGeneric.CanSerialize(type))
     {
         try
         {
             stream.Position = 0;
             stream.Write(buff, 0, buff.Length);
             //如果有注册
             stream.Position   = 0;
             this._protoBufObj = ProtoBuf.Serializer.NonGeneric.Deserialize(type, stream);
         }
         catch (Exception e)
         {
             SocketTools.LogError("Deserialize fail,can not match type:" + type + " and PacketID:" + this.ID);
         }
     }
 }
Exemplo n.º 7
0
        public bool BeginConnect(string ip, int port, Action <bool> callback = null)
        {
            if (Status == SocketStatus.Connected)
            {
                SocketTools.LogError("socket is connected!");
                return(false);
            }
            this.Close();
            IPEndPoint ipEndPoint;

            if (CheckEndToPoint(ip, port, out ipEndPoint))
            {
                _callback         = callback;
                _connectCoroutine = this.SocketClientMgr.StartCoroutine(ConnectSocket(ipEndPoint));
                return(true);
            }
            else
            {
                SocketTools.LogError("ip " + ip + " is not correct format!");
                return(false);
            }
        }