示例#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);
    }
示例#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); });
    }
示例#3
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);
        }
    }
示例#4
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;
            }
        }
    }
示例#5
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));
    }
示例#6
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);
            }
        }
    }
示例#7
0
    void handle_message(Custom_msg_type _type, Message_obj msg, object message)
    {
        switch (_type)
        {
        case Custom_msg_type.CREATE_PLAYER:
            //make the server sent this back if you do create a player
            //then make our create player function call an ienumerator that
            //yields until we recieve this message. That way we can make
            //create_player only return true iff we create a player.
            break;

        case Custom_msg_type.LOGIN:
            break;

        case Custom_msg_type.SEND_PLAYER_LIST:
            //ignore this for now
            break;

        case Custom_msg_type.SEND_PARTY_LIST:
            party_list = (List <List <string> >)(message);
            if (party_list == null)
            {
                party_list = new List <List <string> >();
            }
            break;

        case Custom_msg_type.LEAVE_PARTY:
            break;

        case Custom_msg_type.REQ_JOIN_PARTY:
            if (debug)
            {
                print($"Got a req event with {msg.arg1}");
            }
            request_event(msg.arg1);
            break;

        case Custom_msg_type.INVITE_PLAYER:
            invite_event(msg.arg1);
            break;

        case Custom_msg_type.START_GAME:
            message_event("your game started");
            break;

        case Custom_msg_type.LOGOUT:
            break;

        case Custom_msg_type.MTC:
            message_event(message);
            break;

        case Custom_msg_type.RPC:
            break;

        case Custom_msg_type.CMD:
            break;

        case Custom_msg_type.END_GAME:
            message_event("your game is over");
            break;
        }
    }
示例#8
0
    IEnumerator handle_connect(Custom_msg_type _type, Message_obj msg, int len, int conn_id, int host, int channel_id, byte[] raw_msg)
    {
        switch (_type)
        {
        case Custom_msg_type.CREATE_PLAYER:
            string name_create           = msg.arg1;
            string hashedpassword_create = msg.arg2;

            pm.create_player(name_create, hashedpassword_create);
            break;

        case Custom_msg_type.LOGIN:
            string name            = msg.arg1;
            string hashed_password = msg.arg2;
            print($"login as name {name} and pass {hashed_password}");

            if (!pm.login_player(name, hashed_password, conn_id))
            {
                print("didn't add them");
            }
            //print("logged in");
            break;

        case Custom_msg_type.SEND_PLAYER_LIST:
            //out
            //need a action that sends pm.player_table and pm.party_table

            //              scatch this one

            //pm.inform_lobby_players

            break;

        case Custom_msg_type.LEAVE_PARTY:
            pm.leave_party(conn_id);
            break;

        case Custom_msg_type.REQ_JOIN_PARTY:
            print("request join");
            //requestee
            //action that send request msg
            int requestee = 0;
            if (msg.arg1 != "")
            {
                requestee = pm.get_conn_id(msg.arg1);
            }
            else
            {
                requestee = msg.target_connection;
            }


            print(pm.request_join(conn_id, requestee, send_action(_type, conn_id, requestee, channel_id, host)));
            //pm.request_join
            break;

        case Custom_msg_type.INVITE_PLAYER:
            //invitee
            //action that sends invite msg
            int invitee = 0;
            if (msg.arg1 != "")
            {
                invitee = pm.get_conn_id(msg.arg1);
            }
            else
            {
                invitee = msg.target_connection;
            }
            pm.invite_player(conn_id, invitee, send_action(_type, conn_id, invitee, channel_id, host));


            //pm.invite_player
            break;

        case Custom_msg_type.START_GAME:
            //action that sends start game msg to clients


            pm.start_game(conn_id, start_end_action(Custom_msg_type.START_GAME, channel_id, host));
            //pm.start_game
            break;

        case Custom_msg_type.END_GAME:

            pm.end_game(conn_id, start_end_action(Custom_msg_type.END_GAME, channel_id, host));
            break;

        case Custom_msg_type.LOGOUT:
            print($"loging out {conn_id}");
            pm.logout_player(conn_id);
            break;

        case Custom_msg_type.MTC:
            pm.multicast(conn_id, forward(channel_id, host, raw_msg));
            break;

        case Custom_msg_type.RPC:
            pm.rpc(conn_id, forward(channel_id, host, raw_msg));
            break;

        case Custom_msg_type.CMD:
            pm.command(conn_id, forward(channel_id, host, raw_msg));
            break;
        }
        //print("leaving handle");
        yield return(null);
        //print("back in handle");
    }
