Exemplo n.º 1
0
    bool send_message(Custom_msg_type type, string arg1, string arg2, int targetConnection, object body = null)
    {
        byte            error = 0;
        Message_package msg_p = new Message_package();
        Message_obj     msg   = new Message_obj();

        msg_p.type            = type;
        msg.arg1              = arg1;
        msg.arg2              = arg2;
        msg.target_connection = targetConnection;
        if (body == null)
        {
            msg_p.message = msg;
        }
        else
        {
            msg_p.message = body;
        }

        byte[] data = format_data(msg_p);
        NetworkTransport.Send(host, conn_id, reliable_channel, data, data.Length, out error);
        if (debug)
        {
            print($"trying to send {type}: {(NetworkError)error} at channel: {reliable_channel}" +
                  $" on host {host} on conn {conn_id}");
        }
        return((NetworkError)error == NetworkError.Ok);
    }
Exemplo n.º 2
0
    Action send_action(Custom_msg_type _type, int actor, int recipient, int channel_id, int host_id, string name = "")
    {
        byte[]      data = new byte[2048];
        Message_obj msg  = new Message_obj();

        msg.target_connection = actor;
        if (!(_type == Custom_msg_type.CREATE_PLAYER || _type == Custom_msg_type.LOGIN ||
              _type == Custom_msg_type.ADD_FRIEND) && name == "")
        {
            msg.arg1 = pm.get_name(actor); //arg2 could be a custom message?
        }
        else
        {
            msg.arg1 = name;
        }
        print($"sending message of {msg.arg1} to {recipient} with type {_type}");
        Message_package msg_p = new Message_package();

        msg_p.type    = _type;
        msg_p.message = msg;
        data          = format_data(msg_p);
        byte error = (byte)0;

        return(() =>
               { NetworkTransport.Send(host_id, recipient, channel_id, data, data.Length, out error); print((NetworkError)error); });
    }
Exemplo n.º 3
0
    byte[] pack(Custom_msg_type type, object message)
    {
        Message_package msg = new Message_package();

        msg.type    = type;
        msg.message = message;
        return(format_data(msg));
    }
Exemplo n.º 4
0
    IEnumerator Receive(Action connect, Action failure)
    {
        int _conn;
        int _channel;

        byte[]          _buffer = new byte[2048];
        int             data_size;
        byte            error;
        Message_obj     msg   = new Message_obj();
        Message_package msg_p = new Message_package();

        handle_data_event = ScriptableObject.CreateInstance <handle_message_event>();

        while (true)
        {
            NetworkEventType _data = (byte)0;
            while (_data != NetworkEventType.Nothing)
            {
                _data = NetworkTransport.ReceiveFromHost(host, out _conn, out _channel, _buffer, 2048, out data_size, out error);
                switch (_data)
                {
                case NetworkEventType.DataEvent:
                    msg_p = unformat_bytes <Message_package>(_buffer);
                    if (msg_p.message is Message_obj)
                    {
                        msg = (Message_obj)msg_p.message;
                    }
                    print($"received {msg_p.type}");
                    handle_message(msg_p.type, msg, msg_p.message);
                    handle_data_event.Invoke(new Tuple <Custom_msg_type, Message_obj>(msg_p.type, msg), msg_p.message);
                    break;

                case NetworkEventType.ConnectEvent:
                    if (debug)
                    {
                        print($"conected on channel: {_channel}, with error: {(NetworkError)error} on conn: {_conn}");
                    }
                    if (_conn == conn_id)
                    {
                        connected = true;
                    }
                    connect();
                    break;

                case NetworkEventType.DisconnectEvent:
                    if (debug)
                    {
                        print($"didn't connect: {(NetworkError)error}");
                    }
                    failure();
                    break;
                }
            }

            yield return(null);
        }
    }
