예제 #1
0
파일: Client.cs 프로젝트: ipocsmr/ipocs.net
        private void ClientReader()
        {
            try
            {
                while (this.Connected)
                {
                    var buffer        = new byte[255];
                    int recievedCount = 0;
                    try
                    {
                        recievedCount = this.TcpClient.GetStream().Read(buffer, 0, 1);
                    }
                    catch { break; }
                    if (0 == recievedCount)
                    {
                        continue;
                    }

                    try
                    {
                        recievedCount += this.TcpClient.GetStream().Read(buffer, 1, buffer[0] - 1);
                    }
                    catch { break; }
                    if (0 == recievedCount)
                    {
                        continue;
                    }

                    // Message received. Parse it.
                    var message = IPOCS.Protocol.Message.create(buffer.Take(recievedCount).ToArray());

                    // If unit has not yet sent a ConnectionRequest
                    if (string.IsNullOrWhiteSpace(this.Name))
                    {
                        var pkt = message.packets.FirstOrDefault((p) => p is Protocol.Packets.ConnectionRequest) as Protocol.Packets.ConnectionRequest;
                        if (pkt == null)
                        {
                            // First message must be a Connection Request
                            break;
                        }

                        this.Name = message.RXID_OBJECT;

                        if (OnConnectionRequest != null)
                        {
                            if (!(OnConnectionRequest?.Invoke(this, pkt)).Value)
                            {
                                break;
                            }
                        }

                        this.StaleTimer.Change(Timeout.Infinite, Timeout.Infinite);

                        var responseMsg = new Protocol.Message();
                        responseMsg.RXID_OBJECT = Name;
                        responseMsg.packets.Add(new IPOCS.Protocol.Packets.ConnectionResponse
                        {
                            RM_PROTOCOL_VERSION = pkt.RM_PROTOCOL_VERSION
                        });
                        this.Send(responseMsg);

                        OnConnect?.Invoke(this);
                    }
                    else
                    {
                        // And if not, hand it to listeners
                        OnMessage?.Invoke(message);
                    }
                }
            } catch (Exception e)
            {
                throw;
            }
            finally
            {
            }
            OnDisconnect?.Invoke(this);
        }
예제 #2
0
 private static void Controller_DisconnectMeasurer(object sender, EventArgs e)
 {
     onDisconnectDelegate?.Invoke();
 }