示例#9
0
    void handle_message(Custom_msg_type _type, Message_obj msg, object message)
    {
        if (_type == Custom_msg_type.START_GAME)
        {
            in_game = true;
        }
        if (_type == Custom_msg_type.END_GAME)
        {
            in_game = false;
        }
        switch (_type)
        {
        case Custom_msg_type.CREATE_PLAYER:
            //make the server sent this back if you do create a player
            //then make our create player function call an ienumerator that
            //yields until we recieve this message. That way we can make
            //create_player only return true iff we create a player.
            break;

        case Custom_msg_type.LOGIN:
            break;

        case Custom_msg_type.SEND_PLAYER_LIST:
            friend_event?.Invoke((List <connection_struct>)message);
            //ignore this for now
            break;

        case Custom_msg_type.SEND_PARTY_LIST:
            List <String> party_list = (List <string>)(message);
            if (party_list == null)
            {
                party_list = new List <string>();
            }
            print(party_list.Aggregate(String.Concat));
            try { if (!in_game)
                  {
                      party_event(new Party_Names(party_list.First(), party_list.Skip(1).ToList()));
                  }
            } catch (MissingReferenceException e) { print(e); }
            if (debug)
            {
                print($"recieved party_list");
            }
            break;

        case Custom_msg_type.LEAVE_PARTY:
            break;

        case Custom_msg_type.REQ_JOIN_PARTY:
            if (debug)
            {
                print($"Got a req event with {msg.arg1}");
            }
            request_event(msg.arg1);
            break;

        case Custom_msg_type.INVITE_PLAYER:
            invite_event(msg.arg1);
            break;

        case Custom_msg_type.START_GAME:
            //message_event("your game started");
            DateTime serverTime = (DateTime)message;
            TimeSpan oneWayTrip = DateTime.Now - (serverTime.AddHours(-1));
            print(oneWayTrip.TotalSeconds);
            start_event.Invoke(Time.time);
            break;

        case Custom_msg_type.LOGOUT:
            break;

        case Custom_msg_type.MTC:
            message = unformat_bytes <object>((byte[])message);
            message_event(message);
            break;

        case Custom_msg_type.RPC:
            break;

        case Custom_msg_type.CMD:
            break;

        case Custom_msg_type.END_GAME:
            break;

        case Custom_msg_type.FIND_MATCH:
            break;

        case Custom_msg_type.ADD_FRIEND:
            break;

        case Custom_msg_type.SET_PLAYER_INFO:
            pi_event?.Invoke((player_info)message);
            break;

        case Custom_msg_type.GET_PLAYER_INFO:
            break;

        case Custom_msg_type.GET_FRIEND:
            print($"{msg.arg1} wants to be your friend");
            friend_request(msg.arg1);
            break;
        }
    }
