示例#1
0
        void ReceiveCallback(IAsyncResult iar)
        {
            ReceiveStore store = ((ReceiveStore)iar.AsyncState);

            if (store.socket != null)
            {
                int bytesRead = store.socket.EndReceive(iar);

                this.OnNotify(new YMSGNotification(bytesRead.ToString(), null)
                {
                    NotificationType = YMSGNotificationTypes.BytesReceived
                });

                if (bytesRead > 0)
                {
                    pb.AddBytes(store.buffer, bytesRead);
                    if (pb.Ready)
                    {
                        ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object raw)
                        {
                            byte[][] packets = raw as byte[][];
                            foreach (byte[] pd in packets)
                            {
                                YMSGPacket incoming = pd;
                                OnMessageReceived(incoming); // TODO: thread safe?
                            }
                        }), pb.GetPackets());
                    }
                }

                socket.BeginReceive(store.buffer, 0,
                                    store.buffer.Length, SocketFlags.None,
                                    new AsyncCallback(ReceiveCallback), store);
            }
        }
示例#2
0
        void ConnectCallback(IAsyncResult iar)
        {
            Socket s = ((YMSGConnection)iar.AsyncState).socket;

            s.EndConnect(iar);

            if (s.Connected)
            {
                ReceiveStore rs = new ReceiveStore(s)
                {
                    buffer = new byte[s.ReceiveBufferSize]
                };

                s.BeginReceive(rs.buffer, 0,
                               rs.buffer.Length, SocketFlags.None,
                               new AsyncCallback(ReceiveCallback), rs);
            }

            isConnecting.Set();
        }