Exemplo n.º 1
0
        public void readCallback(IAsyncResult ar)
        {
            string content = "";

            aceRemoteStateObject state = (aceRemoteStateObject)ar.AsyncState;
            Socket handler             = state.workSocket;

            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                content = state.sb.ToString();

                int endLine = content.IndexOf(settings.endMessageSufix);
                if (endLine > -1)
                {
                    string[] lns = content.Split(settings.endMessageSufix.ToCharArray(),
                                                 StringSplitOptions.RemoveEmptyEntries);

                    foreach (string ln in lns)
                    {
                        if (onServerReceived != null)
                        {
                            onServerReceived(this, new aceRemoteInstanceBaseEventArgs(aceRemoteInstanceBaseEventType.serverReceive, ln, handler));
                        }
                    }

                    //String receivedLine = content.Substring(0, endLine);

                    //state.sb.Remove(0, endLine);
                    //String input = state.sb.ToString();


                    state.sb.Clear();
                    //log("Received: " + content);

                    // handler.Close();
                    // allDone.Set();
                    // all data received
                }
                else
                {
                }

                handler.BeginReceive(state.buffer, 0, settings.bufferSize, 0, new AsyncCallback(readCallback), state);
            }
            else
            {
                //if (state.sb.Length > 1)
                //{
                //    content = state.sb.ToString();
                //    if (onServerReceived != null) onServerReceived(this, new aceRemoteInstanceBaseEventArgs(aceRemoteInstanceBaseEventType.serverReceive, content));


                //}
                // handler.Close();
            }
        }
Exemplo n.º 2
0
        public void receiveCallback(IAsyncResult ar)
        {
            try
            {
                aceRemoteStateObject state = (aceRemoteStateObject)ar.AsyncState;
                Socket socket = state.workSocket;

                int bytesRead = socket.EndReceive(ar);

                if (bytesRead > 0)
                {
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                    /*
                     * socket.BeginReceive(state.buffer, 0, settings.bufferSize, 0, new AsyncCallback(receiveCallback),
                     *                  state);*/
                }
                else
                {
                    // receiveDone.Set();
                }

                if (socket.Available == 0)
                {
                    /* state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                     * receiveDone.Set();
                     * return;*/
                }
            } catch (Exception ex)
            {
                //receiveDone.Set();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Prijem string poruke
        /// </summary>
        /// <returns></returns>
        public string receiveString()
        {
            aceRemoteStateObject state = receive();
            string received            = state.sb.ToString();

            log("Data received: " + received);

            return(received);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Prijem poruke
        /// </summary>
        /// <returns></returns>
        public aceRemoteStateObject receive()
        {
            aceRemoteStateObject state = new aceRemoteStateObject(settings.bufferSize);

            state.workSocket = client;

            client.BeginReceive(state.buffer, 0, settings.bufferSize, 0, new AsyncCallback(receiveCallback), state);

            return(state);
        }
        /// <summary>
        /// Accepts the callback.
        /// </summary>
        /// <param name="ar">The ar.</param>
        public void acceptCallback(IAsyncResult ar)
        {
            allDone.Set();

            aceRemoteStateObject state = (aceRemoteStateObject)ar.AsyncState;
            Socket handler             = client.EndAccept(ar);

            state.workSocket = handler;

            log("Connected [" + handler.LocalEndPoint.ToString() + "]::[" + handler.RemoteEndPoint.ToString() + "]");// handler.RemoteEndPoint.ToString();

            handler.BeginReceive(state.buffer, 0, settings.bufferSize, 0, new AsyncCallback(readCallback), state);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Starts the listening.
        /// </summary>
        public void startListening()
        {
            try
            {
                ipHostInfo = Dns.Resolve(settings.serverIP);
                ipAddress  = ipHostInfo.AddressList[0];
                remoteEP   = new IPEndPoint(ipAddress, settings.serverPort);

                log("Server IP: " + ipAddress.ToString());

                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                client.Bind(remoteEP);
                client.Listen(100);

                while (doLoopListening)
                {
                    allDone.Reset();

                    log("Waiting for a connection ...");

                    aceRemoteStateObject state = new aceRemoteStateObject(settings.bufferSize);

                    client.BeginAccept(new AsyncCallback(acceptCallback), state);



                    // ovde je obrada primljene poruke
                    // String input = state.sb.ToString();

                    //if (onServerReceived != null) onServerReceived(this, new aceRemoteInstanceBaseEventArgs(aceRemoteInstanceBaseEventType.serverReceive, input));


                    allDone.WaitOne();
                }
            }
            catch (Exception e)
            {
                log("Listing failed : " + e.Message);
                return;
            }
        }