The SDClient API lets an external application connect to SD, open files, read and write records, execute commands, call subroutines, and manage select lists — all through a single shared library.
The API is a normal way to use SD, not a facility reserved for developers
and administrators. A person running a custom GUI program that talks to SD needs
API access and may need nothing else. On SD Core Solo for Windows the API is
off unless you chose it when installing (a computer a server manages needs it
open), and it signs in to the one account, sduser, with the account password —
see API access in the GettingStarted set.
The library#
The client library is written new for SD Core (a clean-room library under the
Blue Oak Model License 1.0.0), so an application of any licence may link it; it
is the same library for all four SD Core products. It is delivered as binaries
in the release package's clients folder (clients\Windows64), and the
installer copies it from there; its source is not published. Two 64-bit DLLs are
installed, sdclilib.dll and sdclient.dll: the same code under two names, so
the two files are not identical on disk but behave identically. An installer run
without the clients folder beside it installs no client library; copy the
files from a release package instead.
sdclilib.dll is the name existing applications ask for; sdclient.dll is for
new work. Renaming one to the other does not work: an import library
records the DLL name its symbols come from, so an application built against
sdclilib loads sdclilib.dll whatever you call the file on disk. That is why
both names are delivered rather than one being a copy of the other.
There is no 32-bit client. The architecture must match the application, not the computer, so a 32-bit application cannot load these DLLs; it has to be rebuilt as a 64-bit program. A program written for QM does not work with SD out of the box: the login, the network session and the server's rules have changed, so it needs changing.
Where they are, and how to use them#
%USERPROFILE%\SDCoreSolo\usr\clients\client64\ sdclilib.dll sdclient.dll
Copy the DLL your application needs either beside the application's own
executable or into C:\Windows\System32. Those are the two supported routes
and either works.
%USERPROFILE%\SDCoreSolo\usr\binis on your PATH, because the installer adds it every time it runs — it is what makessd-solorun from any directory. Both client DLLs live in that directory too, so an application may find one without your having copied anything. That is convenient, and it is not the same as choosing which copy it loads: PATH order decides, and a stale copy earlier on the PATH wins. Put the DLL where your application will find it deliberately.
usr\clients holds the DLLs and one import library for each — .dll.a
files, GNU-style, for linking rather than loading:
client64\ libsdclilib.dll.a libsdclient.dll.a
They sit beside the DLLs so that everything a client build needs is in one
place — apart from the header, which is elsewhere: sdclilib.h is at
%USERPROFILE%\SDCoreSolo\sdsys\syscom\sdclilib.h, with the Gambas and
PureBasic bindings, sdclient.bas and sdclient.pb, beside it.
Both DLLs also appear in %USERPROFILE%\SDCoreSolo\usr\bin, beside
sd-solo.exe. Those copies are SD's own and
are not the ones you should be taking. The import libraries are deliberately
not in usr\bin: a linker input has no business in a directory that goes on
the PATH.
Connection#
SDConnect(host, port, user, pass, account) |
over the network, to port 4249 - name it: the library is SD Core's too, and a port of -1 means SD Core's 4247. Use 127.0.0.1 for this computer; user is sduser |
SDConnectLocal(account) |
does not work on Solo. Returns false at once |
SDConnectUDS(Unix Domain Socket) is not available, on either port. It sent its credential over the socket in a way that predates SCRAM, and was removed for the same reason the old cleartext API login was.
Why SDConnectLocal does not work#
SDConnectLocal sends no password, and every SD Core Solo for Windows session
proves the account password. The library's SDConnectLocal starts the server
program beside the DLL, which on SD Core is sd.exe; Solo's is sd-solo.exe, so
it returns false immediately, with SDError() saying why, and no session is
started. (Were one started, the server would refuse it anyway.) Connect with
SDConnect and the account password; the API has to be on - see API access in
the GettingStarted set.
The login is SCRAM-SHA-256, inside TLS 1.3. A client that sends a password in clear is refused. The server sets a puzzle only someone who knows the password can answer, and the password itself is never sent in any form. The server also proves itself to the client — another program that grabbed the port before SD started cannot pretend to be SD. The password is the account password — on a managed computer, the global password, which the SD Core for Linux server uses.
A session is confined to its own account. An API session can open everything inside its own account and the shipped SD system files every account needs, but cannot open, rename, delete or list anything else.
Server status codes#
| Constant | Value | Meaning |
|---|---|---|
SV_OK |
0 | success |
SV_ON_ERROR |
1 | an ON ERROR clause fired |
SV_ELSE |
2 | an ELSE clause fired |
SV_ERROR |
3 | an error occurred |
SV_LOCKED |
4 | the record is locked by another session |
SV_PROMPT |
5 | the command issued a prompt and is waiting for input |
Function reference#
Connection management#
| Function | Returns | Description |
|---|---|---|
SDConnect(host, port, user, pass, account) |
Boolean | connect over TCP. A connection that fails returns false and SDError() says why; the session number it was given is the current one afterwards, so select the session you were using with SDSetSession before going on |
SDConnectLocal(account) |
Boolean | does not work on Solo — always false |
SDConnected() |
Boolean | is a session active |
SDDisconnect() |
none | end the current session |
SDDisconnectAll() |
none | end all sessions |
SDGetSession() |
Integer | get the current session number |
SDSetSession(session) |
Boolean | switch to a session |
SDLogto(account) |
Boolean | switch to another account: true if the server moved there, false (and SDError() says why) if not, in which case the session stays where it was. Solo has one account, so there is nowhere else to go |
Sessions. One program can hold up to 1024 sessions at once, each with its own server process and number. The server's own user table (USERPOOL) is the real ceiling. Open more than one with SDConnect, one after another, noting SDGetSession() each time, and move between them with SDSetSession.
Threads. The client library may be used from several threads of one process, as OpenQM's QMClient may, on these rules:
- The current session belongs to the thread.
SDSetSessionandSDGetSessionact on the calling thread's own selection, so two threads can work on two sessions at the same time. A thread that has not selected a session starts on session 0, which is the one the first connection of the process received; a thread given a session by another thread mustSDSetSessionit before use. - A session must not be used by two threads at the same time. The library does not serialize requests on one session. If more than one thread can reach it, take your own lock round each action, and treat an
SDExecuteand theSDRespondcalls that answer it as one action. - The table of sessions is shared, so
SDConnect,SDDisconnectandSDDisconnectAllmay be called from any thread; each connect is given a slot of its own however many start at the same moment. - The transfer buffer and the
SDCallx/SDGetArgarguments are per thread, so one thread's call does not overwrite another's results. They are released when the thread ends, or when no session is left. - A connection that fails keeps its slot for the thread that made it, so that
SDError()still says why after another thread has connected and disconnected; the slot is given back by that thread's nextSDConnect,SDSetSessionorSDDisconnect, by its end, or bySDDisconnectAll. - The same applies to a session that dies under its thread (
SDConnected()is false for it andSDError()says why).
A session whose server has ended — its AUTOLOGOUT period ran out, it was logged out, the process was killed — does not hang the client: the next request on it comes back empty, SDError() says the connection failed, and SDDisconnect() cleans it up. A session can set its own limit by executing the AUTOLOGOUT command (SDExecute("AUTOLOGOUT 10"): ten minutes idle; the period is in minutes, 0 turns it off).
File operations#
| Function | Returns | Description |
|---|---|---|
SDOpen(filename) |
Integer (file number) | open a file |
SDClose(fileNo) |
none | close a file |
SDRead(fileNo, id, err) |
String | read a record |
SDReadl(fileNo, id, wait, err) |
String | read with a shared lock |
SDReadu(fileNo, id, wait, err) |
String | read with an update lock |
SDWrite(fileNo, id, data) |
none | write a record |
SDWriteu(fileNo, id, data) |
none | write with an update lock |
SDDelete(fileNo, id) |
none | delete a record |
SDDeleteu(fileNo, id) |
none | delete with an update lock |
SDRecordlock(fileNo, id, updateLock, wait) |
none | set a record lock |
SDRelease(fileNo, id) |
none | release a record lock |
SDRecordlocked(fileNo, id) |
Integer | query the lock on a record, as recordlocked() does: -3 another user holds a file lock, -2 an update lock, -1 a read lock; 0 not locked; 1 this session holds a read lock, 2 an update lock, 3 a file lock. A file number that is not open gives 0 and SDError() says so |
SDMarkMapping(fileNo, state) |
none | enable/disable mark mapping |
Record manipulation#
| Function | Returns | Description |
|---|---|---|
SDExtract(src, fno, vno, svno) |
String | extract a field, value or subvalue |
SDIns(src, fno, vno, svno, newdata) |
String | insert into a dynamic array |
SDDel(src, fno, vno, svno) |
String | delete from a dynamic array |
SDReplace(src, fno, vno, svno, newdata) |
String | replace a field, value or subvalue |
SDLocate(item, src, fno, vno, svno, pos, order) |
Boolean | find a position for ordered insert |
String functions#
| Function | Returns | Description |
|---|---|---|
SDField(str, delim, first, occurrences) |
String | extract a field from a delimited string |
SDDcount(src, delim) |
Long | the number of delimited items: 1 more than the number of times delim occurs, counting non-overlapping occurrences from the left; 0 for a null string or a null delimiter. The delimiter may be more than one character (a::b::c by :: is 3) |
SDChange(str, old, new, occurrences, start) |
String | replace substrings |
SDMatch(src, template) |
Boolean | match a string against a pattern |
SDMatchfield(src, template, component) |
String | extract a matched component |
SDSubstr(...) |
String | substring (language-specific) |
SDChecksum(data) |
Long | the number SD's own checksum() gives for the same string, worked out in the library with no connection to SD. A byte of 128 or more is taken as a signed character, as the server reads it. A check for accidental damage, not a hash for security (the message digests are the server's digest()); the null string is 0 |
Command execution#
| Function | Returns | Description |
|---|---|---|
SDExecute(cmnd, err) |
String | execute a TCL command |
SDRespond(response, err) |
String | respond to a prompt from SDExecute |
SDEndCommand() |
none | end a multi-line command |
SDCall(name, argcount, ...) |
none | call a cataloged subroutine; the arguments the subroutine changed are put back into yours |
SDCallx(name, argcount, ...) |
none | the same call, but your arguments are not changed: read each one the subroutine changed with SDGetArg(n) afterwards |
SDGetArg(argNo) |
String | argument n (1 to 20) of the last SDCallx; free it with SDFree |
SDCallandSDCallxare variadic: 0 to 20 arguments. Each binding provides per-arity wrappers —SDCall0throughSDCall20, andSDCallx0throughSDCallx20.
Continued in#
SD Client API: Select Lists to Error Handling — the rest of the function reference.