Exemplo n.º 5
0
    IEnumerator Receive()
    {
        int _conn;
        int _channel;

        byte[]          _buffer = new byte[2048];
        int             data_size;
        byte            error;
        Message_obj     msg   = new Message_obj();
        Message_package msg_p = new Message_package();

        while (true)
        {
            yield return(null);

            NetworkEventType _data =
                NetworkTransport.ReceiveFromHost(host, out _conn, out _channel, _buffer, 2048, out data_size, out error);
            switch (_data)
            {
            case NetworkEventType.DataEvent:
                msg_p = unformat_bytes <Message_package>(_buffer);
                if (msg_p.message is Message_obj)
                {
                    msg = (Message_obj)msg_p.message;
                }
                handle_message(msg_p.type, msg, msg_p.message);
                break;

            case NetworkEventType.ConnectEvent:
                if (debug)
                {
                    print($"conected on channel: {_channel}, with error: {(NetworkError)error} on conn: {_conn}");
                }
                if (_conn == conn_id)
                {
                    connected = true;
                }
                break;

            case NetworkEventType.DisconnectEvent:
                if (debug)
                {
                    print($"didn't connect: {(NetworkError)error}");
                }
                break;
            }
        }
    }
Exemplo n.º 6
0
    Action send_action(Custom_msg_type _type, int actor, int recipient, int channel_id, int host_id)
    {
        byte[]      data = new byte[1024];
        Message_obj msg  = new Message_obj();

        msg.target_connection = actor;
        msg.arg1 = pm.get_name(actor); //arg2 could be a custom message?
        Message_package msg_p = new Message_package();

        msg_p.type    = _type;
        msg_p.message = msg;
        data          = format_data(msg_p);
        byte error = (byte)0;

        return(() => NetworkTransport.Send(host_id, recipient, channel_id, data, data.Length, out error));
    }
Exemplo n.º 7
0
    // f(x) => a = 0; return f(x-1)

    IEnumerator receive()
    {
        int _conn;
        int _channel;

        byte[]           _buffer = new byte[2048];
        int              data_size;
        byte             error;
        NetworkEventType _data = NetworkEventType.DataEvent;

        while (true)
        {
            while (_data != NetworkEventType.Nothing)
            {
                //print("receiving");
                _data =
                    NetworkTransport.ReceiveFromHost(host, out _conn, out _channel, _buffer, 2048, out data_size, out error);
                //print($"recieved {_data}");
                switch (_data)
                {
                case NetworkEventType.Nothing:
                    break;

                case NetworkEventType.ConnectEvent:
                    break;

                case NetworkEventType.DataEvent:     //case on channels here? like state_update channels should just go straight back out
                    Message_package msg_p = unformat_bytes <Message_package>(_buffer);
                    Message_obj     msg   = new Message_obj();
                    if (msg_p.message is Message_obj)
                    {     //make sure msg_p.type is mtc rpc or cmd
                        msg = (Message_obj)msg_p.message;
                    }
                    StartCoroutine(handle_connect(msg_p.type, msg, data_size - 1, _conn, host, _channel, _buffer));
                    _buffer = new byte[1024];
                    break;

                case NetworkEventType.DisconnectEvent:
                    print($"disconnect from {_conn} with error {(NetworkError)error}");
                    pm.logout_player(_conn);
                    break;
                }
                yield return(null);
            }
        }
    }
Exemplo n.º 8
0
    Action <List <int> > start_end_action(Custom_msg_type _type, int channel_id, int host_id)
    {
        byte[]          data  = new byte[1024];
        Message_package msg_p = new Message_package();

        msg_p.type = _type;

        data = format_data(msg_p);

        byte error = 0;

        return(players =>
        {//figure out how to multicast TODO
            foreach (int p in players)
            {
                NetworkTransport.Send(host_id, p, channel_id, data, data.Length, out error);
            }
        });
    }
Exemplo n.º 9
0
    IEnumerator inform_players()
    {
        int send_rate = 2;

        byte[]          data;
        Message_package msg;

        while (true)
        {
            try
            {
                msg         = new Message_package();
                msg.type    = Custom_msg_type.SEND_PARTY_LIST;
                msg.message = pm.get_parties().GroupBy(l => l.FirstOrDefault()).Select(g => g.First()).ToList();
                data        = format_data(msg);

                pm.inform_lobby_players(forward(large_data_channel, host, data));
            }
            catch (NullReferenceException e) { print($"null ref in party search: {e}"); }
            finally { }

            yield return(new WaitForSeconds(send_rate));
        }
    }