Exemplo n.º 1
0
        void OnReceived(IAsyncResult iar)
        {
            //Socket remoteSocket = (Socket)iar.AsyncState; // disposed

            lock (this) {
                if (_socket == null)
                {
                    OnDisconnected();
                }
                else
                {
                    _bReceiving = false;
                    int nBytesRead = 0;
                    try {
                        nBytesRead = _socket.EndReceive(iar);
                    } catch { }

                    if (nBytesRead == 0)
                    {
                        OnDisconnected();
                    }
                    else
                    {
                        string sData = Encoding.UTF8.GetString(_bytes, 0, nBytesRead);
                        Log("IN " + sData);
                        _sBuffer += sData;

                        Srpc.Message srpc = null;
                        do
                        {
                            srpc = ProcessData(ref _sBuffer);
                            if (srpc != null)
                            {
                                string sStatus = srpc.GetString(Srpc.Key.Status);
                                if (!String.IsNullOrEmpty(sStatus))
                                {
                                    HandleResponse(srpc);
                                }
                                else
                                {
                                    HandleRequest(srpc);
                                }
                            }
                        } while (srpc != null);

                        Receive();
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void HandleResponse(Srpc.Message response)
        {
            string sSrpcId = response.GetString(Srpc.Key.SrpcId);

            if (!String.IsNullOrEmpty(sSrpcId))
            {
                if (_completions.ContainsKey(sSrpcId))
                {
                    var fCompletion = _completions[sSrpcId];
                    if (fCompletion != null)
                    {
                        fCompletion(response);
                    }
                    _completions.Remove(sSrpcId);
                }
            }
        }
Exemplo n.º 3
0
        void OnMessage(ref Srpc.Message request, ref Srpc.Message response)
        {
            string sMethod = request.GetString(Srpc.Key.Method);

            switch (sMethod)
            {
            case Protocol.Status.Method: {
                int nConnections = request.GetInt(Protocol.Status.Key.Connections);
                if (nConnections > 0)
                {
                    Invoke(() => _form.ShowConnectedAppStatus());
                }
                else
                {
                    Invoke(() => _form.ShowDisconnectedAppStatus());
                }
            } break;
            }
        }