Esempio n. 1
0
        public void Add(MessageType type, StreamBuffer buffer, int size, Action actionOnComplete = null)
        {
            lock (_queue)
            {
                MessageData data = new MessageData();
                data.Type = type;
                data.Buffer = buffer;
                data.Size = size;
                data.ActionOnComplete = actionOnComplete;

                _queue.Add(data);
                Monitor.Pulse(_queue);
            }
        }
Esempio n. 2
0
        public void AddFirst(MessageType type, StreamBuffer buffer, int size)
        {
            lock (_queue)
            {
                MessageData data = new MessageData();
                data.Type = type;
                data.Buffer = buffer;
                data.Size = size;


                _queue.Insert(0, data);
                Monitor.Pulse(_queue);
            }
        }
Esempio n. 3
0
        private void Dispatch(MessageData data)
        {
            switch (data.Type)
            {
                case MessageType.Connect:
                    if (_connector.Connect(HostAddress, HostPortNo) == true)
                        ConnectionStatus = ConnectionStatus.Connected;
                    else
                        ConnectionStatus = ConnectionStatus.Closed;

                    if (NetworkEvent_Connected != null)
                        NetworkEvent_Connected(this, _connector.IsConnected);

                    if (data.ActionOnComplete != null)
                        data.ActionOnComplete();
                    break;


                case MessageType.Close:
                    MQ.Clear();
                    _connector.Close();
                    ConnectionStatus = ConnectionStatus.Closed;

                    if (NetworkEvent_Disconnected != null)
                        NetworkEvent_Disconnected(this);
                    break;


                case MessageType.Disconnect:
                    _connector.Close();
                    ConnectionStatus = ConnectionStatus.Closed;

                    if (NetworkEvent_Disconnected != null)
                        NetworkEvent_Disconnected(this);

                    if (data.ActionOnComplete != null)
                        data.ActionOnComplete();
                    break;


                case MessageType.Send:
                    if (ConnectionStatus == ConnectionStatus.Closed)
                    {
                        ConnectionStatus = ConnectionStatus.Connecting;

                        MQ.AddFirst(data.Type, data.Buffer, data.Size);
                        MQ.AddFirst(MessageType.Connect, null, 0);
                    }

                    else if (ConnectionStatus == ConnectionStatus.Connected)
                    {
                        if (EnableSend == false)
                        {
                            MQ.AddFirst(data.Type, data.Buffer, data.Size);
                            break;
                        }


                        if (_connector.SendPacket(data.Buffer) == true)
                        {
                            if (NetworkEvent_Sent != null)
                                NetworkEvent_Sent(this, data.Size);

                            if (data.ActionOnComplete != null)
                                data.ActionOnComplete();
                        }
                        else
                            Close();
                    }
                    break;


                case MessageType.Receive:
                    NetworkEvent_Received(this, data.Buffer);
                    break;
            }


            if (_stampLastAction == null)
                _stampLastAction = Stopwatch.StartNew();
            else
            {
                _stampLastAction.Reset();
                _stampLastAction.Start();
            }
        }