Ejemplo n.º 1
0
    void StartReceiving()
    {
        int count = 0;

        //return;
        while (ClientConnected)
        {
            if (networkClient.Connected)
            {
                try
                {
                    count = 0;
                    //stream.ReadTimeout = 1;
                    if (stream.CanRead)
                    {
                        int responseLength = ReadInt(stream);
                        //stream.Read(responseLBytes, 0, sizeof(Int32));
                        //int responseLength = BitConverter.ToInt32(responseLBytes, 0);
                        byte[] responseBytes = new byte[responseLength];
                        //Debug.Log("available bytes in the client: " + responseLength);
                        stream.Read(responseBytes, 0, responseLength);
                        //Array.Reverse(responseBytes);
                        string    responseString = System.Text.Encoding.ASCII.GetString(responseBytes);
                        RemoteCmd cmd            = JsonUtility.FromJson <RemoteCmd>(responseString);
                        handler.OnRemoteCmdReceivedAsync(cmd);
                        handler.EnqueueCmd(responseString);
                        //Debug.Log(responseString);
                    }
                    else
                    {
                        Debug.Log("something wrong with reading cannot read");
                    }
                }
                catch (Exception ex)
                {
                    Debug.Log("Exception Thrown");
                    Debug.Log(ex.Message);
                    //Debug.Log(count);
                    ClientConnected = false;
                    stream.Close();
                    networkClient.Close();
                    //AsyncCallback callback = new AsyncCallback(OnClientConnected);
                    //networkListener.BeginAcceptTcpClient(callback, networkListener);
                }
            }
        }
    }
Ejemplo n.º 2
0
    async void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
    {
        Debug.Log("NetworkConnectedHandler started");
        if (status == AsyncStatus.Completed)
        {
            Debug.Log("NetworkConnectedHandler connected");
            writer = new DataWriter(networkConnection.OutputStream);
            reader = new DataReader(networkConnection.InputStream);
            reader.InputStreamOptions = InputStreamOptions.Partial;
            ConnectionEstablished     = true;
            Connecting = false;

            while (true)
            {
                //Debug.Log("while loop");
                try
                {
                    uint fieldCount = await reader.LoadAsync(sizeof(uint));

                    int  stringLength = ReadInt(reader);
                    uint restLength   = reader.UnconsumedBufferLength;
                    // Debug.Log("unconsumedBufferLength is " + restLength + " stringlengthToread is:" + stringLength);
                    uint ActualStringLength = await reader.LoadAsync((uint)stringLength);

                    uint   byteCollected = 0;
                    string readString    = "";
                    while (byteCollected < stringLength)
                    {
                        readString    += reader.ReadString(ActualStringLength);
                        byteCollected += ActualStringLength;
                        if ((uint)stringLength - byteCollected > 0)
                        {
                            ActualStringLength = await reader.LoadAsync((uint)stringLength - byteCollected);
                        }
                    }
                    RemoteCmd cmd = JsonUtility.FromJson <RemoteCmd>(readString);
                    handler.OnRemoteCmdReceivedAsync(cmd);
                    handler.EnqueueCmd(readString);
                    //Debug.Log("String: " + readString);
                }
                catch (Exception ex)
                {
                    Debug.Log("Connection Lost");
                    //Debug.Log(ex.Message);
                    ConnectionEstablished = false;
                    NeedToWait            = true;
                    //ConnectToServer();
                    return;
                }
            }



            //operation.Completed = new AsyncOperationCompletedHandler<uint>(DataReceivedHandler);
        }
        else
        {
            Debug.Log("Failed to establish connection. Error Code: " + asyncInfo.ErrorCode);
            // In the failure case we'll requeue the data and wait before trying again.
            networkConnection.Dispose();

            Connecting = false;
            NeedToWait = true;
        }
    }