-3

documentation I was reading

it is written: public Task<SocketMessage> SendReceiveAsync(SocketMessage query)

in source code is: public async Task<SocketMessage> SendReceiveAsync(SocketMessage query){...}

what I'm trying to understand is how do I call it and how it works. anything I try I get errors.

what I'm trying to do is send and receive message from server. I've been working on this for weeks now I've been also reading How and when to use ‘async’ and ‘await’ but I can't manage to get it work.

this is what I managed to make.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Stride.Core.Mathematics;
using Stride.Input;
using Stride.Engine;
using Stride.Engine.Network;
using System.IO;

namespace MyGame
{
    public class Net : SyncScript
    {
        string str = "";
        public Stream socket;
        public SimpleSocket SS;
        public SocketMessage SM;
        public SocketMessageLayer SML;
        public void Server(){
            SML = new SocketMessageLayer(SS, true);
            //int port, bool singleConnection, int retryCount = 1
            SS.StartServer(21, true, 1);
        }
        public void Client(){
            SML = new SocketMessageLayer(SS, false);
            //string address, int port, bool needAck = true
            SS.StartClient("127.0.0.1", 21, true);
        }
        
        
        Task<SocketMessage> SendReceiveAsync(SocketMessage query){
            //here I need to return somehow as I get error: not all code paths return a value
            return query; // Error: Cannot implicitly convert type 'Stride.Engine.Network.SocketMessage' to 'System.Threading.Tasks.Task<Stride.Engine.Network.SocketMessage>'
        }
        public async Task ReceiveMessage(){
            // you still need to get the query from somewhere...
            SocketMessage result = await SML.SendReceiveAsync(SM);

        
            //[abbreviated] // if I uncomment this I get: Error: ; expected
                                                    //Error: Invalid expression term '}'
        }
        
        
        
        
        
        
        public override void Start(){
        }
        public override void Update()
        {
            
            
        }
    }
}

EDIT: inside code I've explained what I tried and what doesn't work

MilitaryG
  • 48
  • 9
  • 1
    Delete the `Task SendReceiveAsync(SocketMessage query);` line, which looks like a local function with no body, and use parentheses after `await SendReceiveAsync` to call the method. Without the parenthesis, this is a method group. – Youssef13 Aug 31 '21 at 09:26

2 Answers2

2

It's giving you an error, as it should, because that is incorrect syntax. What is SendReceiveAsync part supposed to be?

You are not declaring that function anywhere in your class so either:

You declare the function somewhere in your class (outside of your function):

Task<SocketMessage> SendReceiveAsync(SocketMessage query)
{
   //do logic
}

and then you can call it from your function, like you would any other function:

public async Task ReceiveMessage()
{
// still need to provide a query
     SocketMessage result = await SendReceiveAsync(query);
     //int result = await longRunningTask;
     //use the result 
     //Console.WriteLine(result);
}

You reference the class in which this function is declared:

Maybe this function you are trying to use is already declared in one of your class members. Maybe in the SimpleSocket class? If so try this:


public async Task ReceiveMessage()
{
    // you still need to get the query from somewhere...
    SocketMessage result = await SML.SendReceiveAsync(query);


    [abbreviated]
}

What you are trying to do makes little sense, because you are declaring a function inside another function and then attempting to call this function from inside that same function. Furthermore, you are trying to call a function that is not declared anywhere. So it looks like you need to brush up on actual syntax before jumping onto more complicated stuff.

AsPas
  • 359
  • 5
  • 22
0

You must change ReceiveMessage, so the compiler not mean you want to declare a method called SendReceiveAsync. From the documentation you posted you want to call it on SocketMessageLayer so try:

    public async Task<SocketMessage> ReceiveMessage()
    {
        //and now we call await on the task 
        SocketMessage result = await SML.SendReceiveAsync;
        return result;
    }

by the way, according to the documentation SML.Send is also async so you should redefine:

    public async Task SendMessage(string str){
         await SML.Send(str);
    }

Or if you want to call it in a sync way (what I would not recommend) try:

    public void SendMessage(string str){
         SML.Send(str).GetAwaiter().GetResult();
    }

See Calling async method synchronously

Daniel W.
  • 938
  • 8
  • 21
  • here I get errors: [C:\programming\Stride 3D\MyGame\MyGame\Net.cs(38,13)]: Error: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. [C:\programming\Stride 3D\MyGame\MyGame\Net.cs(37,21)]: Error: 'Net.SendMessage(string)': not all code paths return a value [C:\programming\Stride 3D\MyGame\MyGame\Net.cs(42,36)]: Error: Cannot await 'method group' – MilitaryG Aug 31 '21 at 09:50
  • Sorry, the SendMessage (with await) needs to be async while the sync one needs to return void. I changed my samples. – Daniel W. Aug 31 '21 at 11:08