예제 #1
0
        static private void OnSendAckComplete(IAsyncResult result)
        {
            CukunityClient cukieClient = result.AsyncState as CukunityClient;

            cukieClient.stream.EndWrite(result);
            cukieClient.BeginRead();
        }
예제 #2
0
    static private void OnAcceptTcpClientComplete(IAsyncResult result)
    {
        CukunityServer server = result.AsyncState as CukunityServer;
        TcpClient      client = server.listener.EndAcceptTcpClient(result);

        Debug.LogWarning("Cukunity: client connected");

        CukunityClient cukieClient = new CukunityClient(client, server);

        // ready for a new client
        server.listener.BeginAcceptTcpClient(OnAcceptTcpClientComplete, server);

        // start  client
        cukieClient.SendAck();
    }
예제 #3
0
        static private void OnReadComplete(IAsyncResult result)
        {
            CukunityClient cukieClient = result.AsyncState as CukunityClient;
            int            byteCount   = cukieClient.stream.EndRead(result);
            string         line;

            // nothing received?
            if (byteCount <= 0)
            {
                cukieClient.Close();
                return;
            }

            // append read data to internal buffer
            cukieClient.bufferedString  += enc.GetString(cukieClient.buffer, cukieClient.bufferUsedCount, byteCount);
            cukieClient.bufferUsedCount += byteCount;

            // can we extract a line from this buffer?
            while ((line = cukieClient.ExtractLine(cukieClient.bufferedString)) != null)
            {
                int lineByteCount = enc.GetByteCount(line);
                cukieClient.bufferedString   = cukieClient.bufferedString.Remove(0, lineByteCount);
                cukieClient.bufferUsedCount -= lineByteCount;

                // process received line
                string response = cukieClient.ProcessLineRequest(line);
                if (response.Length > 0)
                {
                    // respond to client
                    byte[] responseBytes = enc.GetBytes(response + "\n");
                    cukieClient.stream.BeginWrite(responseBytes, 0, responseBytes.Length, null, null);
                }
                else
                {
                    cukieClient.Close();
                    return;
                }
            }

            // next read
            cukieClient.BeginRead();
        }