summaryrefslogtreecommitdiff
path: root/csharp/test/Ice/echo/BlobjectI.cs
blob: 34800a7a22de9220f84d808af92ed3fb89ecde55 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Test;

public class BlobjectI : Ice.BlobjectAsync
{

    private class Callback
    {
        public Callback(Ice.AMD_Object_ice_invoke cb, bool twoway)
        {
            _cb = cb;
            _twoway = twoway;
        }

        public void response(bool ok, byte[] encaps)
        {
            _cb.ice_response(ok, encaps);
        }

        public void exception(Ice.Exception ex)
        {
            _cb.ice_exception(ex);
        }

        public void sent(bool sync)
        {
            if(!_twoway)
            {
                _cb.ice_response(true, new byte[0]);
            }
        }

        private Ice.AMD_Object_ice_invoke _cb;
        private bool _twoway;
    }

    public void startBatch()
    {
        Debug.Assert(_batchProxy != null);
        _startBatch = true;
    }

    public void flushBatch()
    {
        Debug.Assert(_batchProxy != null);
        _batchProxy.ice_flushBatchRequests();
        _batchProxy = null;
    }

    public override void
    ice_invoke_async(Ice.AMD_Object_ice_invoke amdCb, byte[] inEncaps, Ice.Current current)
    {
        bool twoway = current.requestId > 0;
        Ice.ObjectPrx obj = current.con.createProxy(current.id);
        if(!twoway)
        {
            if(_startBatch)
            {
                _startBatch = false;
                _batchProxy = obj.ice_batchOneway();
            }
            if(_batchProxy != null)
            {
                obj = _batchProxy;
            }

            if(current.facet.Length != 0)
            {
                obj = obj.ice_facet(current.facet);
            }

            if(_batchProxy != null)
            {
                byte[] outEncaps;
                obj.ice_invoke(current.operation, current.mode, inEncaps, out outEncaps, current.ctx);
                amdCb.ice_response(true, new byte[0]);
            }
            else
            {
                Callback cb = new Callback(amdCb, false);
                obj.ice_oneway().begin_ice_invoke(current.operation,
                                                  current.mode,
                                                  inEncaps,
                                                  current.ctx).whenCompleted(cb.response, cb.exception)
                                                              .whenSent(cb.sent);
            }
        }
        else
        {
            if(current.facet.Length != 0)
            {
                obj = obj.ice_facet(current.facet);
            }
            Callback cb = new Callback(amdCb, true);
            obj.begin_ice_invoke(current.operation,
                                 current.mode,
                                 inEncaps,
                                 current.ctx).whenCompleted(cb.response, cb.exception).whenSent(cb.sent);
        }
    }

    private Ice.ObjectPrx _batchProxy;
    private bool _startBatch;
}