Exemple #1
0
    public void Update()
    {
        switch (_curState)
        {
        case SocketState.idle:
            Setup();
            break;

        case SocketState.connectFaild:
            TryConnect();
            break;

        case SocketState.connecting:
            break;

        case SocketState.connected:
            if (!NowConnected())
            {
                TryConnect();
                return;
            }

            Send();
            Recv();

            break;

        default:
            SDebug.LogError("Socket State Error");
            break;
        }
    }
    private static void WalkDirectoryTree(DirectoryInfo root, string rootDicName)
    {
        FileInfo[]      files   = null;
        DirectoryInfo[] subDirs = null;
        subDirs = root.GetDirectories();
        foreach (DirectoryInfo dirInfo in subDirs)
        {
            WalkDirectoryTree(dirInfo, rootDicName + dirInfo.Name + "/");
        }

        files = root.GetFiles("*.proto");
        if (files != null)
        {
            foreach (FileInfo fi in files)
            {
                string protoFileName = rootDicName + fi.Name;
                SetupArgs(protoFileName);
                _process.Start();

                while (!_process.StandardError.EndOfStream)
                {
                    SDebug.LogError(_process.StandardError.ReadLine());
                }
            }
        }
    }
Exemple #3
0
    private void Recv()
    {
        if (NowConnected() && _socket.Available > 0)
        {
            try {
                int recvSize = Math.Min(_socket.Available, _recvBufferSize - _recvBuffer.Length);
                int ret      = _socket.Receive(_recvBuffer, _recvBuffer.Length, recvSize, SocketFlags.None);

                /* 检测数据接收缓冲区的大小是否大于数据头的大小
                 * 解析数据头,获取后续数据包的大小或者直接返回
                 * 判断数据接收缓冲区剩下的大小(减去数据头的大小)是否大于后续数据包的大小
                 * 解析数据包或者直接返回
                 */

                if (_recvBuffer.Length < _headerSize)
                {
                    return;
                }
            } catch (SocketException e) {
                SDebug.LogError(e.ToString());
                SDebug.LogError("ErrorCode: ", e.ErrorCode);
            } catch (ObjectDisposedException e) {
                SDebug.LogError(e.ToString());
            }
        }
    }
Exemple #4
0
    // 发送数据接口
    public void SendByServerType(ServerType type, byte[] data)
    {
        XYSocketClient sc;

        if (!socketNodeDic.TryGetValue(type, out sc))
        {
            SDebug.LogError("Server Type Not Found");
            return;
        }

        sc.PushSendRequest(data);
    }
Exemple #5
0
    private void OnConnectCallBack(IAsyncResult ar)
    {
        SDebug.Log(string.Format("Connect {0}({1}) {2}", _type, _lep, _socket.Connected ? "Success" : "Faild"));
        if (!_socket.Connected)
        {
            _curState = SocketState.connectFaild;
            return;
        }

        _socket.EndConnect(ar);
        _curState = SocketState.connected;
    }
    static void ReBuild()
    {
        _process = new Process();
        _process.StartInfo.FileName               = _curPath + _fileName;
        _process.StartInfo.UseShellExecute        = false;
        _process.StartInfo.RedirectStandardOutput = true;
        _process.StartInfo.RedirectStandardError  = true;
        _process.StartInfo.CreateNoWindow         = true;

        DirectoryInfo info = new DirectoryInfo(_protoFilePath);

        WalkDirectoryTree(info, "");
        SDebug.Log("Protobuf ReBuild All Completed");
    }
Exemple #7
0
 private void Send()
 {
     if (NowConnected())
     {
         try {
             while (_sendDataQueue.Count > 0)
             {
                 // 发送数据大小检测
                 byte[] data = _sendDataQueue.Dequeue();
                 int    ret  = _socket.Send(data);
             }
         } catch (SocketException e) {
             SDebug.LogError(e.ToString());
             SDebug.LogError("ErrorCode: ", e.ErrorCode);
         } catch (ObjectDisposedException e) {
             SDebug.LogError(e.ToString());
         }
     }
 }
Exemple #8
0
 private void Connect()
 {
     /* 不使用同步Connect的原因
      * 同步的Connect会Block当前线程
      * 可能会出现在尝试Connect的过程中(无网络),长时间Block当前线程
      * 且TCP类型的Connect无法设置为非Block
      */
     try {
         _socket.BeginConnect(_lep, OnConnectCallBack, _socket);
         _curState        = SocketState.connecting;
         _lastConnectTime = Time.realtimeSinceStartup;
     } catch (SocketException e) {
         SDebug.Log(e.ToString());
         SDebug.Log("ErrorCode: ", e.ErrorCode);
         _curState = SocketState.connectFaild;
     } catch (Exception e) {
         SDebug.Log(e.ToString());
         _curState = SocketState.connectFaild;
     }
 }
Exemple #9
0
    private void CloseSocket()
    {
        if (_socket != null)
        {
            try {
                if (_socket.Connected)
                {
                    _socket.Shutdown(SocketShutdown.Both);
                }

                _socket.Close();
            } catch (SocketException e) {
                SDebug.Log(e.ToString());
                SDebug.Log("ErrorCode: ", e.ErrorCode);
            } catch (Exception e) {
                SDebug.Log(e.ToString());
            }

            _socket = null;
        }
    }