Esempio n. 1
0
        public async void IncomingMsgAsync(object sender, MsgReceivedEventArgs e)
        {
            //check if message is from this peer
            if (e.RemoteIp == this.LocalIpAddress)
            {
                //from this peer.
                //no futher proccessing needed
                if (forwardAll == false)
                {
                    return;
                }
            }

            //check if its from a new peer
            if (e.BindingType == TransportType.UDP)
            {
                string remotePeeripAddress = e.RemoteIp;
                bool   peerKnown           = DoesPeerExistByIp(remotePeeripAddress);
                if (!peerKnown)
                {
                    //not a known peer
                    await DirectConnectTCPAsync(remotePeeripAddress);
                }
            }

            //check if its a blank UDP packet
            //These are used as heart beats
            if (e.Message.Length <= 0)
            {
                return;
            }

            //trigger sent message
            MsgReceived?.Invoke(this, e);
        }
Esempio n. 2
0
 /// <summary>
 /// Sends a MsgReceived event
 /// </summary>
 /// <param name="msg"></param>
 protected void OnMsgReceived(NetworkMsg msg)
 {
     if (msg is ClientDisconnectMsg)
     {
         OnDisconnected(false);
     }
     else
     {
         MsgReceived?.Invoke(this, new MsgEventArgs(msg));
     }
 }
Esempio n. 3
0
        private void _messageBus_MessageReceived(object sender, TinyMessageReceivedEventArgs e)
        {
            var msg = JsonConvert.DeserializeObject <Command>(Encoding.UTF8.GetString(e.Message));

            if (msg.TargetID != null && msg.TargetID != ID)
            {
                return;
            }

            MsgReceived?.Invoke(this, msg);
            _messages.Enqueue(msg);
        }
Esempio n. 4
0
 private void button2_Click(object sender, EventArgs e)
 {
     vibrate();
     try
     {
         string receiver = myfriendinfo.id;
         //消息的格式是:命令标识符PRIV|发送者的用户名|接收者的用户名|发送内容|时间|
         string message  = "VIBRATE|" + Friends.myInfo.id + "|" + receiver + "|";
         byte[] outbytes = System.Text.Encoding.GetEncoding("gb2312").GetBytes(message.ToCharArray());   //将字符串转化为字符数组
         Friends.Stream.Write(outbytes, 0, outbytes.Length);
         MsgReceived.AppendText("您发送了一个窗口震动\n");
     }
     catch
     {
         MessageBox.Show("网络发生错误!");
     }
 }
Esempio n. 5
0
 private void SendMsg_Click(object sender, EventArgs e)
 {
     try
     {
         string receiver = myfriendinfo.id;
         string time     = DateTime.Now.ToLongTimeString();
         string msg      = MsgToBeSent.Text;
         //消息的格式是:命令标识符PRIV|发送者的用户名|接收者的用户名|发送内容|时间|
         string message  = "PRIV|" + Friends.myInfo.id + "|" + receiver + "|" + msg + "|" + time + "|";
         byte[] outbytes = System.Text.Encoding.GetEncoding("gb2312").GetBytes(message.ToCharArray());   //将字符串转化为字符数组
         Friends.Stream.Write(outbytes, 0, outbytes.Length);
         MsgToBeSent.Clear();
         MsgReceived.AppendText(myid + "(" + time + ")" + "\n" + msg + "\n");
     }
     catch
     {
         MessageBox.Show("网络发生错误!");
     }
 }
Esempio n. 6
0
 private async void StartListening()
 {
     //set timeout for reading
     while (this.IsPeerActive)
     {
         byte[] messageBin = null;
         try
         {
             messageBin = await readUtil.ReadBytesAsync();
         }
         catch
         {
             //lost connection with peer
             this.IsPeerActive = false;
             peerStatusChange?.Invoke(this, new PeerEventArgs(this));
         }
         if (IsPeerActive == true)
         {
             MsgReceived?.Invoke(this, new MsgReceivedEventArgs(socketClient.RemoteAddress, messageBin, TransportType.TCP));
         }
     }
 }
Esempio n. 7
0
 private void IncomingMsg(object sender, MsgReceivedEventArgs e)
 {
     //send message out
     MsgReceived?.Invoke(this, e);
 }
Esempio n. 8
0
 private void NewPeer_MsgReceived(object sender, MsgReceivedEventArgs e)
 {
     MsgReceived?.Invoke(this, e);
 }
Esempio n. 9
0
 private void button1_Click(object sender, EventArgs e)
 {
     MsgReceived.AppendText("---------------以上为历史消息---------------\n");
     this.Close();
 }
Esempio n. 10
0
        /// <summary>
        /// 开启某个会话中的数据接收,直到会话关闭。
        /// 解码错误释放该TcpClient资源
        /// </summary>
        /// <param name="session"></param>
        public async void StartReadMsgAsync(Session session)
        {
            int bufferDataLength = 0;

            byte[] buffer = new byte[1024 * 5];
            try
            {
                while (true)
                {
                    NetworkStream networkStream = session.TcpClient.GetStream();
                    //此次从网络数据流中读取到的数据长度
                    int length = await networkStream.ReadAsync(buffer, bufferDataLength, buffer.Length - bufferDataLength);

                    //若读到了流的末尾(被动关闭了连接??)
                    if (length == 0)
                    {
                        CloseSeesion(session);
                        return;
                    }

                    bufferDataLength += length;                                         //更新缓冲区中表示有效数据长度的变量
                    //开始解析
                    Int32 pointer = 0; byte[] tem;                                      //pointer表示当前解析完的数据长度值
                    for (Int32 theMsgLength = BitConverter.ToInt32(buffer, pointer);
                         (bufferDataLength - pointer) >= theMsgLength + mataDataLength; //当buffer里未解析的数据还可以形成一条消息
                         theMsgLength = BitConverter.ToInt32(buffer, pointer))
                    {
                        pointer += mataDataLength;
                        Array.ConstrainedCopy(buffer, pointer, tem = new byte[theMsgLength], 0, theMsgLength);
                        //根据长度信息尝试解码消息,失败清空缓冲区,并继续接收数据
                        object data = coder.Decode(tem);
                        MsgReceived?.Invoke(this, new NetClientMsgEventArgs(session, Role.Server, data.GetType(), data));

                        pointer += theMsgLength;
                        if (bufferDataLength == pointer)
                        {
                            break;
                        }
                    }
                    //清理缓存中已处理的数据
                    if (pointer == bufferDataLength)
                    {
                        bufferDataLength = 0;
                    }
                    else if (pointer != 0)
                    {
                        Array.Copy(buffer, pointer, buffer, 0, pointer);
                        bufferDataLength -= pointer;
                    }
                }
            }
            catch (Newtonsoft.Json.JsonException)//信息解码错误
            {
                CloseSeesion(session);
                return;
            }
            catch (NetCoderException)
            {
                CloseSeesion(session);
                return;
            }
            catch (System.ObjectDisposedException e)//已关闭连接
            {
                CloseSeesion(session);
            }
            catch (System.IO.IOException e)//被动关闭连接??
            {
                if (e.InnerException is SocketException)
                {
                    CloseSeesion(session);
                }
                else
                {
                    throw e;
                }
            }
            catch (System.InvalidOperationException)
            {
                CloseSeesion(session);
                //throw;
            }
        }
Esempio n. 11
0
 public void Receive(NetworkMsg msg)
 {
     Msg = msg;
     MsgReceived?.Invoke(this, new MsgEventArgs(msg));
 }