示例#10
0
    void handle_connect(Custom_msg_type _type, Message_obj msg, int len, int conn_id, int host, int channel_id, byte[] raw_msg)
    {
        switch (_type)
        {
        case Custom_msg_type.CREATE_PLAYER:
            string name_create           = msg.arg1;
            string hashedpassword_create = msg.arg2;

            if (pm.create_player(name_create, hashedpassword_create))
            {
                send_action(Custom_msg_type.CREATE_PLAYER, 1, conn_id, channel_id, host)();
            }
            else
            {
                send_action(Custom_msg_type.CREATE_PLAYER, 0, conn_id, channel_id, host)();
            }

            break;

        case Custom_msg_type.LOGIN:
            string name            = msg.arg1;
            string hashed_password = msg.arg2;
            GAME   title           = (GAME)msg.target_connection;
            print($"login as name {name} and pass {hashed_password} @ {conn_id}");
            Tuple <bool, List <string> > pi = pm.login_player(name, hashed_password, conn_id, title);
            if (!pi.Item1)
            {
                send_action(Custom_msg_type.LOGIN, 0, conn_id, channel_id, host)();
            }
            else
            {
                foreach (string friend in pi.Item2)
                {
                    send_action(Custom_msg_type.GET_FRIEND, 0, conn_id, channel_id, host, friend)();
                }
                send_action(Custom_msg_type.LOGIN, 1, conn_id, channel_id, host)();
            }
            break;

        case Custom_msg_type.SEND_PLAYER_LIST:
            //out
            //need a action that sends pm.player_table and pm.party_table

            //              scatch this one

            //pm.inform_lobby_players

            break;

        case Custom_msg_type.LEAVE_PARTY:
            pm.leave_party(conn_id);
            break;

        case Custom_msg_type.REQ_JOIN_PARTY:
            print("request join");
            //requestee
            //action that send request msg
            int requestee = 0;
            if (msg.arg1 != "")
            {
                requestee = pm.get_conn_id(msg.arg1);
            }
            else
            {
                requestee = msg.target_connection;
            }


            print(pm.request_join(conn_id, requestee, send_action(_type, conn_id, requestee, channel_id, host)));
            //pm.request_join
            break;

        case Custom_msg_type.INVITE_PLAYER:
            //invitee
            //action that sends invite msg
            int invitee = 0;
            if (msg.arg1 != "")
            {
                invitee = pm.get_conn_id(msg.arg1);
            }
            else
            {
                invitee = msg.target_connection;
            }
            pm.invite_player(conn_id, invitee, send_action(_type, conn_id, invitee, channel_id, host));


            //pm.invite_player
            break;

        case Custom_msg_type.START_GAME:
            //action that sends start game msg to clients

            print($"{conn_id} is trying to start a game");
            pm.start_game(conn_id, start_end_action(Custom_msg_type.START_GAME, channel_id, host));
            //pm.start_game
            break;

        case Custom_msg_type.END_GAME:

            pm.end_game(conn_id, start_end_action(Custom_msg_type.END_GAME, channel_id, host));
            break;

        case Custom_msg_type.FIND_MATCH:
            pm.find_match(conn_id);
            start_end_action(Custom_msg_type.FIND_MATCH, channel_id, conn_id);
            break;

        case Custom_msg_type.LOGOUT:
            print($"loging out {conn_id}");
            pm.logout_player(conn_id);
            break;

        case Custom_msg_type.MTC:
            pm.multicast(conn_id, forward(channel_id, host, raw_msg));
            break;

        case Custom_msg_type.RPC:
            pm.rpc(conn_id, forward(channel_id, host, raw_msg));
            break;

        case Custom_msg_type.CMD:
            pm.command(conn_id, forward(channel_id, host, raw_msg));
            break;

        case Custom_msg_type.SEND_PARTY_LIST:
            break;

        case Custom_msg_type.ADD_FRIEND:
            print($"{conn_id} wants to friend {msg.arg1}");
            Tuple <bool, int> pi2 = pm.send_friend_request(conn_id, msg.arg1);
            if (pi2.Item1)
            {
                send_action(Custom_msg_type.ADD_FRIEND, 1, conn_id, channel_id, host)();
                if (pi2.Item2 != -1)
                {
                    print($"{pi2.Item2} is online");
                    send_action(Custom_msg_type.GET_FRIEND, conn_id, pm.get_conn_id(msg.arg1), channel_id, host)();
                }
            }
            else
            {
                send_action(Custom_msg_type.ADD_FRIEND, 0, conn_id, channel_id, host)();
            }
            break;

        case Custom_msg_type.GET_PLAYER_INFO:
            forward(large_data_channel, host, pm.player_data(host))(new List <int>()
            {
                conn_id
            });
            break;

        case Custom_msg_type.SET_PLAYER_INFO:
            pm.set_player_data(conn_id, raw_msg);
            break;
        }
    }