📄 umysqlvio.pas
字号:
{--------------------------------------------------------------------------------
Licencing issues:
23-March-2002 〤ristian Nicola
Note:
Mysql is copyright by MySQL AB. Refer to their site ( http://www.mysql.com )
for licencing issues.
Zlib is copyright by Jean-loup Gailly and Mark Adler. Refer to their site for
licencing issues. ( http://www.info-zip.org/pub/infozip/zlib/ )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
NOTES:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. If you are using it for a commercial software it must be open source and
it must include full source code of this library in an unaltered fashion
or you would need to ask for permission to use it. This library will be
considered donationware which means if you want to contribute with any money
or hardware you are more than welcome.
4. This notice may not be removed or altered from any source distribution.
Cristian Nicola
n_cristian@hotmail.com
If you use the mysqldirect library in a product, i would appreciate *not*
receiving lengthy legal documents to sign. The sources are provided
for free but without warranty of any kind. The library has been
entirely written by Cristian Nicola after libmysql of MYSQL AB.
--------------------------------------------------------------------------------}
unit uMysqlVio;
////////////////////////////////////////////////////////////////////////////////
// VIO = Virtual Input Output
// supposed to hide low level read/write from the other 2 levels
// this will be controled and used by "net" no one should ever need to use it directly
interface
{$I mysqlinc.inc}
uses sysutils, uMysqlErrors, uMysqlCT {$IFDEF HAVE_SSL},uMysqlSSL{$ENDIF};
type
TMysqlVio = class (TObject)
private
FSd : longint;
FHPipe : longint;
ffcntl_mode : longint;
ftype : TEnumVioType;
flast_error : string[MYSQL_ERRMSG_SIZE];
flast_errno : cardinal;
{$IFDEF HAVE_SSL}
fssl : pointer;
fnewcon : pointer;
{$ENDIF}
{$IFDEF NEVERENABLEME}public{$ENDIF}
ftimeout : longint; //22-03-2002
{$IFDEF NEVERENABLEME}private{$ENDIF}
fNoTimeOut : boolean;
procedure Setlast_error(const Value: string);
procedure SetNoTimeOut(const Value: boolean);
function Getlast_error: string;
function fastsend:longint;
function keepalive(onoff:boolean):longint;
function vio_poll_read(timeout:cardinal):boolean;
{$IFDEF HAVE_SSL}
function new_VioSSLConnectorFd(const key:pchar;const cert:pchar;const ca:pchar;const capath:pchar;const cipher:pchar):pointer;
{$ENDIF}
public
property fcntl_mode : longint read ffcntl_mode;
property last_error : string read Getlast_error write Setlast_error;
property last_errno : cardinal read flast_errno write flast_errno;
property VIO_type : TEnumVioType read ftype;
property NoTimeOut : Boolean read fNoTimeOut write SetNoTimeOut;
constructor create;
destructor destroy; override;
function vio_read (buf:pointer; const sz:longint):longint;
function vio_write (buf:pointer; size:longint):longint;
function vio_blocking(onoff:boolean):longint;
function vio_intrerupted:boolean;
function vio_should_retry:boolean;
function vio_open( _type:TEnumVioType; host:string='localhost'; unix_socket:string={$IFDEF _WIN_}MYSQL_NAMEDPIPE{$ELSE}MYSQL_UNIX_ADDR{$ENDIF}; port:longint=0; connect_timeout:cardinal=0; trysock:boolean=true):longint;
function vio_close:longint;
{$IFDEF HAVE_SSL}
procedure SwitchToSSL(const key:pchar;const cert:pchar;const ca:pchar;const capath:pchar;var cipher:pchar; timeout:cardinal);
{$ENDIF}
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
const
//poll constants
IPPROTO_IP = 0;
IP_TOS = {$IFDEF _WIN_}8;{$ELSE}1;{$ENDIF} //need check
SOL_SOCKET = {$IFDEF _WIN_}$ffff;{$ELSE}1;{$ENDIF} //need check
SO_KEEPALIVE = {$IFDEF _WIN_}$0008;{$ELSE}$0009;{$ENDIF} //need check
IPPROTO_TCP = 6;
TCP_NODELAY = $0001;
FD_SETSIZE = {$IFDEF _WIN_}64;{$ELSE}1024;{$ENDIF} //need check
POLLIN=1;
POLLPRI=2;
POLLOUT=4;
POLLERR=8;
POLLHUP=16;
POLLNVAL=32;
POLLRDNORM=POLLIN;
POLLRDBAND=POLLPRI;
POLLWRNORM=POLLOUT;
POLLWRBAND=POLLOUT;
POLL_CAN_READ=(POLLIN or POLLRDNORM );
POLL_CAN_WRITE=(POLLOUT or POLLWRNORM or POLLWRBAND );
POLL_HAS_EXCP=(POLLRDBAND or POLLPRI );
POLL_EVENTS_MASK=(POLL_CAN_READ or POLL_CAN_WRITE or POLL_HAS_EXCP);
{$IFDEF _WIN_}
IOC_IN = $80000000;
IOCPARM_MASK = $7f;
FIONBIO = IOC_IN or { set/clear non-blocking i/o }
((Longint(SizeOf(Longint)) and IOCPARM_MASK) shl 16) or
(Longint(Byte('f')) shl 8) or 126;
{$ENDIF}
type
//poll types for poll reading
PFDSet = ^TFDSet;
TFDSet = record
fd_count: longint;
fd_array: array[0..FD_SETSIZE-1] of longint;
end;
PTimeVal = ^TimeVal;
TimeVal = record
tv_sec: Longint;
tv_usec: Longint;
end;
TPollFD = record
fd:longint;
events:smallint;
revents:smallint;
end;
{$IFNDEF _WIN_}
PPollFD = ^TPollFD;
{$ENDIF}
const
AF_UNIX = 1;
AF_INET = 2;
SOCK_STREAM = 1;
SOCKET_ERROR = -1;
INADDR_NONE = $FFFFFFFF;
{$IFNDEF _WIN_}
const
F_SETFL = 4;
F_GETFL = 3;
__NFDBITS = 8 * sizeof(longword);
EAGAIN = 11; { Try again }
EINTR = 4; { Interrupted system call }
{$ENDIF}
{$IFDEF _WIN_}
const
WSABASEERR = 10000;
WSAEINTR = (WSABASEERR+4);
WSAEWOULDBLOCK = (WSABASEERR+35);
WSAEINPROGRESS = (WSABASEERR+36);
WSADESCRIPTION_LEN = 256;
WSASYS_STATUS_LEN = 128;
{$ENDIF}
const INVALID_HANDLE_VALUE = -1;
type
PHostEnt = ^HostEnt;
HostEnt = record
h_name: PChar;
h_aliases: ^PChar;
h_addrtype: {$IFDEF _WIN_}Smallint;{$ELSE}longint;{$ENDIF}
h_length: {$IFDEF _WIN_}Smallint;{$ELSE}cardinal;{$ENDIF}
case Byte of
0: (h_addr_list: ^PChar);
1: (h_addr: ^PChar)
end;
SunB = packed record
s_b1, s_b2, s_b3, s_b4: char;
end;
SunW = packed record
s_w1, s_w2: word;
end;
in_addr = record
case longint of
0: (S_un_b: SunB);
1: (S_un_w: SunW);
2: (S_addr: longint);
end;
{$IFNDEF _WIN_}
type
TSockAddr = record
case Integer of
0: (sa_family: word;
sa_data: packed array[0..13] of Byte);
1: (sin_family: word;
sin_port: word;
sin_addr: in_addr;
sin_zero: packed array[0..7] of Byte);
end;
TUnixSockAddr = record
sun_family:word; //* address family AF_LOCAL/AF_UNIX */
sun_path:array[0..107]of char; //* 108 bytes of socket address */
end;
{$ELSE}
TSockAddr = record
case longint of
0: (sin_family: word;
sin_port: word;
sin_addr: in_addr;
sin_zero: array[0..7] of Char);
1: (sa_family: word;
sa_data: array[0..13] of Char)
end;
{$ENDIF}
{$IFDEF _WIN_}
type
WSAData = record
wVersion: Word;
wHighVersion: Word;
szDescription: array[0..WSADESCRIPTION_LEN] of Char;
szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char;
iMaxSockets: Word;
iMaxUdpDg: Word;
lpVendorInfo: PChar;
end;
//winsock imports
function setsockopt(s: longint; level, optname: longint; optval: PChar; optlen: longint): longint; stdcall; external 'wsock32.dll' name 'setsockopt';
function ioctlsocket(s: longint; cmd: LongWORD; var arg: longint): longint; stdcall; external 'wsock32.dll' name 'ioctlsocket';
function send(s: longint; var Buf; len, flags: longint): longint; stdcall; external 'wsock32.dll' name 'send';
function recv(s: longint; var Buf; len, flags: longint): longint; stdcall; external 'wsock32.dll' name 'recv';
function WSAGetLastError: longint; stdcall; external 'wsock32.dll' name 'WSAGetLastError';
function __WSAFDIsSet(s: longint; var FDSet: TFDSet): LongBool; stdcall; external 'wsock32.dll' name '__WSAFDIsSet';
function select(nfds: longint; readfds, writefds, exceptfds: PFDSet; timeout: PTimeVal): Longint; stdcall; external 'wsock32.dll' name 'select';
function shutdown(s: longint; how: longint): longint; stdcall; external 'wsock32.dll' name 'shutdown';
function closesocket(s: longint): longint; stdcall; external 'wsock32.dll' name 'closesocket';
function WSACleanup: longint; stdcall; external 'wsock32.dll' name 'WSACleanup';
function WSAStartup(wVersionRequired: word; var WSData: WSAData): longint; stdcall; external 'wsock32.dll' name 'WSAStartup';
function socket(af, Struct, protocol: longint): longint; stdcall; external 'wsock32.dll' name 'socket';
function inet_addr(cp: PChar): longint; stdcall; external 'wsock32.dll' name 'inet_addr';
function gethostbyname(name: PChar): PHostEnt; stdcall; external 'wsock32.dll' name 'gethostbyname';
function htons(hostshort: longint): longint; stdcall; external 'wsock32.dll' name 'htons';
function connect(s: longint; var name: TSockAddr; namelen: longint): longint; stdcall; external 'wsock32.dll' name 'connect';
type
//windows pipes type
POverlapped = ^_Overlapped;
_OVERLAPPED = record
Internal: longWORD;
InternalHigh: longWORD;
Offset: longWORD;
OffsetHigh: longWORD;
hEvent: longword;
end;
PSecurityAttributes = ^TSecurity_Attributes;
TSECURITY_ATTRIBUTES = record
nLength: LongWORD;
lpSecurityDescriptor: Pointer;
bInheritHandle: LongBOOL;
end;
const
//windows pipes constants
GENERIC_READ = LongWORD($80000000);
GENERIC_WRITE = $40000000;
OPEN_EXISTING = 3;
ERROR_PIPE_BUSY = 231;
PIPE_READMODE_BYTE = 0;
PIPE_WAIT = 0;
//windows pipes functions
function WriteFile(hFile: longword; const Buffer; nNumberOfBytesToWrite: longword;
var lpNumberOfBytesWritten: longword; lpOverlapped: POverlapped): longbool; stdcall; external 'kernel32.dll' name 'WriteFile';
function ReadFile(hFile: longword; var Buffer; nNumberOfBytesToRead: longword;
var lpNumberOfBytesRead: longword; lpOverlapped: POverlapped): longbool; stdcall; external 'kernel32.dll' name 'ReadFile';
function CloseHandle(hObject: longword): longbool; stdcall;external 'kernel32.dll' name 'CloseHandle';
function CreateFile(lpFileName: PChar; dwDesiredAccess, dwShareMode: LongWORD;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -