This file is indexed.

/usr/include/autobahn/wamp_session.ipp is in autobahn-cpp-dev 17.5.1+git7cc5d37-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) Crossbar.io Technologies GmbH and contributors
//
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////

#include "exceptions.hpp"
#include "wamp_call.hpp"
#include "wamp_event.hpp"
#include "wamp_invocation.hpp"
#include "wamp_message.hpp"
#include "wamp_message_type.hpp"
#include "wamp_publication.hpp"
#include "wamp_registration.hpp"
#include "wamp_register_request.hpp"
#include "wamp_subscribe_request.hpp"
#include "wamp_subscription.hpp"
#include "wamp_transport.hpp"
#include "wamp_unregister_request.hpp"
#include "wamp_unsubscribe_request.hpp"
#include "wamp_authenticate.hpp"
#include "wamp_challenge.hpp"

#include "wamp_auth_utils.hpp"

#if !(defined(_WIN32) || defined(WIN32))
#include <arpa/inet.h>
#include <unistd.h>
#endif

#include <boost/system/error_code.hpp>
#include <cstdint>
#include <exception>
#include <iostream>
#include <sstream>
#include <stdlib.h>

namespace autobahn {

inline wamp_session::wamp_session(
        boost::asio::io_service& io_service,
        bool debug_enabled)
    : m_debug_enabled(debug_enabled)
    , m_io_service(io_service)
    , m_transport()
    , m_request_id(0)
    , m_session_id(0)
    , m_goodbye_sent(false)
    , m_running(false)
{
}

inline wamp_session::~wamp_session()
{
}

inline boost::future<void> wamp_session::start()
{
    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        if (m_running) {
            m_session_start.set_exception(boost::copy_exception(protocol_error("session already started")));
            return;
        }

        if (!m_transport) {
            m_session_start.set_exception(boost::copy_exception(no_transport_error()));
            return;
        }

        m_running = true;
        m_session_start.set_value();
    });

    return m_session_start.get_future();
}

inline boost::future<void> wamp_session::stop()
{
    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        if (!m_running) {
            m_session_stop.set_exception(boost::copy_exception(protocol_error("session already stopped")));
            return;
        }

        if (!m_transport) {
            m_session_start.set_exception(boost::copy_exception(no_transport_error()));
            return;
        }

        if (m_session_id) {
            m_session_stop.set_exception(boost::copy_exception(protocol_error("session still joined")));
            return;
        }

        m_running = false;
        m_session_stop.set_value();
    });

    return m_session_stop.get_future();
}

inline boost::future<uint64_t> wamp_session::join(
        const std::string& realm,
        const std::vector<std::string>& authentication_methods,
        const std::string& authentication_id)
{
    msgpack::zone zone;
    std::unordered_map<std::string, msgpack::object> roles;

    std::unordered_map<std::string, bool> caller_features;
    caller_features["call_timeout"] = true;
    std::unordered_map<std::string, msgpack::object> caller;
    caller["features"] = msgpack::object(caller_features, zone);
    roles["caller"] = msgpack::object(caller, zone);

    std::unordered_map<std::string, bool> callee_features;
    callee_features["call_timeout"] = true;
    std::unordered_map<std::string, msgpack::object> callee;
    callee["features"] = msgpack::object(callee_features, zone);
    roles["callee"] = msgpack::object(callee, zone);

    std::unordered_map<std::string, msgpack::object> publisher;
    roles["publisher"] = msgpack::object(publisher, zone);

    std::unordered_map<std::string, msgpack::object> subscriber;
    roles["subscriber"] = msgpack::object(subscriber, zone);

    std::unordered_map<std::string, msgpack::object> details;
    details["roles"] = msgpack::object(roles, zone);
    details["authmethods"] = msgpack::object(authentication_methods, zone);
    details["authid"] = msgpack::object(authentication_id, zone);

    auto message = std::make_shared<wamp_message>(3, std::move(zone));
    message->set_field(0, static_cast<int>(message_type::HELLO));
    message->set_field(1, realm);
    message->set_field(2, details);

    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        if (m_session_id) {
            m_session_join.set_exception(boost::copy_exception(protocol_error("session already joined")));
            return;
        }

        try {
            send_message(std::move(*message), false);
        } catch (const std::exception& e) {
            m_session_join.set_exception(boost::copy_exception(e));
        }
    });

    return m_session_join.get_future();
}

inline boost::future<std::string> wamp_session::leave(const std::string& reason)
{
    auto message = std::make_shared<wamp_message>(3);
    message->set_field(0, static_cast<int>(message_type::GOODBYE));
    message->set_field(1, std::unordered_map<int, int>() /* No Details */);
    message->set_field(2, reason);

    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        if (m_goodbye_sent) {
            m_session_leave.set_exception(boost::copy_exception(protocol_error("goodbye already sent")));
        }

        try {
            send_message(std::move(*message), false);
            m_goodbye_sent = true;
        } catch (const std::exception& e) {
            m_session_leave.set_exception(boost::copy_exception(e));
        }

        m_session_id = 0;
    });

    return m_session_leave.get_future();
}

inline boost::future<void> wamp_session::publish(const std::string& topic,const wamp_publish_options& options)
{
    uint64_t request_id = ++m_request_id;

    auto message = std::make_shared<wamp_message>(4);
    message->set_field(0, static_cast<int>(message_type::PUBLISH));
    message->set_field(1, request_id);
    message->set_field(2, options);
    message->set_field(3, topic);

    auto result = std::make_shared<boost::promise<void>>();
    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        try {
            send_message(std::move(*message));
            result->set_value();
        } catch (const std::exception& e) {
            result->set_exception(boost::copy_exception(e));
        }
    });

    return result->get_future();
}

template <typename List>
inline boost::future<void> wamp_session::publish(const std::string& topic, const List& arguments,const wamp_publish_options& options)
{
    uint64_t request_id = ++m_request_id;

    auto message = std::make_shared<wamp_message>(5);
    message->set_field(0, static_cast<int>(message_type::PUBLISH));
    message->set_field(1, request_id);
    message->set_field(2, options);
    message->set_field(3, topic);
    message->set_field(4, arguments);

    auto result = std::make_shared<boost::promise<void>>();
    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        try {
            send_message(std::move(*message));
            result->set_value();
        } catch (const std::exception& e) {
            result->set_exception(boost::copy_exception(e));
        }
    });

    return result->get_future();
}

template <typename List, typename Map>
inline boost::future<void> wamp_session::publish(
        const std::string& topic, const List& arguments, const Map& kw_arguments,const wamp_publish_options& options)
{
    uint64_t request_id = ++m_request_id;

    auto message = std::make_shared<wamp_message>(6);
    message->set_field(0, static_cast<int>(message_type::PUBLISH));
    message->set_field(1, request_id);
    message->set_field(2, options);
    message->set_field(3, topic);
    message->set_field(4, arguments);
    message->set_field(5, kw_arguments);

    auto result = std::make_shared<boost::promise<void>>();
    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        try {
            send_message(std::move(*message));
            result->set_value();
        } catch (const std::exception& e) {
            result->set_exception(boost::copy_exception(e));
        }
    });

    return result->get_future();
}

inline boost::future<wamp_subscription> wamp_session::subscribe(
        const std::string& topic,
        const wamp_event_handler& handler,
        const wamp_subscribe_options& options)
{
    uint64_t request_id = ++m_request_id;

    auto message = std::make_shared<wamp_message>(4);
    message->set_field(0, static_cast<int>(message_type::SUBSCRIBE));
    message->set_field(1, request_id);
    message->set_field(2, options);
    message->set_field(3, topic);

    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());
    auto subscribe_request = std::make_shared<wamp_subscribe_request>(handler);

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        try {
            send_message(std::move(*message));
            m_subscribe_requests.emplace(request_id, subscribe_request);
        } catch (const std::exception& e) {
            subscribe_request->response().set_exception(boost::copy_exception(e));
        }
    });

    return subscribe_request->response().get_future();
}

inline boost::future<void> wamp_session::unsubscribe(const wamp_subscription& subscription)
{
    uint64_t request_id = ++m_request_id;

    auto message = std::make_shared<wamp_message>(3);
    message->set_field(0, static_cast<int>(message_type::UNSUBSCRIBE));
    message->set_field(1, request_id);
    message->set_field(2, subscription.id());

    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());
    auto unsubscribe_request = std::make_shared<wamp_unsubscribe_request>(subscription);

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        try {
            send_message(std::move(*message));
            m_unsubscribe_requests.emplace(request_id, unsubscribe_request);
        } catch (const std::exception& e) {
            unsubscribe_request->response().set_exception(boost::copy_exception(e));
        }
    });

    return unsubscribe_request->response().get_future();
}

inline boost::future<wamp_call_result> wamp_session::call(
        const std::string& procedure,
        const wamp_call_options& options)
{
    uint64_t request_id = ++m_request_id;

    auto message = std::make_shared<wamp_message>(4);
    message->set_field(0, static_cast<int>(message_type::CALL));
    message->set_field(1, request_id);
    message->set_field(2, options);
    message->set_field(3, procedure);

    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());
    auto call = std::make_shared<wamp_call>();

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        try {
            send_message(std::move(*message));
            m_calls.emplace(request_id, call);
        } catch (const std::exception& e) {
            call->result().set_exception(boost::copy_exception(e));
        }
    });

    return call->result().get_future();
}

template<typename List>
inline boost::future<wamp_call_result> wamp_session::call(
        const std::string& procedure,
        const List& arguments,
        const wamp_call_options& options)
{
    uint64_t request_id = ++m_request_id;

    auto message = std::make_shared<wamp_message>(5);
    message->set_field(0, static_cast<int>(message_type::CALL));
    message->set_field(1, request_id);
    message->set_field(2, options);
    message->set_field(3, procedure);
    message->set_field(4, arguments);

    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());
    auto call = std::make_shared<wamp_call>();

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        try {
            send_message(std::move(*message));
            m_calls.emplace(request_id, call);
        } catch (const std::exception& e) {
            call->result().set_exception(boost::copy_exception(e));
        }
    });

    return call->result().get_future();
}

template<typename List, typename Map>
inline boost::future<wamp_call_result> wamp_session::call(
        const std::string& procedure,
        const List& arguments,
        const Map& kw_arguments,
        const wamp_call_options& options)
{
    uint64_t request_id = ++m_request_id;

    auto message = std::make_shared<wamp_message>(6);
    message->set_field(0, static_cast<int>(message_type::CALL));
    message->set_field(1, request_id);
    message->set_field(2, options);
    message->set_field(3, procedure);
    message->set_field(4, arguments);
    message->set_field(5, kw_arguments);

    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());
    auto call = std::make_shared<wamp_call>();

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        try {
            send_message(std::move(*message));
            m_calls.emplace(request_id, call);
        } catch (const std::exception& e) {
            call->result().set_exception(boost::copy_exception(e));
        }
    });

    return call->result().get_future();
}

inline boost::future<wamp_registration> wamp_session::provide(
        const std::string& name,
        const wamp_procedure& procedure,
        const provide_options& options)
{
    uint64_t request_id = ++m_request_id;

    auto message = std::make_shared<wamp_message>(4);
    message->set_field(0, static_cast<int>(message_type::REGISTER));
    message->set_field(1, request_id);
    message->set_field(2, options);
    message->set_field(3, name);

    auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());
    auto register_request = std::make_shared<wamp_register_request>(procedure);

    m_io_service.dispatch([=]() {
        auto shared_self = weak_self.lock();
        if (!shared_self) {
            return;
        }

        try {
            send_message(std::move(*message));
            m_register_requests.emplace(request_id, register_request);
        } catch (const std::exception& e) {
            register_request->response().set_exception(boost::copy_exception(e));
        }
    });

    return register_request->response().get_future();
}

inline boost::future<void> wamp_session::unprovide(const wamp_registration& registration)
{
    uint64_t request_id = ++m_request_id;

	auto message = std::make_shared<wamp_message>(3);
	message->set_field(0, static_cast<int>(message_type::UNREGISTER));
	message->set_field(1, request_id);
	message->set_field(2, registration.id());

	auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());
	auto unregister_request = std::make_shared<wamp_unregister_request>(registration);

	m_io_service.dispatch([=]() {
		auto shared_self = weak_self.lock();
		if (!shared_self) {
			return;
		}

		try {
			send_message(std::move(*message));
			m_unregister_requests.emplace(request_id, unregister_request);
		}
		catch (const std::exception& e) {
			unregister_request->response().set_exception(boost::copy_exception(e));
		}
	});

	return unregister_request->response().get_future();
}

inline boost::future<wamp_authenticate> wamp_session::on_challenge(const wamp_challenge& challenge)
{
    // a dummy implementation
    boost::promise<wamp_authenticate> dummy;
    dummy.set_value( wamp_authenticate( "" ) );
    return dummy.get_future();
}

inline void wamp_session::on_attach(const std::shared_ptr<wamp_transport>& transport)
{
    // FIXME: We should be deferring this operation to the io service. This
    //        will almost certainly require us to return a future here to
    //        all the caller to sync up with the actual attaching of the
    //        transport.

    if (m_transport) {
        throw protocol_error("Transport already attached to session");
    }

    // This should never be possible as you cannot start a session without
    // having a transport already attached.
    assert(!m_running);

    m_transport = transport;
}

inline void wamp_session::on_detach(bool was_clean, const std::string& reason)
{
    // FIXME: We should be deferring this operation to the io service. This
    //        will almost certainly require us to return a future here to
    //        all the caller to sync up with the actual detaching of the
    //        transport.

    if (!m_transport) {
        throw protocol_error("Transport already detached from session");
    }

    // FIXME: Figure out what to do if we are detaching a transport
    //        from a session that is still running. Ideally we would
    //        not detach the transport until m_session_stop is satisfied.
    //        Perhaps we could use the same promise/future discussed above.
    //        One side effect here will be if the transport is re-used for
    //        another session as it may still receive messages for the old
    //        session.
    assert(!m_running);

    m_transport.reset();
}

inline void wamp_session::on_message(wamp_message&& message)
{
    // FIXME: Move this check into the transport
    //if (obj.type != msgpack::type::ARRAY) {
    //    throw protocol_error("invalid message structure - message is not an array");
    //}

    if (message.size() < 1) {
        throw protocol_error("invalid message structure - missing message code");
    }

    if (!message.is_field_type(0, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("invalid message code type - not an integer");
    }

    message_type code = static_cast<message_type>(message.field<int>(0));

    switch (code) {
        case message_type::HELLO:
            throw protocol_error("received HELLO message unexpected for WAMP client roles");
        case message_type::WELCOME:
            process_welcome(std::move(message));
            break;
        case message_type::ABORT:
            process_abort(std::move(message));
            break;
        case message_type::CHALLENGE:
            process_challenge(std::move(message));
            break;
        case message_type::AUTHENTICATE:
            throw protocol_error("received AUTHENTICATE message unexpected for WAMP client roles");
        case message_type::GOODBYE:
            process_goodbye(std::move(message));
            break;
        case message_type::HEARTBEAT:
            // FIXME
            break;
        case message_type::ERROR:
            process_error(std::move(message));
            break;
        case message_type::PUBLISH:
            throw protocol_error("received PUBLISH message unexpected for WAMP client roles");
        case message_type::PUBLISHED:
            // FIXME
            break;
        case message_type::SUBSCRIBE:
            throw protocol_error("received SUBSCRIBE message unexpected for WAMP client roles");
        case message_type::SUBSCRIBED:
            process_subscribed(std::move(message));
            break;
        case message_type::UNSUBSCRIBE:
            throw protocol_error("received UNSUBSCRIBE message unexpected for WAMP client roles");
        case message_type::UNSUBSCRIBED:
            process_unsubscribed(std::move(message));
            break;
        case message_type::EVENT:
            process_event(std::move(message));
            break;
        case message_type::CALL:
            throw protocol_error("received CALL message unexpected for WAMP client roles");
        case message_type::CANCEL:
            throw protocol_error("received CANCEL message unexpected for WAMP client roles");
        case message_type::RESULT:
            process_call_result(std::move(message));
            break;
        case message_type::REGISTER:
            throw protocol_error("received REGISTER message unexpected for WAMP client roles");
        case message_type::REGISTERED:
            process_registered(std::move(message));
            break;
        case message_type::UNREGISTER:
            throw protocol_error("received UNREGISTER message unexpected for WAMP client roles");
        case message_type::UNREGISTERED:
            // FIXME
            break;
        case message_type::INVOCATION:
            process_invocation(std::move(message));
            break;
        case message_type::INTERRUPT:
            throw protocol_error("received INTERRUPT message - not implemented");
        case message_type::YIELD:
            throw protocol_error("received YIELD message unexpected for WAMP client roles");
    }
}

inline void wamp_session::process_challenge(wamp_message&& message)
{
    // kind of authentication
    std::string whatAuth = message.field<std::string>(1);

    /////////////////////////////////////////
    // wampcra authentication
    /////////////////////////////////////////

    wamp_challenge challenge_object("");

    if (whatAuth == "wampcra") {
        if (!message.is_field_type(2, msgpack::type::MAP)) {
            throw protocol_error("CHALLENGE - Details must be a dictionary");
        }

        std::string challenge, salt;
        int iterations = 0 , keylen = 0;

        // parse the details, and fill variables above
        try {
            std::unordered_map<std::string, msgpack::object> details;
            message.field(2).convert(details);
            auto itr = details.find("challenge");
            if (itr != details.end()) {
                challenge = itr->second.as<std::string>();
            } else {
                throw protocol_error("wampcra must always introduce a challenge ( in details )");
            }

            itr = details.find("salt");
            if (itr != details.end()) {
                // ok we must salt our secret
                salt = itr->second.as<std::string>();

                itr = details.find("iterations");
                if (itr == details.end()) {
                    throw protocol_error("wampcra must always tell a number of iterations when introducing salting ( in details )");
                }
                iterations = itr->second.as<int>();


                itr = details.find("keylen");
                if (itr == details.end()) {
                    throw protocol_error("wampcra must always tell a key length (keylen) when introducing salting ( in details )");
                }
                keylen = itr->second.as<int>();
            }

            // make the challenge object
            challenge_object = wamp_challenge("wampcra",challenge,salt,iterations,keylen);

        } catch (const std::exception&) {
            if (m_debug_enabled) {
                std::cerr << "failed to parse challenge details" << std::endl;
            }
            throw protocol_error("wampcra authentication: Failed parse challange details");
        };
    /////////////////////////////////////////
    // ticket authentication
    /////////////////////////////////////////
    } else if ( whatAuth == "ticket" ) {
        // make the challenge object
        challenge_object = wamp_challenge("ticket");
    }
    else {
        throw protocol_error("not supported challenge type - can now only handle 'wampcra' and 'ticket'");
    }

    // I am not sure if this is neccesary. Looking at other
    // comments in this code and the examples, it seems like the
    // context_response should live at least until the end of
    // the lambda callback - below
    std::shared_ptr< boost::future< void > > context_response = std::make_shared< boost::future<void> >();

    // call the context, to get a signature...
    (*context_response) = on_challenge(challenge_object).then([=]( boost::future<wamp_authenticate> fu_auth) {
        try {
            const wamp_authenticate sig = fu_auth.get();

            auto message = std::make_shared<wamp_message>(3);
            message->set_field(0, static_cast<int>(message_type::AUTHENTICATE));
            message->set_field(1, sig.signature());
            message->set_field(2, std::unordered_map<int, int>() /* No Extra/Dict */);

            auto weak_self = std::weak_ptr<wamp_session>(this->shared_from_this());
            m_io_service.dispatch([=]() {
                auto shared_self = weak_self.lock();
                if (!shared_self) {
                    return;
                }

                try {
                    send_message(std::move(*message), false);
                } catch (const std::exception&) {
                    if (m_debug_enabled) {
                        std::cerr << "failed to handle authentication" << std::endl;
                    }
                    throw protocol_error("authentication error: failed send signature");
                }
            });

            // make sure the context_response is copied into this lambda...
            context_response.get();
        } catch (const std::exception&) {
            if (m_debug_enabled) {
                std::cerr << "failed to handle authentication" << std::endl;
            }
            throw protocol_error("authentication error: failed send signature");
        }
    });
}

inline void wamp_session::process_welcome(wamp_message&& message)
{
    m_session_id = message.field<uint64_t>(1);
    message.field(2).convert(m_welcome_details);
    m_session_join.set_value(m_session_id);
}

inline void wamp_session::process_abort(wamp_message&& message)
{
    // [ABORT, Details|dict, Reason|uri]

    if (message.size() != 3 ) {
        throw protocol_error("ABORT - length must be 3");
    }

    // Details|dict
    if (!message.is_field_type(1, msgpack::type::MAP)) {
        throw protocol_error("ABORT - Details must be a dictionary");
    }

    // Reason|uri
    if (!message.is_field_type(2, msgpack::type::STR)) {
        throw protocol_error("ABORT - REASON must be a string (URI)");
    }

    std::string uri = message.field<std::string>(2);
    m_session_join.set_exception(boost::copy_exception(abort_error(uri)));
}

inline void wamp_session::process_goodbye(wamp_message&& message)
{
    m_session_id = 0;

    // if we did not initiate closing, reply ..
    if (!m_goodbye_sent) {
        // [GOODBYE, Details|dict, Reason|uri]
        wamp_message goodbye(3);
        goodbye.set_field(0, static_cast<int>(message_type::GOODBYE));
        goodbye.set_field(1, std::unordered_map<int,int>() /* No Details */);
        goodbye.set_field(2, std::string("wamp.error.goodbye_and_out"));

        send_message(std::move(goodbye));
    } else {
        // we previously initiated closing, so this
        // is the peer reply.
    }

    std::string reason = message.field<std::string>(2);
    m_session_leave.set_value(reason);
}

inline void wamp_session::process_error(wamp_message&& message)
{
    // [ERROR, REQUEST.Type|int, REQUEST.Request|id, Details|dict, Error|uri]
    // [ERROR, REQUEST.Type|int, REQUEST.Request|id, Details|dict, Error|uri, Arguments|list]
    // [ERROR, REQUEST.Type|int, REQUEST.Request|id, Details|dict, Error|uri, Arguments|list, ArgumentsKw|dict]

    if (message.size() < 5 || message.size() > 7) {
        throw protocol_error("invalid ERROR message structure - length must be 5, 6 or 7");
    }

    // REQUEST.Type|int
    //
    if (!message.is_field_type(1, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("invalid ERROR message structure - REQUEST.Type must be an integer");
    }
    auto request_type = static_cast<message_type>(message.field<int>(1));

    if (request_type != message_type::CALL &&
         request_type != message_type::REGISTER &&
         request_type != message_type::UNREGISTER &&
         request_type != message_type::PUBLISH &&
         request_type != message_type::SUBSCRIBE &&
         request_type != message_type::UNSUBSCRIBE) {
        throw protocol_error("invalid ERROR message - ERROR.Type must one of CALL, REGISTER, UNREGISTER, SUBSCRIBE, UNSUBSCRIBE");
    }

    // REQUEST.Request|id
    if (!message.is_field_type(2, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("invalid ERROR message structure - REQUEST.Request must be an integer");
    }
    auto request_id = message.field<uint64_t>(2);

    // Details
    if (!message.is_field_type(3, msgpack::type::MAP)) {
        throw protocol_error("invalid ERROR message structure - Details must be a dictionary");
    }

    // Error|uri
    if (!message.is_field_type(4, msgpack::type::STR)) {
        throw protocol_error("invalid ERROR message - Error must be a string (URI)");
    }
    auto error = message.field<std::string>(4);

    // Arguments|list
    if (message.size() > 5) {
        if (!message.is_field_type(5, msgpack::type::ARRAY)) {
            throw protocol_error("invalid ERROR message structure - Arguments must be a list");
        }
    }

    // ArgumentsKw|list
    if (message.size() > 6) {
        if (!message.is_field_type(6, msgpack::type::MAP)) {
            throw protocol_error("invalid ERROR message structure - ArgumentsKw must be a dictionary");
        }
        try {
            auto kw_args = message.field<std::unordered_map<std::string, std::string>>(6);
            const auto itr = kw_args.find("what");
            if (itr != kw_args.end()) {
                error += ": ";
                error += itr->second;
            }
        } catch (const std::exception&) {
            if (m_debug_enabled) {
                std::cerr << "failed to parse error message keyword arguments" << std::endl;
            }

            error += ": unknown exception";
        }
    }

    switch (request_type) {

        case message_type::CALL:
            {
                //
                // process CALL ERROR
                //
                auto call_itr = m_calls.find(request_id);

                if (call_itr != m_calls.end()) {
                    // FIXME: Forward all error info.
                    call_itr->second->result().set_exception(boost::copy_exception(std::runtime_error(error)));
                    m_calls.erase(call_itr);
                } else {
                    throw protocol_error("bogus ERROR message for non-pending CALL request ID: " + error);
                }
            }
            break;
        case message_type::REGISTER:
            {
                auto reg_itr = m_register_requests.find(request_id);
                if (reg_itr != m_register_requests.end())
                {
                    reg_itr->second->response().set_exception(boost::copy_exception(std::runtime_error(error)));
                    m_register_requests.erase(reg_itr);
                } else {
                    throw protocol_error("bogus ERROR message for non-pending REGISTER request ID: " + error);
                }
            }
            break;
        case message_type::UNREGISTER:
            {
                auto unreg_itr = m_unregister_requests.find(request_id);
                if (unreg_itr != m_unregister_requests.end())
                {
                   unreg_itr->second->response().set_exception(boost::copy_exception(std::runtime_error(error)));
                   m_unregister_requests.erase(unreg_itr);
                } else {
                    throw protocol_error("bogus ERROR message for non-pending UNREGISTER request ID: " + error);
                }
            }
            break;
        case message_type::PUBLISH:
            {
                //TODO: there's currently no way to get to the future returned by the publish function
                // but there needs to be something more sensibly then just propagating the error to the
                // function running the io_service
                throw protocol_error("Received ERROR for a PUBLISH request: " + error);
            }
            break;
        case message_type::SUBSCRIBE:
            {
                auto sub_itr = m_subscribe_requests.find(request_id);
                if (sub_itr != m_subscribe_requests.end())
                {
                    sub_itr->second->response().set_exception(boost::copy_exception(std::runtime_error(error)));
                    m_subscribe_requests.erase(sub_itr);
                } else {
                    throw protocol_error("bogus ERROR message for non-pending SUBSCRIBE request ID: " + error);
                }
            }
            break;
        case message_type::UNSUBSCRIBE:
            {
                auto unsub_itr = m_unsubscribe_requests.find(request_id);
                if (unsub_itr != m_unsubscribe_requests.end())
                {
                    unsub_itr->second->response().set_exception(boost::copy_exception(std::runtime_error(error)));
                    m_unsubscribe_requests.erase(unsub_itr);
                } else {
                    throw protocol_error("bogus ERROR message for non-pending UNSUBSCRIBE request ID: " + error);
                }
            }
            break;
        default:
            throw protocol_error("unhandled ERROR message: " + error);
            break;
    }
}


inline void wamp_session::process_invocation(wamp_message&& message)
{
    // [INVOCATION, Request|id, REGISTERED.Registration|id, Details|dict]
    // [INVOCATION, Request|id, REGISTERED.Registration|id, Details|dict, CALL.Arguments|list]
    // [INVOCATION, Request|id, REGISTERED.Registration|id, Details|dict, CALL.Arguments|list, CALL.ArgumentsKw|dict]

    if (message.size() < 4 || message.size() > 6) {
        throw protocol_error("INVOCATION message length must be 4, 5 or 6");
    }

    if (!message.is_field_type(1, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("INVOCATION.Request must be an integer");
    }
    uint64_t request_id = message.field<uint64_t>(1);

    if (!message.is_field_type(2, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("INVOCATION.Registration must be an integer");
    }
    uint64_t registration_id = message.field<uint64_t>(2);

    auto procedure_itr = m_procedures.find(registration_id);
    if (procedure_itr != m_procedures.end()) {
        if (!message.is_field_type(3, msgpack::type::MAP)) {
            throw protocol_error("INVOCATION.Details must be a map");
        }

        wamp_invocation invocation = std::make_shared<wamp_invocation_impl>();
        invocation->set_request_id(request_id);
        invocation->set_details(message.field(3));
        if (message.size() > 4) {
            if (!message.is_field_type(4, msgpack::type::ARRAY)) {
                throw protocol_error("INVOCATION.Arguments must be an array/vector");
            }
            invocation->set_arguments(message.field(4));

            if (message.size() > 5) {
                if (!message.is_field_type(5, msgpack::type::MAP)) {
                    throw protocol_error("INVOCATION.KwArguments must be a map");
                }
                invocation->set_kw_arguments(message.field(5));
            }
        }

        invocation->set_zone(std::move(message.zone()));

        auto weak_this = std::weak_ptr<wamp_session>(this->shared_from_this());

        auto send_result_fn = [weak_this] (const std::shared_ptr<wamp_message>& message) {
            // Make sure the session still exists, since the invocation could run
            // on a different thread.
            auto shared_this = weak_this.lock();
            if (!shared_this) {
                return; // FIXME: or throw exception?
            }

            // Send to the io_service thread, and make sure the session still exists (again).
            shared_this->m_io_service.dispatch([weak_this, message] {
                auto shared_this = weak_this.lock();
                if (!shared_this) {
                    return; // FIXME: or throw exception?
                }
                shared_this->send_message(std::move(*message));
            });
        };

        invocation->set_send_result_fn(std::move(send_result_fn));

        try {
            if (m_debug_enabled) {
                std::cerr << "Invoking procedure registered under " << registration_id << std::endl;
            }
            procedure_itr->second(invocation);
        }

        // FIXME: implement Autobahn-specific exception with error URI
        catch (const std::exception& e) {
            // we can at least describe the error with e.what()
            //
            if (invocation->sendable()) {
                std::map<std::string, std::string> error_kw_arguments;
                error_kw_arguments["what"] = e.what();
                invocation->error("wamp.error.runtime_error", EMPTY_ARGUMENTS, error_kw_arguments);
            }
        }
        catch (...) {
            // no information available on actual error
            //
            if (invocation->sendable()) {
                invocation->error("wamp.error.runtime_error");
            }
        }
    } else {
        throw protocol_error("bogus INVOCATION message for non-registered registration ID");
    }
}

inline void wamp_session::process_call_result(wamp_message&& message)
{
    // [RESULT, CALL.Request|id, Details|dict]
    // [RESULT, CALL.Request|id, Details|dict, YIELD.Arguments|list]
    // [RESULT, CALL.Request|id, Details|dict, YIELD.Arguments|list, YIELD.ArgumentsKw|dict]

    if (message.size() < 3 || message.size() > 5) {
        throw protocol_error("RESULT - length must be 3, 4 or 5");
    }

    if (!message.is_field_type(1, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("RESULT - CALL.Request must be an id");
    }
    uint64_t request_id = message.field<uint64_t>(1);

    auto call_itr = m_calls.find(request_id);
    if (call_itr != m_calls.end()) {
        if (!message.is_field_type(2, msgpack::type::MAP)) {
            throw protocol_error("RESULT - Details must be a dictionary");
        }

        wamp_call_result result(std::move(message.zone()));
        if (message.size() > 3) {
            if (!message.is_field_type(3, msgpack::type::ARRAY)) {
                throw protocol_error("RESULT - YIELD.Arguments must be a list");
            }
            result.set_arguments(message.field(3));

            if (message.size() > 4) {
                if (!message.is_field_type(4, msgpack::type::MAP)) {
                    throw protocol_error("RESULT - YIELD.ArgumentsKw must be a dictionary");
                }
                result.set_kw_arguments(message.field(4));
            }
        }
        call_itr->second->set_result(std::move(result));
        m_calls.erase(call_itr);
    } else {
        throw protocol_error("bogus RESULT message for non-pending request ID");
    }
}

inline void wamp_session::process_subscribed(wamp_message&& message)
{
    // [SUBSCRIBED, SUBSCRIBE.Request|id, Subscription|id]
    if (message.size() != 3) {
        throw protocol_error("SUBSCRIBED - length must be 3");
    }

    if (!message.is_field_type(1, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("SUBSCRIBED - SUBSCRIBED.Request must be an integer");
    }
    uint64_t request_id = message.field<uint64_t>(1);

    auto subscribe_request_itr = m_subscribe_requests.find(request_id);
    if (subscribe_request_itr != m_subscribe_requests.end()) {
        if (!message.is_field_type(2, msgpack::type::POSITIVE_INTEGER)) {
            throw protocol_error("SUBSCRIBED - SUBSCRIBED.Subscription must be an integer");
        }

        uint64_t subscription_id = message.field<uint64_t>(2);
        m_subscription_handlers.insert(
                std::make_pair(subscription_id, subscribe_request_itr->second->handler()));
        subscribe_request_itr->second->set_response(wamp_subscription(subscription_id));
        m_subscribe_requests.erase(request_id);
    } else {
        throw protocol_error("SUBSCRIBED - no pending request ID");
    }
}

inline void wamp_session::process_unsubscribed(wamp_message&& message)
{
    // [UNSUBSCRIBED, UNSUBSCRIBE.Request|id]
    if (message.size() != 2) {
        throw protocol_error("UNSUBSCRIBED - length must be 2");
    }

    if (!message.is_field_type(1, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("UNSUBSCRIBED - UNSUBSCRIBED.Request must be an integer");
    }
    uint64_t request_id = message.field<uint64_t>(1);

    auto unsubscribe_request_itr = m_unsubscribe_requests.find(request_id);
    if (unsubscribe_request_itr != m_unsubscribe_requests.end()) {
        uint64_t subscription_id = unsubscribe_request_itr->second->subscription().id();
        m_subscription_handlers.erase(subscription_id);
        unsubscribe_request_itr->second->set_response();
        m_unsubscribe_requests.erase(request_id);
    } else {
        throw protocol_error("UNSUBSCRIBED - no pending request ID");
    }
}

inline void wamp_session::process_event(wamp_message&& message)
{
    // [EVENT, SUBSCRIBED.Subscription|id, PUBLISHED.Publication|id, Details|dict]
    // [EVENT, SUBSCRIBED.Subscription|id, PUBLISHED.Publication|id, Details|dict, PUBLISH.Arguments|list]
    // [EVENT, SUBSCRIBED.Subscription|id, PUBLISHED.Publication|id, Details|dict, PUBLISH.Arguments|list, PUBLISH.ArgumentsKw|dict]

    if (message.size() < 4 || message.size() > 6) {
        throw protocol_error("EVENT - length must be 4, 5 or 6");
    }

    if (!message.is_field_type(1, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("EVENT - SUBSCRIBED.Subscription must be an integer");
    }
    uint64_t subscription_id = message.field<uint64_t>(1);

    auto subscription_handlers_itr = m_subscription_handlers.lower_bound(subscription_id);
    auto subscription_handlers_end = m_subscription_handlers.upper_bound(subscription_id);

    if (subscription_handlers_itr != m_subscription_handlers.end() &&
            subscription_handlers_itr != subscription_handlers_end) {

        if (!message.is_field_type(2, msgpack::type::POSITIVE_INTEGER)) {
            throw protocol_error("EVENT - PUBLISHED.Publication must be an id");
        }
        //uint64_t publication_id = message[2].as<uint64_t>();

        if (!message.is_field_type(3, msgpack::type::MAP)) {
            throw protocol_error("EVENT - Details must be a dictionary");
        }

        wamp_event event(std::move(message.zone()));

        event.set_details(message.field(3));

        if (message.size() > 4) {
            if (!message.is_field_type(4, msgpack::type::ARRAY)) {
                throw protocol_error("EVENT - EVENT.Arguments must be a list");
            }
            event.set_arguments(message.field(4));

            if (message.size() > 5) {
                if (!message.is_field_type(5, msgpack::type::MAP)) {
                    throw protocol_error("EVENT - EVENT.ArgumentsKw must be a dictionary");
                }
                event.set_kw_arguments(message.field(5));
            }
        }

        try {
            // now trigger the user supplied event handler ..
            //
            while (subscription_handlers_itr != subscription_handlers_end) {
                 (subscription_handlers_itr->second)(event);
                 ++subscription_handlers_itr;
            }
        } catch (...) {
            if (m_debug_enabled) {
                std::cerr << "Warning: event handler threw exception" << std::endl;
            }
        }

    } else {
        // silently swallow EVENT for non-existent subscription IDs.
        // We may have just unsubscribed, this EVENT might be have
        // already been in-flight.
        if (m_debug_enabled) {
            std::cerr << "EVENT - non-existent subscription ID " << subscription_id << std::endl;
        }
    }
}

inline void wamp_session::process_registered(wamp_message&& message)
{
    // [REGISTERED, REGISTER.Request|id, Registration|id]

    if (message.size() != 3) {
        throw protocol_error("REGISTERED - length must be 3");
    }

    if (!message.is_field_type(1, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("REGISTERED - REGISTERED.Request must be an integer");
    }
    uint64_t request_id = message.field<uint64_t>(1);

    auto register_request_itr = m_register_requests.find(request_id);
    if (register_request_itr != m_register_requests.end()) {
        if (!message.is_field_type(2, msgpack::type::POSITIVE_INTEGER)) {
            throw protocol_error("REGISTERED - REGISTERED.Registration must be an integer");
        }
        uint64_t registration_id = message.field<uint64_t>(2);

        m_procedures[registration_id] = register_request_itr->second->procedure();
        register_request_itr->second->set_response(wamp_registration(registration_id));
        m_register_requests.erase(register_request_itr);
    } else {
        throw protocol_error("REGISTERED - no pending request ID");
    }
}

inline void wamp_session::process_unregistered(wamp_message&& message)
{
    // [UNREGISTERED, UNREGISTER.Request|id]
    if (message.size() != 2) {
        throw protocol_error("UNREGISTERED - length must be 2");
    }

    if (!message.is_field_type(1, msgpack::type::POSITIVE_INTEGER)) {
        throw protocol_error("UNREGISTERED - UNREGISTERED.Request must be an integer");
    }

    uint64_t request_id = message.field<uint64_t>(1);
	auto unregister_request_itr = m_unregister_requests.find(request_id);
    if (unregister_request_itr != m_unregister_requests.end()) {
        uint64_t registration_id = unregister_request_itr->second->registration().id();
        m_procedures.erase(registration_id);
        unregister_request_itr->second->set_response();
        m_unregister_requests.erase(request_id);
    } else {
        throw protocol_error("UNREGISTERED - no pending request ID");
    }
}

inline void wamp_session::send_message(wamp_message&& message, bool session_established)
{
    if (!m_running) {
        throw protocol_error("session not running");
    }

	if (!m_transport) {
        throw no_transport_error();
    }

	if (!m_transport->is_connected()) {
		throw no_transport_error();
	}

    if (session_established && !m_session_id) {
        throw no_session_error();
    }

    m_transport->send_message(std::move(message));
}

inline const std::unordered_map<std::string, msgpack::object>&  wamp_session::welcome_details()
{
    return m_welcome_details;
}

} // namespace autobahn