Drizzled Public API Documentation

state.cc
Go to the documentation of this file.
00001 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00002  *
00003  * Drizzle Client & Protocol Library
00004  *
00005  * Copyright (C) 2008 Eric Day (eday@oddments.org)
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are
00010  * met:
00011  *
00012  *     * Redistributions of source code must retain the above copyright
00013  * notice, this list of conditions and the following disclaimer.
00014  *
00015  *     * Redistributions in binary form must reproduce the above
00016  * copyright notice, this list of conditions and the following disclaimer
00017  * in the documentation and/or other materials provided with the
00018  * distribution.
00019  *
00020  *     * The names of its contributors may not be used to endorse or
00021  * promote products derived from this software without specific prior
00022  * written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00027  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00043 #include <libdrizzle/common.h>
00044 
00045 drizzle_return_t drizzle_state_loop(drizzle_con_st *con)
00046 {
00047   if (con == NULL)
00048   {
00049     return DRIZZLE_RETURN_INVALID_ARGUMENT;
00050   }
00051 
00052   while (drizzle_state_none(con) == false)
00053   {
00054     drizzle_return_t ret= con->state_stack[con->state_current - 1](con);
00055     if (ret != DRIZZLE_RETURN_OK)
00056     {
00057       if (ret != DRIZZLE_RETURN_IO_WAIT && ret != DRIZZLE_RETURN_PAUSE &&
00058           ret != DRIZZLE_RETURN_ERROR_CODE)
00059       {
00060         drizzle_con_close(con);
00061       }
00062 
00063       return ret;
00064     }
00065   }
00066 
00067   return DRIZZLE_RETURN_OK;
00068 }
00069 
00070 drizzle_return_t drizzle_state_packet_read(drizzle_con_st *con)
00071 {
00072   if (con == NULL)
00073   {
00074     return DRIZZLE_RETURN_INVALID_ARGUMENT;
00075   }
00076 
00077   drizzle_log_debug(con->drizzle, "drizzle_state_packet_read");
00078 
00079   if (con->buffer_size < 4)
00080   {
00081     drizzle_state_push(con, drizzle_state_read);
00082     return DRIZZLE_RETURN_OK;
00083   }
00084 
00085   con->packet_size= drizzle_get_byte3(con->buffer_ptr);
00086 
00087   if (con->packet_number != con->buffer_ptr[3])
00088   {
00089     drizzle_set_error(con->drizzle, "drizzle_state_packet_read",
00090                       "bad packet number:%u:%u", con->packet_number,
00091                       con->buffer_ptr[3]);
00092     return DRIZZLE_RETURN_BAD_PACKET_NUMBER;
00093   }
00094 
00095   drizzle_log_debug(con->drizzle, "packet_size= %zu, packet_number= %u",
00096                     con->packet_size, con->packet_number);
00097 
00098   con->packet_number++;
00099 
00100   con->buffer_ptr+= 4;
00101   con->buffer_size-= 4;
00102 
00103   drizzle_state_pop(con);
00104   return DRIZZLE_RETURN_OK;
00105 }