I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tax 0:66300c77c6e9 1
tax 0:66300c77c6e9 2 /*
tax 0:66300c77c6e9 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
tax 0:66300c77c6e9 4
tax 0:66300c77c6e9 5 Permission is hereby granted, free of charge, to any person obtaining a copy
tax 0:66300c77c6e9 6 of this software and associated documentation files (the "Software"), to deal
tax 0:66300c77c6e9 7 in the Software without restriction, including without limitation the rights
tax 0:66300c77c6e9 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
tax 0:66300c77c6e9 9 copies of the Software, and to permit persons to whom the Software is
tax 0:66300c77c6e9 10 furnished to do so, subject to the following conditions:
tax 0:66300c77c6e9 11
tax 0:66300c77c6e9 12 The above copyright notice and this permission notice shall be included in
tax 0:66300c77c6e9 13 all copies or substantial portions of the Software.
tax 0:66300c77c6e9 14
tax 0:66300c77c6e9 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tax 0:66300c77c6e9 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tax 0:66300c77c6e9 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
tax 0:66300c77c6e9 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tax 0:66300c77c6e9 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tax 0:66300c77c6e9 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
tax 0:66300c77c6e9 21 THE SOFTWARE.
tax 0:66300c77c6e9 22 */
tax 0:66300c77c6e9 23
tax 0:66300c77c6e9 24 /** \file
tax 0:66300c77c6e9 25 MySQL Client header file
tax 0:66300c77c6e9 26 */
tax 0:66300c77c6e9 27
tax 0:66300c77c6e9 28 #ifndef MYSQL_CLIENT_H
tax 0:66300c77c6e9 29 #define MYSQL_CLIENT_H
tax 0:66300c77c6e9 30
tax 0:66300c77c6e9 31 #include "core/net.h"
tax 0:66300c77c6e9 32 #include "core/netservice.h"
tax 0:66300c77c6e9 33 #include "api/TCPSocket.h"
tax 0:66300c77c6e9 34 #include "api/DNSRequest.h"
tax 0:66300c77c6e9 35 #include "mbed.h"
tax 0:66300c77c6e9 36
tax 0:66300c77c6e9 37 #include <string>
tax 0:66300c77c6e9 38 using std::string;
tax 0:66300c77c6e9 39
tax 0:66300c77c6e9 40 #include <map>
tax 0:66300c77c6e9 41 using std::map;
tax 0:66300c77c6e9 42
tax 0:66300c77c6e9 43 typedef unsigned char byte;
tax 0:66300c77c6e9 44
tax 0:66300c77c6e9 45 ///MySQL client results
tax 0:66300c77c6e9 46 enum MySQLResult
tax 0:66300c77c6e9 47 {
tax 0:66300c77c6e9 48 MYSQL_OK, ///<Success
tax 0:66300c77c6e9 49 MYSQL_PROCESSING, ///<Processing
tax 0:66300c77c6e9 50 MYSQL_PRTCL, ///<Protocol error
tax 0:66300c77c6e9 51 MYSQL_SETUP, ///<Not properly configured
tax 0:66300c77c6e9 52 MYSQL_DNS, ///<Could not resolve name
tax 0:66300c77c6e9 53 MYSQL_AUTHFAILED, ///<Auth failure
tax 0:66300c77c6e9 54 MYSQL_READY, ///<Ready to send commands
tax 0:66300c77c6e9 55 MYSQL_SQL, ///<SQL Error
tax 0:66300c77c6e9 56 MYSQL_TIMEOUT, ///<Connection timeout
tax 0:66300c77c6e9 57 MYSQL_CONN ///<Connection error
tax 0:66300c77c6e9 58 };
tax 0:66300c77c6e9 59
tax 0:66300c77c6e9 60 ///A MySQL Client
tax 0:66300c77c6e9 61 /**
tax 0:66300c77c6e9 62 This MySQL client implements a limited subset of the MySQL internal client/server protocol (including authentication), for server versions 4.1 and newer.
tax 0:66300c77c6e9 63 */
tax 0:66300c77c6e9 64 class MySQLClient : protected NetService
tax 0:66300c77c6e9 65 {
tax 0:66300c77c6e9 66 public:
tax 0:66300c77c6e9 67 ///Instantiates the MySQL client
tax 0:66300c77c6e9 68 MySQLClient();
tax 0:66300c77c6e9 69 virtual ~MySQLClient();
tax 0:66300c77c6e9 70
tax 0:66300c77c6e9 71 //High Level setup functions
tax 0:66300c77c6e9 72
tax 0:66300c77c6e9 73 ///Opens a connection to a server
tax 0:66300c77c6e9 74 /**
tax 0:66300c77c6e9 75 Opens a connection to the server host using the provided username, password passowrd and selecting database
tax 0:66300c77c6e9 76 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter
tax 0:66300c77c6e9 77 @param host : server
tax 0:66300c77c6e9 78 @param user : username
tax 0:66300c77c6e9 79 @param db : database to use
tax 0:66300c77c6e9 80 @param pMethod : callback to call on each request completion
tax 0:66300c77c6e9 81 */
tax 0:66300c77c6e9 82 MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)); //Non blocking
tax 0:66300c77c6e9 83
tax 0:66300c77c6e9 84 ///Opens a connection to a server
tax 0:66300c77c6e9 85 /**
tax 0:66300c77c6e9 86 Opens a connection to the server host using the provided username, password passowrd and selecting database
tax 0:66300c77c6e9 87 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter
tax 0:66300c77c6e9 88 @param host : server
tax 0:66300c77c6e9 89 @param user : username
tax 0:66300c77c6e9 90 @param db : database to use
tax 0:66300c77c6e9 91 @param pItem : callback's class instance
tax 0:66300c77c6e9 92 @param pMethod : callback's method to call on each request completion
tax 0:66300c77c6e9 93 */
tax 0:66300c77c6e9 94 template<class T>
tax 0:66300c77c6e9 95 MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking
tax 0:66300c77c6e9 96 {
tax 0:66300c77c6e9 97 setOnResult(pItem, pMethod);
tax 0:66300c77c6e9 98 setup(host, user, password, db);
tax 0:66300c77c6e9 99 return MYSQL_PROCESSING;
tax 0:66300c77c6e9 100 }
tax 0:66300c77c6e9 101
tax 0:66300c77c6e9 102
tax 0:66300c77c6e9 103 ///Executes an SQL command
tax 0:66300c77c6e9 104 /**
tax 0:66300c77c6e9 105 Executes an SQL request on the SQL server
tax 0:66300c77c6e9 106 This is a non-blocking function
tax 0:66300c77c6e9 107 On completion, the callback set in the open function is fired with the result of the command in parameter
tax 0:66300c77c6e9 108 @param sqlCommand SQL request to execute
tax 0:66300c77c6e9 109 */
tax 0:66300c77c6e9 110 MySQLResult sql(string& sqlCommand);
tax 0:66300c77c6e9 111
tax 0:66300c77c6e9 112 ///Closes the connection to the server
tax 0:66300c77c6e9 113 MySQLResult exit();
tax 0:66300c77c6e9 114
tax 0:66300c77c6e9 115 void setOnResult( void (*pMethod)(MySQLResult) );
tax 0:66300c77c6e9 116 class CDummy;
tax 0:66300c77c6e9 117 template<class T>
tax 0:66300c77c6e9 118 void setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) )
tax 0:66300c77c6e9 119 {
tax 0:66300c77c6e9 120 m_pCb = NULL;
tax 0:66300c77c6e9 121 m_pCbItem = (CDummy*) pItem;
tax 0:66300c77c6e9 122 m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod;
tax 0:66300c77c6e9 123 }
tax 0:66300c77c6e9 124
tax 0:66300c77c6e9 125 ///Setups timeout
tax 0:66300c77c6e9 126 /**
tax 0:66300c77c6e9 127 @param ms : time of connection inactivity in ms after which the request should timeout
tax 0:66300c77c6e9 128 */
tax 0:66300c77c6e9 129 void setTimeout(int ms);
tax 0:66300c77c6e9 130
tax 0:66300c77c6e9 131 virtual void poll(); //Called by NetServices
tax 0:66300c77c6e9 132
tax 0:66300c77c6e9 133 protected:
tax 0:66300c77c6e9 134 void resetTimeout();
tax 0:66300c77c6e9 135
tax 0:66300c77c6e9 136 void init();
tax 0:66300c77c6e9 137 void close();
tax 0:66300c77c6e9 138
tax 0:66300c77c6e9 139 void setup(Host& host, const string& user, const string& password, const string& db); //Setup connection, make DNS Req if necessary
tax 0:66300c77c6e9 140 void connect(); //Start Connection
tax 0:66300c77c6e9 141
tax 0:66300c77c6e9 142 void handleHandshake();
tax 0:66300c77c6e9 143 void sendAuth();
tax 0:66300c77c6e9 144
tax 0:66300c77c6e9 145 void handleAuthResult();
tax 0:66300c77c6e9 146 void sendAuth323();
tax 0:66300c77c6e9 147
tax 0:66300c77c6e9 148 void sendCommand(byte command, byte* arg, int len);
tax 0:66300c77c6e9 149 void handleCommandResult();
tax 0:66300c77c6e9 150
tax 0:66300c77c6e9 151 void readData(); //Copy to buf
tax 0:66300c77c6e9 152 void writeData(); //Copy from buf
tax 0:66300c77c6e9 153
tax 0:66300c77c6e9 154 void onTCPSocketEvent(TCPSocketEvent e);
tax 0:66300c77c6e9 155 void onDNSReply(DNSReply r);
tax 0:66300c77c6e9 156 void onResult(MySQLResult r); //Called when exchange completed or on failure
tax 0:66300c77c6e9 157 void onTimeout(); //Connection has timed out
tax 0:66300c77c6e9 158
tax 0:66300c77c6e9 159 private:
tax 0:66300c77c6e9 160 CDummy* m_pCbItem;
tax 0:66300c77c6e9 161 void (CDummy::*m_pCbMeth)(MySQLResult);
tax 0:66300c77c6e9 162
tax 0:66300c77c6e9 163 void (*m_pCb)(MySQLResult);
tax 0:66300c77c6e9 164
tax 0:66300c77c6e9 165 TCPSocket* m_pTCPSocket;
tax 0:66300c77c6e9 166
tax 0:66300c77c6e9 167 Timer m_watchdog;
tax 0:66300c77c6e9 168 int m_timeout;
tax 0:66300c77c6e9 169
tax 0:66300c77c6e9 170 DNSRequest* m_pDnsReq;
tax 0:66300c77c6e9 171
tax 0:66300c77c6e9 172 bool m_closed;
tax 0:66300c77c6e9 173
tax 0:66300c77c6e9 174 enum MySQLStep
tax 0:66300c77c6e9 175 {
tax 0:66300c77c6e9 176 // MYSQL_INIT,
tax 0:66300c77c6e9 177 MYSQL_HANDSHAKE,
tax 0:66300c77c6e9 178 MYSQL_AUTH,
tax 0:66300c77c6e9 179 MYSQL_COMMANDS,
tax 0:66300c77c6e9 180 MYSQL_CLOSED
tax 0:66300c77c6e9 181 };
tax 0:66300c77c6e9 182
tax 0:66300c77c6e9 183 //Parameters
tax 0:66300c77c6e9 184 Host m_host;
tax 0:66300c77c6e9 185
tax 0:66300c77c6e9 186 string m_user;
tax 0:66300c77c6e9 187 string m_password;
tax 0:66300c77c6e9 188 string m_db;
tax 0:66300c77c6e9 189
tax 0:66300c77c6e9 190 //Low-level buffers & state-machine
tax 0:66300c77c6e9 191 MySQLStep m_state;
tax 0:66300c77c6e9 192
tax 0:66300c77c6e9 193 byte* m_buf;
tax 0:66300c77c6e9 194 byte* m_pPos;
tax 0:66300c77c6e9 195 int m_len;
tax 0:66300c77c6e9 196 int m_size;
tax 0:66300c77c6e9 197
tax 0:66300c77c6e9 198 int m_packetId;
tax 0:66300c77c6e9 199
tax 0:66300c77c6e9 200 };
tax 0:66300c77c6e9 201
tax 0:66300c77c6e9 202 #endif